我已完成以下代码将Excel单元格数据复制到txt文件。我必须为Excel中的每个单元格创建一个文本文件。就像我在Excel中有n个单元格数据一样,我需要n个txt文件。
Sub ExportToNotepad()
Dim wsData As Variant
Dim myFileName As String
Dim FN As Integer
Dim p As Integer, q As Integer
Dim sPath As String
Dim myString As String
Dim lastrow As Long, lastcolumn As Long
Dim wb As Workbook
Set wb = Workbooks.Open("C:\Users\sc00359878\Desktop\sample_excel.xlsx")
lastrow = wb.Sheets("sheet1").Range("B" & Rows.Count).End(xlUp).Row
lastcolumn = ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column
sPath = "C:\Users\sc00359878\Desktop\output\"
For p = 1 To lastrow
wsData = ActiveSheet.Range("B" & p).Value
If wsData = "" Then Exit Sub
myFileName = "sMove.txt"
myFileName = sPath & myFileName
FN = FreeFile
Open myFileName For Output As #FN
Print #FN, wsData
Close #FN
myString = ""
Next p
End Sub
我尝试运行时遇到以下错误。
答案 0 :(得分:3)
与VB的其他“风格”不同,您不能将变量声明为VBScript中的数据类型,因为VBScript is not a strongly-typed language。换句话说,
Dim wsData As Variant
应该是
Dim wsData
并且您需要修复执行此操作的所有其他变量声明。