[英]find location of a cell with an exact match in excel vba
我通過努力環路range a
的sheet a
,並搜索中的每個值range b
在sheet b
,然后添加一個列sheet b
,如果有一個匹配。
這是我得到的:
Function add_column_binary(sheet_name_from As String, col_from As Integer, sheet_to As String, col_to As Integer)
' set range - the range to be looped through to find key for serachign the second range
Dim first_range As Range
Set first_range = set_range(sheet_name_from, col_from)
' set ragen - the range in teh second sheet to be repeatedly searched
Dim second_range As Range
Set second_range = set_range(sheet_to, col_to)
' find last column
Dim last_col As Integer
last_col = Worksheets(sheet_to).Cells(1, Columns.Count).End(xlToLeft).column
' label last column
Worksheets(sheet_to).Cells(1, last_col + 1).Value = "Invited = 1"
Dim rows1 As Long
rows1 = first_range.Cells(rows.Count, col_from).End(xlUp).Row + 1
Dim n As Long
Dim constructed_id As String
Dim find_result As Range
For n = 2 To rows1
constructed_id = "ObjectID(" & first_range.Cells(n, 1) & ")" ' format object id
' **** I keep getting "run-time error '1004':" ****
' **** "Application-defined or object-defined error" ****
With Worksheets(sheet_to).Range(second_range)
Set find_result = .Find(constructed_id, LookIn:=xlValues, lookat:=xlWhole)
End With
Next n
Stop
End Function
Sub test_stuff()
Dim x As Range
Set x = add_column_binary("invitesOutput.csv", 3, "usersFullOutput.csv", 1)
End Sub
在For
循環中使用With
是否有問題?
更改以下代碼:
With Worksheets(sheet_to).Range(second_range)
Set find_result = .Find(constructed_id, LookIn:=xlValues, lookat:=xlWhole)
End With
對此:
Set find_result = second_range.Find(constructed_id, LookIn:=xlValues, lookat:=xlWhole)
原因: second_range
已被定義為范圍,您可以將其用於Find()
方法。
范圍內 Worksheets(sheet_to).Range(second_range)
期待一個字符串,表示的范圍不Range對象 。 因此出現錯誤。
要克服什么都不重要的情況,請使用IF語句:
If Not find_result Is Nothing then 'Not Is Nothing, in other words is something
'Your code
End if
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.