Erreur de conformité: fonction ou variable prévue (en utilisant .Range ())

Donc, tout ce que j'essaye de faire, c'est find le nom de la colonne avec "Gravité", puis dans cette colonne, ignorer 1 cellule et replace le text "Haut" par 1 et les autres avec 2. L'erreur de compilation pointe à la ligne avec .Range où j'ai configuré Rng = la variable offset.

Voici ma VBA:

 Sub Sev() Dim ws As Worksheet Dim aCell As Range, Rng As Range Dim col As Long, lRow As Long Dim colName As Ssortingng '~~> 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 

Votre code après "c'est votre gamme" n'est pas défini correctement. J'ai réécrit quelques lignes dans votre code avec mes modifications marquées PGCodeRider en ligne. Je pense que cela correspond à ce que vous voulez.

  Sub Sev() Dim ws As Worksheet Dim aCell As Range, Rng As Range Dim col As Long, lRow As Long Dim colName As Ssortingng '~~> 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 

Version 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