所以,我所要做的就是找到列名称为" Severity"在其中,然后在该列中,跳过1个单元格,并替换文本"高"带有1,其他带有2.编译错误指向.Range
的行,其中我设置Rng =偏移量变量。
这是我的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
答案 0 :(得分:0)
“这是您的范围”之后的代码未正确定义。我在代码中重写了几行,我的编辑标记为PGCodeRider
。我认为这会伴随你想要的东西。
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 'pgCoderRider excluded
Dim lastcell As Range: Set lastcell = .Cells(1, col) 'PGCODRIDER MODIFIED
Set Rng = .Range(aCell.Offset(1, 0), lastcell) 'PGCODRIDER MODIFIED
Rng.Select 'PGCODRIDER MODIFIED
'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
第2版:
Sub Sev2()
Dim ws As Worksheet
Dim aCell As Range, Rng As Range
Dim col As Long, lRow As Long
Dim ColNumber As Integer
'~~> 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) 'PGCodeRider not needed
'lRow = .Range(colName & .Rows.Count).End(xlUp).Row 'PGCOdeRider not needed
'~~> This is your range
'lastCell = .Range(col).End(xlDown).Select 'pgCoderRider excluded
Dim lastcell As Range: Set lastcell = .Cells(Rows.Count, col).End(xlUp) 'PGCODRIDER MODIFIED
Set Rng = .Range(aCell.Offset(1, 0), lastcell) 'PGCODRIDER MODIFIED
'Rng.Select 'PGCODRIDER excludeded
'Debug.Print Rng.Address
'cell = aCell.Offset(1, 0) 'PGCODRIDER excludeded
For Each cell In Rng.Cells
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