J'ai un très grand ensemble de données qui comprend les heures de début et de fin des avions dans le NAS. Je souhaite créer une macro pour créer une représentation visuelle de ces données dans Excel, comme suit:
(note: cette image utilise de fausses données)
Comme vous pouvez le voir, j’ai fait les 7 premières lignes enCependant, il existe plusieurs fichiers de données contenant au moins 2500 lignes, ce qui rend le processus fastidieux. J’ai essayé de créer une macro mais je ne sais pas comment rechercher et sélectionner la plage appropriée à mettre en évidence.
Voici ce que j'ai jusqu'ici:
Sub autofill()
Dim rng As Range
Dim row As Range
Dim cell As Range
"set the range of the whole search area
Set rng = Range("A2:HJ121")
For Each row In rng.Rows
Dim callsign As Variant
Set callsign = cell("contents", "A" & row)
Dim valstart As Variant
Set valstart = cell("contents", "E" & row)
Dim valstop As Variant
Set valstop = cell("contents", "F" & row)
"now select the range beginning from the column whose header matches the
"time in valstart and ends at the time which matches the time in valstop
Selection.Merge
Selection.Style = "Highlight"
Selection.Value = callsign
Next row
End Sub
Quel est le moyen le plus simple de sélectionner les lignes dont j'ai besoin?
Je ne suis pas un programmeur de profession; excusez-moi d’avance si mon code montre une technique bâclée ou enfreint certains principes sacrés de la programmation.: P
Merci!
Réponses:
1 pour la réponse № 1C’est la que je vais chez VBA pour ça.
Option Explicit
Public Sub fillSchedule()
Dim startCol As Long
Dim endCol As Long
Dim i As Long
Dim j As Long
Dim ws As Excel.Worksheet
Dim entryTime As Single
Dim exitTime As Single
Dim formatRange As Excel.Range
Set ws = ActiveSheet
startCol = ws.Range("H:H").Column
endCol = ws.Range("HJ:HJ").Column
Call clearFormats
For i = 2 To ws.Cells(1, 1).End(xlDown).Row
entryTime = ws.Cells(i, 5).Value
exitTime = ws.Cells(i, 6).Value
Set formatRange = Nothing
For j = startCol To endCol
If (ws.Cells(1, j).Value > exitTime) Then
Exit For
End If
If ((entryTime < ws.Cells(1, j).Value) And (ws.Cells(1, j).Value < exitTime)) Then
If (formatRange Is Nothing) Then
Set formatRange = ws.Cells(i, j)
Else
Set formatRange = formatRange.Resize(, formatRange.Columns.Count + 1)
End If
End If
Next j
If (Not formatRange Is Nothing) Then
Call formatTheRange(formatRange, ws.Cells(i, "A").Value)
End If
Next i
End Sub
Private Sub clearFormats()
With ActiveSheet.Range("H2:HJ121")
.clearFormats
.ClearContents
End With
End Sub
Private Sub formatTheRange(ByRef r As Excel.Range, ByRef callsign As String)
r.HorizontalAlignment = xlCenter
r.Merge
r.Value = callsign
" Apply color
With r.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorLight2
.TintAndShade = 0.799981688894314
.PatternTintAndShade = 0
End With
" Apply borders
With r.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With r.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With r.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With r.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
End Sub
0 pour la réponse № 2
Que diriez-vous d'une solution de formatage conditionnel?
Mettez en surbrillance toutes les cellules de H2 à (dernière cellule en bas à droite).
Utilisez cette formule:
=IF(AND((H$1>$E2),(H$1<$F2)),TRUE)
Ensuite, appliquez un remplissage. Et si vous êtes prêt à abandonner la bordure et le nom à l’intérieur de la plage remplie, cela fonctionnera pour vous :).
Vous pouvez également souhaiter figer les volets de G2 afin de pouvoir faire défiler l'écran jusqu'à la colonne HJ tout en affichant la colonne Indicatif.
J'espère que cela t'aides