[英]How to search specific column within range in VBA
您好,感謝您抽出寶貴的時間閱讀本文,並感謝您的任何建議!
我正在使用VBA搜索數組中的第一個值,該值大於單元格中其他位置指定的值,並將列和行號輸出到新單元格。
我使用的代碼是:
Sub A()
Dim rng As Range
Dim v As Variant
Dim colnum As Long
Dim row As Long
Set rng = Range("B2:BT72")
v = Sheets("WIP").Range("AC76").Value
For Each cll In rng
If cll.Value > v Then
colnum = cll.Column
row = cll.row
Exit For
End If
Next
Sheets("WIP").Range("AF76").Value = colnum + 25
Sheets("WIP").Range("AG76").Value = row + 25
End Sub
而且效果很好。 但是,我現在只需要搜索特定的列並僅輸出行值即可。 我需要搜索的列也在特定的單元格中指定。 我已將代碼修改為:
Sub B()
Dim rng As Range
Dim v As Variant
Dim x As Variant
Dim row As Long
Set rng = Range("A2:BT72")
v = Sheets("WIP").Range("AC77").Value
x = Sheets("WIP").Range("AF77").Value
For Each cell In rng.Columns(x - 25)
If cell.Value > v Then
row = cell.row
Exit For
End If
Next
Sheets("WIP").Range("AG77").Value = row + 25
End Sub
這次,我在“如果cell.value> V然后”這一行上收到一條錯誤消息類型不匹配的消息,有人可以找到原因嗎?
謝謝
在第二個示例中,需要為范圍對象指定'.Cells'屬性:
Sub B()
Dim rng As Range
Dim v As Variant
Dim x As Variant
Dim row As Long
Set rng = Range("A2:BT72")
v = Sheets("WIP").Range("AC77").Value
x = Sheets("WIP").Range("AF77").Value
For Each cell In rng.Columns(x - 25).Cells
If cell.Value > v Then
row = cell.row
Exit For
End If
Next
Sheets("WIP").Range("AG77").Value = row + 25
End Sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.