|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
本帖最后由 刘淼淼 于 2023-4-12 17:27 编辑
Sub Main Dim SurferApp As Object '定义Surfer软件启动对象
Dim BaseName,basename1,inputFile,outputFile As String
Dim Wks As Object
Dim WksRange1 As Object
Set SurferApp = CreateObject("Surfer.Application") '启动Surfer
SurferApp.Visible = True '因为仅仅是计算,不显示,效率高点。设置窗口不可见
'SurferApp.WindowState = srfWindowStateMaximized 'surfer最大化
path="C:\Users\Administrator\Desktop\2023.4.6自动化编程试验\2023.4.6切片slice自动化试验数据\" '设置工作目录 SurferApp.ScreenUpdating=True '设置图形窗口是否刷新,至此已完成surfer的初始设置。
Dim excelApp As Object
Set excelApp = CreateObject("Excel.Application")
excelApp.Visible = True '因为仅仅是计算,不显示,效率高点。设置窗口不可见
excelApp.WindowState = srfWindowStateMaximized 'surfer最大化
excelApp.ScreenUpdating = TrueDim xlBook As Object
Set xlBook = excelApp.Workbooks.Add
excelApp.ScreenUpdating = False
Dim DataArr As Object, DataWb As Object, DataSht As Object
Dim EndRow As Long, ToSht As Object, ToRng As ObjectDim FileName2 As String '要合并的工作簿名称
Dim a As Long, b As Long
Set ToSht = xlBook.Worksheets(1)ToSht.Rows("2:1048576").Clear '清除原有数据
FileName2 = Dir(path& "\*.csv")
Do While FileName2 <> ""
excelApp.Workbooks.Open FileName:= FileName2
Set DataWb = xlBook
Set DataSht = DataWb.Worksheets(1)
EndRow = DataSht.Range("A1048576").End(xlUp).Row '提示错误1004
DataArr = DataSht.Range("A2").Resize(EndRow - 1, 8).Value
Set ToRng = ToSht.Range("A1048576").End(xlUp).Offset(1, 0)
For a = 1 To UBound(DataArr, 1) '将数组中超过15位的数字转为文本
For b = 1 To UBound(DataArr, 2)
If Len(DataArr(a, b)) > 15 Then
DataArr(a, b) = "'" & DataArr(a, b)
End If
Next b
Next a
ToRng.Resize(UBound(DataArr, 1), 8).Value = DataArr
DataWb.Close savechanges:=False
FileName2 = Dir
Loop
'Application.ScreenUpdating = TrueMsgBox "合并完成!"
'SurferApp.Quit() '退出Surfer应用程序
End Sub
代码如上我是在surfer scripter中编 辑的上述代码,首先利用surfer 处理,接着利用ExcelVBA 进行合并表格,首先新建一个xlbook,利用它,实现一个文件夹下的多个.csv文件合并到一个Excel表中。EndRow = DataSht.Range("A1048576").End(xlUp).Row '提示错误1004是什么原因了,请教各位大侠,还是这个合并的代码本身就有问题了
|
|