2017-07-14 84 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

相關問題