0
所以,我想要做的就是找到其中有“Severity”的列名,然后在该列中跳过1个单元格,并替换文本“高”为1,其他为2。编译错误指向.Range
的行,其中我设置Rng =偏移量变量。Complie错误:预期的函数或变量(使用.Range())
这里是我的VBA:
Sub Sev()
Dim ws As Worksheet
Dim aCell As Range, Rng As Range
Dim col As Long, lRow As Long
Dim colName As String
'~~> Change this to the relevant sheet
Set ws = ThisWorkbook.Sheets("Sheet1")
With ws
Set aCell = .Range("A1:N1").Find(What:="Severity", LookIn:=xlValues, LookAt:=xlWhole, _
MatchCase:=False)
'~~> If Found
If Not aCell Is Nothing Then
col = aCell.Column
colName = Split(.Cells(, col).Address, "$")(1)
lRow = .Range(colName & .Rows.Count).End(xlUp).Row
'~~> This is your range
lastCell = Range(col).End(xlDown).Select
Set Rng = .Range(aCell.Offset(1, 0), lastCell).Select
'Debug.Print Rng.Address
cell = aCell.Offset(1, 0)
For Each cell In Rng
If (InStr(aCell.Value, "high")) > 0 Then
aCell.Value = 1
Else
aCell.Value = 2
End If
Next cell
'~~> If not found
Else
MsgBox "Nov Not Found"
End If
End With
End Sub
我不再有编译错误。感谢你的回答。然而,for循环和偏移量没有完成我想要的。它只会改变第一个单元格,这是严重性。它没有别的。有任何想法吗? – BeastlyBernardo
我在@BeastlyBernardo中放了第二个版本的代码。我认为那是你想要的。尝试更多地使用调试器工具来隔离你的错误,并确保你了解设置整数和范围,或者两个单元格(i + 1,4)等的组合之间的差异。这就是你挣扎的地方。祝你好运。 – PGCodeRider
谢谢!我发现错误是什么 – BeastlyBernardo