IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Macros et VBA Excel Discussion :

Dernière ligne utilisée [XL-2010]


Sujet :

Macros et VBA Excel

  1. #1
    Membre régulier Avatar de horemheb
    Homme Profil pro
    Enseignant Chercheur
    Inscrit en
    Novembre 2010
    Messages
    122
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 68
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Enseignant Chercheur
    Secteur : Enseignement

    Informations forums :
    Inscription : Novembre 2010
    Messages : 122
    Points : 77
    Points
    77
    Par défaut Dernière ligne utilisée
    Bonjour, désolé pour l'intitulé de ma question, je ne sais pas comment la résumer. Je détaille :

    Quand on lance une impression sur Excel il détermine automatiquement le nombre de pages qu'il doit imprimer sachant où est l'élément le plus "lointain". Quelle est la fonction VBA qui permettrait de connaître sa position ?

    Par exemple vous avez des valeurs dans la colonne 1, mais pas dans toutes les cellules, vous ne savez pas combien, vous ne savez pas où. Comment savoir à partir de quelle ligne il n'y a plus de valeur au-delà ? Le but est de regrouper toutes les valeurs en haut de la colonne.

    Merci
    Mille jours d’entraînement pour forger, dix milles jours d’entraînement pour polir.
    Shimen Musashi - Gorin-no-sho Rouleau de l’eau

  2. #2
    Membre émérite
    Homme Profil pro
    Chef de projet en SSII
    Inscrit en
    Novembre 2011
    Messages
    1 503
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Irlande

    Informations professionnelles :
    Activité : Chef de projet en SSII

    Informations forums :
    Inscription : Novembre 2011
    Messages : 1 503
    Points : 2 657
    Points
    2 657
    Par défaut
    Bonjour horemheb,

    Voici la propriété te permettant de connaitre la dernière ligne utilisée d'une feuille.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    Sub test()
     
    With Worksheets("Feuil1").UsedRange
        MsgBox .Cells(.Cells.Count).Row
    End With
     
    End Sub
    Cordialement,
    Kimy
    La logique :
    • Plus ya de gruyère, moins ya de gruyère.
    • Plus tu pédales moins vite, moins tu avances plus vite.
    Plusoyer les réponses pertinentes et n'oublier pas de résolver en fin de post !

  3. #3
    Membre expert
    Homme Profil pro
    Retraité
    Inscrit en
    Avril 2011
    Messages
    1 858
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Retraité

    Informations forums :
    Inscription : Avril 2011
    Messages : 1 858
    Points : 3 974
    Points
    3 974
    Par défaut
    Bonjour,

    Ou encore
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    Worksheets("Feuil1").UsedRange.SpecialCells(xlCellTypeLastCell).row
    Cordialement.

  4. #4
    Membre émérite
    Homme Profil pro
    Chef de projet en SSII
    Inscrit en
    Novembre 2011
    Messages
    1 503
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Irlande

    Informations professionnelles :
    Activité : Chef de projet en SSII

    Informations forums :
    Inscription : Novembre 2011
    Messages : 1 503
    Points : 2 657
    Points
    2 657
    Par défaut
    Re,

    Petite macro expérimentale permettant de remonter toutes les valeurs de chaque colonne d'une feuille sur la plage d'origine (afin qu'il n'y ai plus de "trous" = cellules vides) :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    Option Explicit
     
    Sub Regroup()
    Dim oRng As Range
    Dim oCol As Range
    Dim oCell As Range
    Dim i As Integer
     
    With Worksheets("Feuil1").UsedRange
        For Each oCol In .Columns
            Set oRng = FindAll(oCol, "*")
            If Not oRng Is Nothing Then
                oRng.Select
                i = 1
                For Each oCell In oRng
                    oCol.Cells(i) = oCell
                    i = i + 1
                Next oCell
                If i <= .Rows.Count Then
                    Range(oCol.Cells(i), oCol.Cells(oCol.Cells.Count)).ClearContents
                End If
            End If
        Next oCol
    End With
     
    End Sub
     
    Function FindAll(SearchRange As Variant, _
                    FindWhat As Variant, _
                    Optional LookIn As XlFindLookIn = xlValues, _
                    Optional LookAt As XlLookAt = xlWhole, _
                    Optional SearchOrder As XlSearchOrder = xlByRows, _
                    Optional MatchCase As Boolean = False, _
                    Optional BeginsWith As String = vbNullString, _
                    Optional EndsWith As String = vbNullString, _
                    Optional BeginEndCompare As VbCompareMethod = vbTextCompare) As Range
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ' FindAll
    ' This searches the range specified by SearchRange and returns a Range object
    ' that contains all the cells in which FindWhat was found. The search parameters to
    ' this function have the same meaning and effect as they do with the
    ' Range.Find method. If the value was not found, the function return Nothing. If
    ' BeginsWith is not an empty string, only those cells that begin with BeginWith
    ' are included in the result. If EndsWith is not an empty string, only those cells
    ' that end with EndsWith are included in the result. Note that if a cell contains
    ' a single word that matches either BeginsWith or EndsWith, it is included in the
    ' result.  If BeginsWith or EndsWith is not an empty string, the LookAt parameter
    ' is automatically changed to xlPart. The tests for BeginsWith and EndsWith may be
    ' case-sensitive by setting BeginEndCompare to vbBinaryCompare. For case-insensitive
    ' comparisons, set BeginEndCompare to vbTextCompare. If this parameter is omitted,
    ' it defaults to vbTextCompare. The comparisons for BeginsWith and EndsWith are
    ' in an OR relationship. That is, if both BeginsWith and EndsWith are provided,
    ' a match if found if the text begins with BeginsWith OR the text ends with EndsWith.
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
     
    Dim FoundCell As Range
    Dim FirstFound As Range
    Dim LastCell As Range
    Dim ResultRange As Range
    Dim XLookAt As XlLookAt
    Dim Include As Boolean
    Dim CompMode As VbCompareMethod
    Dim Area As Range
    Dim MaxRow As Long
    Dim MaxCol As Long
    Dim BeginB As Boolean
    Dim EndB As Boolean
     
     
    CompMode = BeginEndCompare
    If BeginsWith <> vbNullString Or EndsWith <> vbNullString Then
        XLookAt = xlPart
    Else
        XLookAt = LookAt
    End If
     
    ' this loop in Areas is to find the last cell
    ' of all the areas. That is, the cell whose row
    ' and column are greater than or equal to any cell
    ' in any Area.
     
    For Each Area In SearchRange.Areas
        With Area
            If .Cells(.Cells.Count).Row > MaxRow Then
                MaxRow = .Cells(.Cells.Count).Row
            End If
            If .Cells(.Cells.Count).Column > MaxCol Then
                MaxCol = .Cells(.Cells.Count).Column
            End If
        End With
    Next Area
    Set LastCell = SearchRange.Worksheet.Cells(MaxRow, MaxCol)
     
    On Error GoTo 0
    Set FoundCell = SearchRange.Find(what:=FindWhat, _
            After:=LastCell, _
            LookIn:=LookIn, _
            LookAt:=XLookAt, _
            SearchOrder:=SearchOrder, _
            MatchCase:=MatchCase)
     
    If Not FoundCell Is Nothing Then
        Set FirstFound = FoundCell
        Do Until False ' Loop forever. We'll "Exit Do" when necessary.
            Include = False
            If BeginsWith = vbNullString And EndsWith = vbNullString Then
                Include = True
            Else
                If BeginsWith <> vbNullString Then
                    If StrComp(Left(FoundCell.Text, Len(BeginsWith)), BeginsWith, BeginEndCompare) = 0 Then
                        Include = True
                    End If
                End If
                If EndsWith <> vbNullString Then
                    If StrComp(Right(FoundCell.Text, Len(EndsWith)), EndsWith, BeginEndCompare) = 0 Then
                        Include = True
                    End If
                End If
            End If
            If Include = True Then
                If ResultRange Is Nothing Then
                    Set ResultRange = FoundCell
                Else
                    Set ResultRange = Application.Union(ResultRange, FoundCell)
                End If
            End If
            Set FoundCell = SearchRange.FindNext(After:=FoundCell)
            If (FoundCell Is Nothing) Then
                Exit Do
            End If
            If (FoundCell.Address = FirstFound.Address) Then
                Exit Do
            End If
     
        Loop
    End If
     
    Set FindAll = ResultRange
     
    End Function
    En restant disponible.

    Cordialement,
    Kimy
    La logique :
    • Plus ya de gruyère, moins ya de gruyère.
    • Plus tu pédales moins vite, moins tu avances plus vite.
    Plusoyer les réponses pertinentes et n'oublier pas de résolver en fin de post !

  5. #5
    Membre régulier Avatar de horemheb
    Homme Profil pro
    Enseignant Chercheur
    Inscrit en
    Novembre 2010
    Messages
    122
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 68
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Enseignant Chercheur
    Secteur : Enseignement

    Informations forums :
    Inscription : Novembre 2010
    Messages : 122
    Points : 77
    Points
    77
    Par défaut Dernière ligne utilisée
    Merci pour ta réponse, c'est ce que je cherchais.
    Mille jours d’entraînement pour forger, dix milles jours d’entraînement pour polir.
    Shimen Musashi - Gorin-no-sho Rouleau de l’eau

+ Répondre à la discussion
Cette discussion est résolue.

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo