2017-07-14 2 views
0

私は、「重大度」の列名を見つけて、その列内で1セルをスキップして置き換えるテキスト "高"は1、その他は2です。コンパイルエラーは、.Rangeの行を指しています。ここで、Rng =オフセット変数を設定します。正しく定義されていません「これはあなたの範囲である」後準拠エラー:期待される関数または変数(.Range()を使用)

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

あなたのコード:

は、ここに私のVBAです。 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に2番目のコードを入れました。私はそれがあなたが望むことをすると思います。デバッガツールを使用してエラーを特定し、整数と範囲の設定の違い、つまり2つのセル(i + 1,4)などの組み合わせの違いを理解してください。それが苦労していた場所です。がんばろう。 – PGCodeRider

+0

ありがとう!私はエラーが何かを発見した – BeastlyBernardo

関連する問題