[英]How do I use a VLookup as range parameter in VBA?
我有一个IF语句工作正常,但我需要使用VLookup作为范围,因为作为IF主题的单元格可能在不同的行中,但从不是不同的列。
我是VBA的新手(但不是擅长),所以VLookup是一种本能的举动,但如果我能以另一种方式达到同样的目的,我就不会束缚它。 我一直在尝试在线寻找解决方案,但似乎并没有真正回答这个问题。
以下是具有静态范围的原始代码。
Sub FINDTOTAL()
Dim Amount As String
Amount = "Subtotal"
thetotal = Application.WorksheetFunction.Vlookup(Amount, Sheet1.Range("G:H"), 2, False)
End Sub
Sub CalculateSubtotal()
If Range("H25") > 10000 Then
Sheets("Billing").Select
Else
Worksheets("Sheet1").Range("H25").Copy
MsgBox "Subtotal has been copied and can be pasted into the quote"
End If
End Sub
VLookup工作,IF语句工作,我只是不能让它们一起工作。 我需要对IF语句进行小计评估,无论它在列中的位置(概率,它可能在第3 - 50行之间的任何位置)。
也许是这样的:
Sub FindTotal()
Dim Total As Variant
Total = Application.VLookup("Subtotal", Sheet1.Range("G:H"), 2, False)
If Not IsError(Total) Then
If IsNumeric(Total) Then
If Total > 10000 Then
' Do one thing
Else
' Do another thing
End If
End If
End If
End Sub
如果要将总计写入其他位置的其他单元格,则无需复制,只需直接传输值。
演示Find
/ Offset
方法:
Sub FindTotal()
Dim rng As Range
Set rng = Sheet1.Columns("G").Cells.Find(What:="Subtotal")
If Not rng Is Nothing Then
Dim totalRng As Range
Set totalRng = rng.Offset(, 1)
If IsNumeric(totalRng.Value) Then
If totalRng.Value > 10000 Then
' Do one thing
Else
' Do another thing
End If
End If
End If
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.