【问题标题】:Relative reference for Macros宏的相对参考
【发布时间】:2013-08-30 14:29:04
【问题描述】:

我是宏的新手,并且是录制它们的新手。我创建了一个带有参数的网络查询,它根据我选择的单元格编辑查询的一部分。我现在必须运行这个网络查询 800 多次。所以我记录了自己在选中“使用相对引用”的情况下这样做。但它总是将网络查询放在我记录宏的同一单元格中,而不是在我为网络查询选择的单元格旁边的单元格中。

例如:我根据 A1 中的引用在 A2 中运行了一个查询。所以,我希望宏通过使用来自 B1 的信息来运行查询并将其放入 B2,但它总是将其放入 A2。

代码!

With ActiveSheet.QueryTables.Add(Connection:= _
"FINDER;C:\Users\alillien.ASSOCIATED_NT\AppData\Roaming\Microsoft \Queries\990Finder.iqy" _
    , Destination:=Range("$C$576"))
    .Name = "990 Finder_284"
    .FieldNames = True
    .RowNumbers = False
    .FillAdjacentFormulas = False
    .PreserveFormatting = False
    .RefreshOnFileOpen = False
    .BackgroundQuery = True
    .RefreshStyle = xlInsertDeleteCells
    .SavePassword = False
    .SaveData = True
    .AdjustColumnWidth = True
    .RefreshPeriod = 0
    .WebSelectionType = xlSpecifiedTables
    .WebFormatting = xlWebFormattingAll
    .WebTables = """MainContent_GridView1"""
    .WebPreFormattedTextToColumns = True
    .WebConsecutiveDelimitersAsOne = True
    .WebSingleBlockTextImport = False
    .WebDisableDateRecognition = False
    .WebDisableRedirections = False
    .Refresh BackgroundQuery:=False
End With
ActiveCell.Offset(2, 0).Range("A1").Select
End Sub

我知道这是因为 "Destination:=Range("$C$576")" 行,但我不知道如何将其编辑为相对于我的起点/我单击可调整查询的位置.

非常感谢!

【问题讨论】:

    标签: excel vba excel-web-query


    【解决方案1】:

    好的,你可以试试这个。

    替换:

    范围("$C$576")

    与:

    ActiveCell.Offset(1,0)

    这将有效地使当前引用为 $C$576 的范围更改为工作表中的活动单元格,但向下移动一个单元格(我认为这是您想要的)。如果您想要一个单元格正确,请将其更改为 (0,1)

    【讨论】:

      【解决方案2】:

      我相信在您想使用它的上下文中不允许使用 RC 表示法。我试过了,但没有用,但您可以以相同的方式使用 cells 对象。

      创建一些变量来保存您所在的当前行和列。

      您可以将其应用于代码的“Destination:=Range(Cells(curRow + 1, curCol).Address)”部分:

      Dim curRow As Integer
      Dim curCol As Integer
      curRow = ActiveCell.Row
      curCol = ActiveCell.Column
          With ActiveSheet.QueryTables.Add(Connection:= _
      "FINDER;C:\Users\alillien.ASSOCIATED_NT\AppData\Roaming\Microsoft \Queries\990Finder.iqy" _
          , Destination:=Range(Cells(curRow + 1, curCol).Address))
          .Name = "990 Finder_284"
          .FieldNames = True
          .RowNumbers = False
          .FillAdjacentFormulas = False
          .PreserveFormatting = False
          .RefreshOnFileOpen = False
          .BackgroundQuery = True
          .RefreshStyle = xlInsertDeleteCells
          .SavePassword = False
          .SaveData = True
          .AdjustColumnWidth = True
          .RefreshPeriod = 0
          .WebSelectionType = xlSpecifiedTables
          .WebFormatting = xlWebFormattingAll
          .WebTables = """MainContent_GridView1"""
          .WebPreFormattedTextToColumns = True
          .WebConsecutiveDelimitersAsOne = True
          .WebSingleBlockTextImport = False
          .WebDisableDateRecognition = False
          .WebDisableRedirections = False
          .Refresh BackgroundQuery:=False
      End With
      

      以下是 RC 和 A1 寻址工作原理的备忘单:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-11-23
        • 2015-05-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多