一步一步地运行这段代码
F8
,在新的Excel文件上:
Option Explicit
Public Sub TestMe()
Dim ws As Worksheet: Set ws = Worksheets(1)
Dim harga As Range: Set harga = ws.Range("A1:C10")
Dim comboBox3 As String
Dim koin As Range: Set koin = ws.Range("D1:D10")
comboBox3 = "Something"
Range("D3") = "Something"
Range("A3") = "Something"
With WorksheetFunction
Debug.Print .Match(comboBox3, koin, 0)
ws.Range("D5") = .Index(harga, .Match(comboBox3, koin, 0), 1)
End With
End Sub
它显示了
Match()
和
Index()
使用。在所讨论的代码中,可能没有
Match
从而导致错误。如果您希望返回错误,而不是“抛出”,则尝试
Application.Match()
和
Application.Index()
:
Public Sub TestMe()
Dim ws As Worksheet: Set ws = Worksheets(1)
Dim harga As Range: Set harga = ws.Range("A1:C10")
Dim comboBox3 As String
Dim koin As Range: Set koin = ws.Range("D1:D10")
' comboBox3 = "Something"
' Range("D3") = "Something"
' Range("A3") = "Something"
With Application
Debug.Print .Match(comboBox3, koin, 0)
ws.Range("D5") = .Index(harga, .Match(comboBox3, koin, 0), 1)
End With
End Sub