我尝试找到我的光标在另一个工作表上使用application.match函数的活动单元格的值。如果发现我想根据ActiveCell.Row和确定的列更改活动工作表上单元格的值。
我尝试使用此代码
Sub test()
Dim wert As String
Dim such1 As String
Dim var As Integer
such1 = ActiveCell.Value
On Error Resume Next
var = Application.Match(such1, Worksheets(Test1).Columns(1), 0)
If Err = 0 Then
wert = Sheets("Test2").Cell(var, "N").Value
Sheets("Test2").Cell(ActiveCell.Row, "O").Value = wert
Else
MsgBox "Value not existent"
End If
End Sub
不知怎的,我总是得到错误信息。我不明白为什么。你有什么想法吗?
答案 0 :(得分:0)
使用这样的语法:
Option Explicit
Public Sub test()
Dim foundRow As Variant
Dim activeRow As Long
Dim foundN As String
activeRow = ActiveCell.Row
foundRow = Application.Match(ActiveCell, Worksheets("Test1").Columns(1), 0)
If Not IsError(foundRow) Then
foundN = Worksheets("Test2").Cells(foundRow, "N").Value
Worksheets("Test2").Cells(activeRow, "O").Value = foundN
Else
MsgBox "Value not existent"
End If
End Sub
代码中的错误:
Worksheets(test1)
应为Worksheets("Test1")
Worksheets("Test2").Cell
应为Worksheets("Test2").Cells
(单元格末尾的“s”) Application.Match()
与 WorksheetFunction.Match()
WorksheetFunction.Match()
不如Application.Match()
WorksheetFunction.Match()
抛出运行时错误
On Error Resume Next
来绕过VBA错误 Application.Match()
返回错误对象
On Error Resume Next
不起作用If IsError(Application.Match(...))
Dim foundRow As
Variant