为什么这个带有定义变量的Vookup宏不工作

发布于 2025-01-29 01:35:16 字数 1670 浏览 2 评论 0原文

我记录了以下宏:

Sub Macro2()
'
' Macro1 Macro
'

'
    Range("D108").Select
    ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-1],R[-12]C[-1]:R[-4]C[1],2,FALSE)"
    Range("E108").Select
    ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-2],R[-12]C[-2]:R[-4]C,3,FALSE)"
    Range("D108").Select
    Selection.AutoFill Destination:=Range("D108:D110"), Type:=xlFillDefault
    Range("D108:D110").Select
    Range("E108").Select
    Selection.AutoFill Destination:=Range("E108:E110"), Type:=xlFillDefault
    Range("E108:E110").Select
End Sub

然后,我尝试修改,因此我输入了两个单个数字:



Sub Macro1()
'
' Macro1 Macro
'

Dim row2start As Integer
row2start = InputBox("Write row to start this agenda", " ", "Enter your input text HERE")

Dim des2 As String
des2 = "D" & row2start + 1

Dim des3 As String
des3 = "E" & row2start + 1

Dim newActions As Integer
newActions = InputBox("count how many pending actions", " ", "Enter your input text HERE")
newActions2 = (row2start + 1) + newActions

Dim des4 As String
des4 = des2 & ":D" & newActions2

Dim des5 As String
des5 = des3 & ":E" & newActions2

    Range(des2).Select
    ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-1],R[-12]C[-1]:R[-4]C[1],2,FALSE)"
    Range(des3).Select
    ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-2],R[-12]C[-2]:R[-4]C,3,FALSE)"
    Range(des2).Select
    Selection.AutoFill Destination:=Range(des4), Type:=xlFillDefault
    Range(des4).Select
    Range(des3).Select
    Selection.AutoFill Destination:=Range(des5), Type:=xlFillDefault
    Range(des5).Select
End Sub

我的问题是我运行它,什么也没有发生。我不知道为什么?有什么想法吗? 从技术上讲,它是相同的记录宏,我只是用变量对其进行了更新。我已经仔细检查了变量,但找不到任何错误。 多谢

I recorded the below macro:

Sub Macro2()
'
' Macro1 Macro
'

'
    Range("D108").Select
    ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-1],R[-12]C[-1]:R[-4]C[1],2,FALSE)"
    Range("E108").Select
    ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-2],R[-12]C[-2]:R[-4]C,3,FALSE)"
    Range("D108").Select
    Selection.AutoFill Destination:=Range("D108:D110"), Type:=xlFillDefault
    Range("D108:D110").Select
    Range("E108").Select
    Selection.AutoFill Destination:=Range("E108:E110"), Type:=xlFillDefault
    Range("E108:E110").Select
End Sub

Then, I tried to modify, so I enter two single numbers as below:



Sub Macro1()
'
' Macro1 Macro
'

Dim row2start As Integer
row2start = InputBox("Write row to start this agenda", " ", "Enter your input text HERE")

Dim des2 As String
des2 = "D" & row2start + 1

Dim des3 As String
des3 = "E" & row2start + 1

Dim newActions As Integer
newActions = InputBox("count how many pending actions", " ", "Enter your input text HERE")
newActions2 = (row2start + 1) + newActions

Dim des4 As String
des4 = des2 & ":D" & newActions2

Dim des5 As String
des5 = des3 & ":E" & newActions2

    Range(des2).Select
    ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-1],R[-12]C[-1]:R[-4]C[1],2,FALSE)"
    Range(des3).Select
    ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-2],R[-12]C[-2]:R[-4]C,3,FALSE)"
    Range(des2).Select
    Selection.AutoFill Destination:=Range(des4), Type:=xlFillDefault
    Range(des4).Select
    Range(des3).Select
    Selection.AutoFill Destination:=Range(des5), Type:=xlFillDefault
    Range(des5).Select
End Sub

My problem is that I run it and nothing happens. I don't know why? Any ideas why?
Technically, it is the same recorded macro, I just updated it with variables. I have double checked the variables but I cannot find any errors.
Thanks a lot

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

红颜悴 2025-02-05 01:35:16

我相信您的宏观问题是您的公式都使用了相对地址,因此随着公式的复制,预期的查找范围不会被“查找”:

Sub Macro1()
' Macro1 Macro Modified
Dim row2start As Long
row2start = InputBox("Write row to start this agenda", " ", "Enter your input text HERE")

Dim des2 As String
des2 = "D" & row2start + 1

Dim des3 As String
des3 = "E" & row2start + 1

Dim newActions As Long, newActions2 As Long
newActions = InputBox("count how many pending actions", " ", "Enter your input text HERE")
newActions2 = (row2start + 1) + newActions

Dim des4 As String
des4 = des2 & ":D" & newActions2

Dim des5 As String
des5 = des3 & ":E" & newActions2

Range(des2, des4).FormulaR1C1 = "=VLOOKUP(RC[-1],R96C3:R104C5,2,FALSE)"
Range(des3, des5).FormulaR1C1 = "=VLOOKUP(RC[-2],R96C3:R104C5,3,FALSE)"
    
End Sub

呈现的代码上面的查找范围内使用了绝对地址,并且还避免使用autofill,因为在这种情况下不需要(在此上下文中都可以很容易地写入相同的公式为垂直范围的单元格,就像单个单元格)。

I believe the issue with your macro is that your formulae were all using relative addresses, such that the intended look-up range was not being 'looked-up' as the formula was copied down:

Sub Macro1()
' Macro1 Macro Modified
Dim row2start As Long
row2start = InputBox("Write row to start this agenda", " ", "Enter your input text HERE")

Dim des2 As String
des2 = "D" & row2start + 1

Dim des3 As String
des3 = "E" & row2start + 1

Dim newActions As Long, newActions2 As Long
newActions = InputBox("count how many pending actions", " ", "Enter your input text HERE")
newActions2 = (row2start + 1) + newActions

Dim des4 As String
des4 = des2 & ":D" & newActions2

Dim des5 As String
des5 = des3 & ":E" & newActions2

Range(des2, des4).FormulaR1C1 = "=VLOOKUP(RC[-1],R96C3:R104C5,2,FALSE)"
Range(des3, des5).FormulaR1C1 = "=VLOOKUP(RC[-2],R96C3:R104C5,3,FALSE)"
    
End Sub

The code presented above uses absolute addressing for the look-up range, and also avoids using AutoFill, as it is not necessary in this context (where the same formula can be written just as easily to a vertical range of cells, as it can be to a single cell).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文