修复了查询已存在的消息,然后获取应用程序定义或对象定义的错误消息

时间:2019-04-08 15:26:19

标签: excel vba

使用宏记录器,我将文本记录到文本文件中的列中。当我第二次运行它时,出现错误“运行时错误'-2147024809(80070057'):名称为'wkErrFile'的查询已存在。我已解决该问题,并得到了我第一次运行时未曾遇到的另一个错误

所以我添加了一条删除语句ActiveWorkbook.Queries(“ wkErrFile”)。在开始时删除,以便代码可以继续通过ActiveWorkbook.Queries.Add Name:=“ wkErrFile”,Formula:= ....停止运行在遍历所有代码之前,在结束之前添加一条delete语句之后,注释掉该第一个delete语句用于后续运行。第一个错误消失了,但是现在我遇到了第二个错误。

Sub FixHighlighterMacro()
'
' FixHighlighterMacro Macro
'

  'ActiveWorkbook.Queries("wkErrFile").Delete
    ActiveWorkbook.Queries.Add Name:="wkErrFile", Formula:= _
        "let" & Chr(13) & "" & Chr(10) & "    Source = Table.FromColumns({Lines.FromBinary(File.Contents(""U:\Backup\Development\Macros\VBA\FixingH\wkErrFile.txt""), null, null, 1252)})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & "    Source"
    ActiveWorkbook.Worksheets.Add
    With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
        "OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=wkErrFile;Extended Properties=""""" _
        , Destination:=Range("$A$1")).QueryTable
        .CommandType = xlCmdSql
        .CommandText = Array("SELECT * FROM [wkErrFile]")
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .PreserveColumnInfo = True
        .ListObject.DisplayName = "wkErrFile"
        .Refresh BackgroundQuery:=False
        ActiveWorkbook.Queries("wkErrFile").Delete
    End With
    Selection.TextToColumns Destination:=Range("A1"), DataType:=xlFixedWidth, _
        FieldInfo:=Array(Array(0, 2), Array(2, 2), Array(5, 2), Array(10, 1), Array(36, 2), _
        Array(37, 2), Array(38, 2), Array(39, 2), Array(40, 2), Array(41, 2), Array(42, 2), Array( _
        74, 2), Array(76, 1), Array(81, 2), Array(83, 2), Array(87, 1), Array(100, 2), Array(105, 1) _
        , Array(111, 2), Array(117, 1), Array(127, 2), Array(133, 1), Array(147, 2), Array(177, 2), _
        Array(189, 2), Array(199, 2)), TrailingMinusNumbers:=True
    ActiveWindow.ScrollColumn = 2
    ActiveWindow.ScrollColumn = 3
    ActiveWindow.ScrollColumn = 4
    ActiveWindow.ScrollColumn = 5
    ActiveWindow.ScrollColumn = 6
    ActiveWindow.ScrollColumn = 7
    ActiveWindow.ScrollColumn = 8
    ActiveWindow.ScrollColumn = 9
    ActiveWindow.ScrollColumn = 10
End Sub

我希望在每次运行期间添加新的工作表后,将代码放置在单独的列中。自第一次发生以来,错误不允许后续运行。
1.请告诉我如何编码这两个错误? 2.请在以下位置解释有关wkErrFile的情况:
   (a).CommandText = Array(“ SELECT * FROM [wkErrFile]”)和    (b).ListObject.DisplayName =“ wkErrFile” 非常感谢! 谢谢!

0 个答案:

没有答案