![](/img/trans.png)
[英]VBA Excel. How to search for a value, get the first row/first column values and insert them in a different sheet if they don't yet exist
[英]VBA - Get row number of a value in a sheet, and then insert a different value into a different column of that same row
有沒有辦法在列表中搜索一個值,例如 A 列,識別該值的行號(例如,它可能是 A 列的第 40 行),go 到不同的列(例如第 40 行,B 列),然后通過宏將數據插入該列(因此它是自動完成的)。
我嘗試過使用下面的代碼,但似乎無處可去;
Dim Cell As Range
Dim RowNumber As Long
Columns("B:B").Select
Set cell = Selection.Find(What:="celda", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
RowNumber = Cell.Row
If cell = "celda" Then
'find row, go to Column B of that row, and insert "abc"
Else
'do it another thing
End If
我在下面的鏈接中找到了上面的代碼;
您幾乎不需要修改您的代碼。 試試下面...
Sub FindAndAdd()
Dim fCell As Range
Dim strSearch As String
strSearch = "Harun"
With ActiveSheet
Set fCell = .Columns("A:A").Find(What:=strSearch, _
After:=.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False)
'Lookat:=xlWhole to match whole word.
End With
If Not fCell Is Nothing Then
fCell.Offset(0, 1) = "New Value"
Else
MsgBox "No match found.", vbInformation, "Search Result"
End If
End Sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.