Notes
L’accès à cette page nécessite une autorisation. Vous pouvez essayer de vous connecter ou de modifier des répertoires.
L’accès à cette page nécessite une autorisation. Vous pouvez essayer de modifier des répertoires.
Cet exemple recherche dans les quatre premières colonnes toutes les cellules qui contiennent la constante « X », et masque la colonne contenant le X.
Exemple de code fourni par : Dennis Wallentin, VSTO & .NET & Excel
Sub Hide_Columns()
'Excel objects.
Dim m_wbBook As Workbook
Dim m_wsSheet As Worksheet
Dim m_rnCheck As Range
Dim m_rnFind As Range
Dim m_stAddress As String
'Initialize the Excel objects.
Set m_wbBook = ThisWorkbook
Set m_wsSheet = m_wbBook.Worksheets("Sheet1")
'Search the four columns for any constants.
Set m_rnCheck = m_wsSheet.Range("A1:D1").SpecialCells(xlCellTypeConstants)
'Retrieve all columns that contain an X. If there is at least one, begin the DO/WHILE loop.
With m_rnCheck
Set m_rnFind = .Find(What:="X")
If Not m_rnFind Is Nothing Then
m_stAddress = m_rnFind.Address
'Hide the column, and then find the next X.
Do
m_rnFind.EntireColumn.Hidden = True
Set m_rnFind = .FindNext(m_rnFind)
Loop While Not m_rnFind Is Nothing And m_rnFind.Address <> m_stAddress
End If
End With
End Sub
Cet exemple recherche dans les quatre premières colonnes toutes les cellules qui contiennent la constante « X », et affiche la colonne contenant le X.
Sub Unhide_Columns()
'Excel objects.
Dim m_wbBook As Workbook
Dim m_wsSheet As Worksheet
Dim m_rnCheck As Range
Dim m_rnFind As Range
Dim m_stAddress As String
'Initialize the Excel objects.
Set m_wbBook = ThisWorkbook
Set m_wsSheet = m_wbBook.Worksheets("Sheet1")
'Search the four columns for any constants.
Set m_rnCheck = m_wsSheet.Range("A1:D1").SpecialCells(xlCellTypeConstants)
'Retrieve all columns that contain X. If there is at least one, begin the DO/WHILE loop.
With m_rnCheck
Set m_rnFind = .Find(What:="X", LookIn:=xlFormulas)
If Not m_rnFind Is Nothing Then
m_stAddress = m_rnFind.Address
'Unhide the column, and then find the next X.
Do
m_rnFind.EntireColumn.Hidden = False
Set m_rnFind = .FindNext(m_rnFind)
Loop While Not m_rnFind Is Nothing And m_rnFind.Address <> m_stAddress
End If
End With
End Sub
À propos du collaborateur
Dennis Wallentin est l’auteur de VSTO & .NET & Excel, un blog consacré aux solutions .NET Framework pour Excel et Excel Services. Dennis développe des solutions Excel depuis plus de 20 ans et a également co-écrit « Professional Excel Development: The Definitive Guide to Developing Applications Using Microsoft Excel, VBA and .NET (2nd Edition) ».
Assistance et commentaires
Avez-vous des questions ou des commentaires sur Office VBA ou sur cette documentation ? Consultez la rubrique concernant l’assistance pour Office VBA et l’envoi de commentaires afin d’obtenir des instructions pour recevoir une assistance et envoyer vos commentaires.