如何为Windows编写动态宏?

时间:2019-01-04 11:44:56

标签: windows automation macros

我正在尝试编写自己的Windows宏,但在线上的许多内容似乎都偏向VBA和Excel,或者更静态的“键入并完成”或“先执行,然后再执行”宏。我正在寻找一种编写更多程序宏的方法,以自动化包含动态输入的繁琐任务。

我已经读过this article on arrays in VBA for Excel which ranges as Dynamic input,但这不是我要的。

我所追求的是一种记录宏的方法,但是要在宏中拆开一部分(即查看为执行该宏而编写的代码),并在其中插入一些微小的动作,例如从宏中拉出。一个要使用的数组而不是使用执行的按键,并不断迭代直到该数组为空。

示例(与项目无关)

  1. 我打开一个新标签页(ctrl + t)
  2. 我输入了一个随机网址(www.example.com)
  3. 我随机点击页面的一部分

我想在第2阶段拆开该宏以多次插入一个不同的地址,因此我将打开与要完成的数组一样多的选项卡。

1 个答案:

答案 0 :(得分:1)

假设您记录了以下宏以提取公司数据。


    Sub Macro3()
    '
    ' Macro3 Macro
    '

    '
        Range("A5").Select
        With ActiveSheet.QueryTables.Add(Connection:= _
            "URL;https://www.google.com/search?q=NYSE:GE&tbm=fin&gws_rd=ssl#gws_rd=ssl&scso=_EzKKXMOFH8jKswXW2pigDQ2:0&spf=1552560659066&wptab=COMPANY" _
            , Destination:=Range("$A$5"))
            .CommandType = 0
            .Name = "0&spf=1552560659066&wptab=COMPANY_1"
            .FieldNames = True
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .PreserveFormatting = True
            .RefreshOnFileOpen = False
            .BackgroundQuery = True
            .RefreshStyle = xlInsertDeleteCells
            .SavePassword = False
            .SaveData = True
            .AdjustColumnWidth = True
            .RefreshPeriod = 0
            .WebSelectionType = xlSpecifiedTables
            .WebFormatting = xlWebFormattingNone
            .WebTables = "4"
            .WebPreFormattedTextToColumns = True
            .WebConsecutiveDelimitersAsOne = True
            .WebSingleBlockTextImport = False
            .WebDisableDateRecognition = False
            .WebDisableRedirections = False
            .Refresh BackgroundQuery:=False
        End With
    End Sub

这是将其放入循环中的方式。 CommandType引发错误,不需要,因此必须将其删除。


    Sub GetCompanyData()
    Dim companies(1 To 3) As String

    i = 1
    companies(i) = "F": i = i + 1
    companies(i) = "CAT": i = i + 1
    companies(i) = "GE": i = i + 1

    For i = 1 To UBound(companies)

        LastUSedRow = ActiveSheet.UsedRange.Rows.Count
        NextInsertAddr = Cells(LastUSedRow + 1, 1).Address
        Range(NextInsertAddr) = "Company: " + companies(i)
        NextInsertAddr = Cells(LastUSedRow + 2, 1).Address
        URLToPull = "URL;https://www.google.com/search?q=NYSE:" + companies(i) + "&tbm=fin&gws_rd=ssl#gws_rd=ssl&scso=_EzKKXMOFH8jKswXW2pigDQ2:0&spf=1552560659066&wptab=COMPANY"
    '
        With ActiveSheet.QueryTables.Add(Connection:=URLToPull _
            , Destination:=Range(NextInsertAddr))
      '      .CommandType = 0
            .Name = "0&spf=1552560659066&wptab=COMPANY"
            .FieldNames = True
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .PreserveFormatting = True
            .RefreshOnFileOpen = False
            .BackgroundQuery = True
            .RefreshStyle = xlInsertDeleteCells
            .SavePassword = False
            .SaveData = True
            .AdjustColumnWidth = True
            .RefreshPeriod = 0
            .WebSelectionType = xlSpecifiedTables
            .WebFormatting = xlWebFormattingNone
            .WebTables = "4"
            .WebPreFormattedTextToColumns = True
            .WebConsecutiveDelimitersAsOne = True
            .WebSingleBlockTextImport = False
            .WebDisableDateRecognition = False
            .WebDisableRedirections = False
            .Refresh BackgroundQuery:=False
        End With
    Next i


    End Sub