2017-07-14 59 views
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 

回答

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 
+0

我不再有编译错误。感谢你的回答。然而,for循环和偏移量没有完成我想要的。它只会改变第一个单元格,这是严重性。它没有别的。有任何想法吗? – BeastlyBernardo

+0

我在@BeastlyBernardo中放了第二个版本的代码。我认为那是你想要的。尝试更多地使用调试器工具来隔离你的错误,并确保你了解设置整数和范围,或者两个单元格(i + 1,4)等的组合之间的差异。这就是你挣扎的地方。祝你好运。 – PGCodeRider

+0

谢谢!我发现错误是什么 – BeastlyBernardo

相关问题