如何恢复 VBA 中列的索引
How to recover index of column in VBA
Sub Bouton7()
Dim Derniere_ligne As Long
Dim ligne_en_cours As Long
Dim libelle As String
Derniere_ligne = Cells(Rows.Count, 2).End(xlUp).Row
For ligne_en_cours = 2 To Derniere_ligne
libelle = Cells(ligne_en_cours, 5).Value
If libelle = "XY" Then
Rows(ligne_en_cours).Interior.ColorIndex = 4 'this will change colour for whole row
End If
Next
End Sub
这里我在libelle = "XY
时给线条上色
但是我想替换 libelle = Cells(ligne_en_cours, 5).Value
每 libelle = Cells(ligne_en_cours, index (CODE_LIBELLE) ).Value
因为在我的table中,CODE_LIBELLE
是第5列,所以我想恢复列名的索引
谢谢
ps :
我这样做是为了恢复代码 libelle 的索引列:
Derniere_ligne = Cells(Rows.Count, 3).End(xlUp).Row
Set ws = Sheets("Feuil4")
For c = 1 To 6
rec = ws.Cells(1, c).Value
If rec = "CODE_LIBELLE" Then
rec2 = c
End If
Next c
但我的 c 始终是循环的结尾而不是 rec2
的值
像这样的东西应该可以工作:
Sub Bouton7()
Dim Derniere_ligne As Long
Dim ligne_en_cours As Long
Dim libelle As String, m, ws As Worksheet
Set ws = ActiveSheet ' or whatever
'locate the header position in Row1
m = Application.Match("CODE_LIBELLE", ws.Rows(1), 0)
If IsError(m) Then 'm will be an error value if no match
MsgBox "Required header not found"
Exit Sub
End If
For ligne_en_cours = 2 To Cells(Rows.Count, 2).End(xlUp).Row
If ws.Cells(ligne_en_cours, m).Value = "XY" Then
ws.Rows(ligne_en_cours).Interior.ColorIndex = 4
End If
Next
End Sub
编辑:您发布的循环很好,但是您将使用 rec2
作为列位置而不是 c
:
Set ws = Sheets("Feuil4")
For c = 1 To 6
rec = ws.Cells(1, c).Value
If rec = "CODE_LIBELLE" Then
rec2 = c
Exit For 'no need to check further so jump out of the loop...
End If
Next c
Sub Bouton7()
Dim Derniere_ligne As Long
Dim ligne_en_cours As Long
Dim libelle As String
Derniere_ligne = Cells(Rows.Count, 2).End(xlUp).Row
For ligne_en_cours = 2 To Derniere_ligne
libelle = Cells(ligne_en_cours, 5).Value
If libelle = "XY" Then
Rows(ligne_en_cours).Interior.ColorIndex = 4 'this will change colour for whole row
End If
Next
End Sub
这里我在libelle = "XY
但是我想替换 libelle = Cells(ligne_en_cours, 5).Value
每 libelle = Cells(ligne_en_cours, index (CODE_LIBELLE) ).Value
因为在我的table中,CODE_LIBELLE
是第5列,所以我想恢复列名的索引
谢谢
ps :
我这样做是为了恢复代码 libelle 的索引列:
Derniere_ligne = Cells(Rows.Count, 3).End(xlUp).Row
Set ws = Sheets("Feuil4")
For c = 1 To 6
rec = ws.Cells(1, c).Value
If rec = "CODE_LIBELLE" Then
rec2 = c
End If
Next c
但我的 c 始终是循环的结尾而不是 rec2
的值像这样的东西应该可以工作:
Sub Bouton7()
Dim Derniere_ligne As Long
Dim ligne_en_cours As Long
Dim libelle As String, m, ws As Worksheet
Set ws = ActiveSheet ' or whatever
'locate the header position in Row1
m = Application.Match("CODE_LIBELLE", ws.Rows(1), 0)
If IsError(m) Then 'm will be an error value if no match
MsgBox "Required header not found"
Exit Sub
End If
For ligne_en_cours = 2 To Cells(Rows.Count, 2).End(xlUp).Row
If ws.Cells(ligne_en_cours, m).Value = "XY" Then
ws.Rows(ligne_en_cours).Interior.ColorIndex = 4
End If
Next
End Sub
编辑:您发布的循环很好,但是您将使用 rec2
作为列位置而不是 c
:
Set ws = Sheets("Feuil4")
For c = 1 To 6
rec = ws.Cells(1, c).Value
If rec = "CODE_LIBELLE" Then
rec2 = c
Exit For 'no need to check further so jump out of the loop...
End If
Next c