我的单元格中有数据,其中的方程链接到另一个单元格。
导出后无法找到值,因此在所述文本文件中写入 # Ref。
还写着“;” (逗号用分号)。
Sub ExportRangetoFile()
'Update 20130913
Dim wb As Workbook
Dim saveFile As String
Dim WorkRng As Range
On Error Resume Next
xTitleId = "KutoolsforExcel"
Set WorkRng = Application.Selection
Set WorkRng = Application.InputBox("Range", xTitleId, WorkRng.Address, Type:=8)
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Set wb = Application.Workbooks.Add
WorkRng.Copy
wb.Worksheets(1).Paste
saveFile = Application.GetSaveAsFilename(fileFilter:="Text Files (*.txt), *.txt")
wb.SaveAs Filename:=saveFile, FileFormat:=xlText, CreateBackup:=False
wb.Close
Application.CutCopyMode = False
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
不要使用复制/粘贴,而是使用选择性粘贴、值或仅将目标范围的值分配给源范围的值。这样您就不会将公式粘贴到新的工作簿中,也不会获得#Ref
编辑:问题的第二半 - 尝试使用正确的文件格式之一(xlText 不是)来保存逗号分隔的文件 - 请参阅 https://learn.microsoft.com/en-us/office/vba/ api/excel.xl文件格式
这是一个简单的 VBA Sub,它处理使用显示的文本而不是单元格中的公式的 OP 要求。
需要注意的重要一点是
Cell
的属性是.Text
而不是.Value
Sub sbMakeTXT()
Dim iRows, iColumns, iRow, iColumn As Long
Dim sText As String
Dim sFileName As String
Dim sTXTLine As String
Dim sTab As String
Dim iFileNumber As String
Dim rSelectedRange As Range
Set rSelectedRange = Application.Selection
iColumns = rSelectedRange.Columns.Count
iRows = rSelectedRange.Rows.Count
sTab = Chr(9)
sTXTLine = ""
sFileName = "C:\Users\david\Desktop\a.txt"
iFileNumber = FreeFile
Open sFileName For Output As iFileNumber
For iRow = 1 To iRows
For iColumn = 1 To iColumns
' Use the .Text in the Cell not .Value to deal with formula results
sText = Cells(iRow, iColumn).Text
' Remove TABs in case they interfere with the file structure
sText = Replace(sText, sTab, "")
sTXTLine = sTXTLine & sTab & sText
Next iColumn
Print #iFileNumber, sTXTLine
sTXTLine = ""
Next iRow
Close #iFileNumber
End Sub
使用以下数据作为测试数据 -
发货人ID | 公司名称 | 电话 |
---|---|---|
1 | 极速快递 | (503)555-9831 |
2 | 联合套餐 | (503)555-3199 |
3 | 联邦航运 | (503)555-9931 |