我已经编写了下面的代码,使用VB代码从MS excel生成记事本格式的数据。 我在记事本中获取文件,但问题是如果excel中有多个工作表,那么我也会得到一个提取。我想在diff文件中获取所有工作表的摘录。
请建议。
Private Declare Function GetTempPath Lib "kernel32" Alias "GetTempPathA" _
(ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long
Private Const MAX_PATH As Long = 260
'~~> Change this where and how you want to save the file
Const FlName = "C:\My\excel\MyWorkbook.vbs"
Sub Sample()
Dim tmpFile As String
Dim MyData As String, strData() As String
Dim entireline As String
Dim filesize As Integer
'~~> Create a Temp File
tmpFile = "C:\My\excel\Sheet1.vbs"
ActiveWorkbook.SaveAs Filename:=tmpFile _
, FileFormat:=xlText, CreateBackup:=False
'~~> Read the entire file in 1 Go!
Open tmpFile For Binary As #1
MyData = Space$(LOF(1))
Get #1, , MyData
Close #1
strData() = Split(MyData, vbCrLf)
'~~> Get a free file handle
filesize = FreeFile()
'~~> Open your file
Open FlName For Output As #filesize
For i = LBound(strData) To UBound(strData)
entireline = Replace(strData(i), """", "")
'~~> Export Text
Print #filesize, entireline
Next i
Close #filesize
MsgBox "Done"
End Sub
'Function TempPath() As String
'TempPath = String$(MAX_PATH, Chr$(0))
'GetTempPath MAX_PATH, TempPath
'TempPath = Replace(TempPath, Chr$(0), "")
'End Function
答案 0 :(得分:0)
当您使用语句ActiveWorkbook.SaveAs ...
时,您将立即保存所有工作表。相反,循环遍历工作表并单独保存每个工作表......
For Each Sheet In ActiveWorkbook.Sheets
Sheet.SaveAs Filename:=tmpFile, FileFormat:=xlText, CreateBackup:=False
' Open and process the file...
Next