【问题标题】:Unable to merge different log files无法合并不同的日志文件
【发布时间】:2018-11-26 06:40:28
【问题描述】:

Getting Run time error 1004.

总结:- 我需要将位于永久位置的不同系统生成的日志文件复制到位于相同或任何随机位置的工作簿。 这些日志是从客户端的服务器复制的,并且以非格式化方式包含数千行数据。 该数据每天刷新。它以 .log 文件的形式出现。

文件命名为 ms.log、ms.log.1、ms.log.2 等。

我的代码在一段时间内运行良好。但是由于几天后收到一条错误消息,指出“运行时错误 1004”。错误提示“无法打开文件,因为第 2 行位置 620 应有分号”。

附上错误截图。

错误调试器标记为黄色的代码行:

Set xWb = Workbooks.Open(xStrPath & xFiles.Item(i))

错误仅出现在一个日志文件中。如果我跳过该文件,则没有错误。如果我手动打开并复制该文件的内容,则没有错误。

Sub CopyAllData()

Dim xWb As Workbook
Dim xToBook As Workbook
Dim xStrPath As String
Dim xFileDialog As FileDialog
Dim xFile As String
Dim xFiles As New Collection
Dim i As Long
path1 = "C:\Users\BhatiaP\Desktop\Project\"

Application.ScreenUpdating = False
Application.EnableEvents = False
Application.DisplayAlerts = False

Set xFileDialog = Application.FileDialog(msoFileDialogFolderPicker)
xFileDialog.AllowMultiSelect = False
xFileDialog.Title = "Select a folder"
If xFileDialog.Show = -1 Then
    xStrPath = xFileDialog.SelectedItems(1)
End If
If xStrPath = "" Then Exit Sub
If Right(xStrPath, 1) <> "\" Then xStrPath = xStrPath & "\"
xFile = Dir(xStrPath & "*.log*")
If xFile = "" Then
    MsgBox "No files found"
    Exit Sub
End If
Do While xFile <> ""
    xFiles.Add xFile, xFile
    xFile = Dir()
Loop
Set xToBook = Workbooks.Open(path1 & "Master.xlsm")
If xFiles.Count > 0 Then
    For i = 1 To xFiles.Count
        'On Error Resume Next
        Set xWb = Workbooks.Open(xStrPath & xFiles.Item(i))
        xWb.Worksheets(1).copy After:=xToBook.Sheets(xToBook.Sheets.Count)
        On Error Resume Next
        ActiveSheet.Name = xWb.Name
        On Error GoTo 0
        xWb.Close False
    Next
End If

Application.ScreenUpdating = True
Application.EnableEvents = True
Application.DisplayAlerts = True

End Sub

【问题讨论】:

  • 能否请您发布您正在使用的代码,因为小sn-p 在这种情况下没有用。
  • @alowflyingpig:感谢您的及时回复。原帖已更新。

标签: excel vba


【解决方案1】:

代码打开每个文件并按原样复制数据。我试着逐行复制数据,它成功了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-11
    • 2022-10-15
    • 2019-03-25
    • 1970-01-01
    • 2014-06-12
    相关资源
    最近更新 更多