kelly mort
Well-known Member
- Joined
- Apr 10, 2017
- Messages
- 2,169
- Office Version
- 2016
- Platform
- Windows
Do you mean automatically, or if the vba code is run again?The data can grow or shrink. And when it does, I want to adapt to it with the borders.
In that case it would probably be simplest to have the vba apply the borders as it fills the sheet. Could you post the code so that we could assess how to adapt it for this extra task?a vba script is filling the sheet
Private Sub Worksheet_Change(ByVal Target As Range)
Dim K, H, L, N&, V, R&, C%
If Target.Address <> "$B$1" Then Exit Sub
Application.EnableEvents = False
Me.UsedRange.Offset(1).Clear
If IsEmpty(Target) Then Application.EnableEvents = True: Exit Sub
K = [{2,3,4,15}]
H = Application.Index(Worksheets(1).UsedRange.Rows(1), , K)
L = Application.Index(Worksheets(1).UsedRange.Rows(1), , [{16,16,17,17}])
For N = 1 To Me.Index - 1
With Sheets(N).UsedRange
V = Application.Match(Target, .Columns(1), 0)
If IsNumeric(V) Then
Cells(R + 2, 1).Value2 = .Parent.Name
Cells(R + 3, 1).Resize(, UBound(K)).Value2 = H
R = R + 4
Cells(R, 1).Resize(, UBound(K)).Value2 = Application.Index(.Rows(V), , K)
For C = 5 To 13 Step 2
If IsEmpty(.Cells(V, C)) Then Exit For
R = R + 1
Cells(R, 2).Resize(, 2).Value = .Cells(V, C).Resize(, 2).Value
Next
R = R + 1
L(2) = .Cells(V, 16).Value2: L(4) = .Cells(V, 17).Value2
Cells(R, 1).Resize(, UBound(L)).Value2 = L
End If
End With
Next
Application.EnableEvents = True
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
Dim K, H, L, N&, V, R&, C%
Dim RowStart As Long, Rws As Long '*******************************
If Target.Address <> "$B$1" Then Exit Sub
Application.EnableEvents = False
Me.UsedRange.Offset(1).Clear
If IsEmpty(Target) Then Application.EnableEvents = True: Exit Sub
K = [{2,3,4,15}]
H = Application.Index(Worksheets(1).UsedRange.Rows(1), , K)
L = Application.Index(Worksheets(1).UsedRange.Rows(1), , [{16,16,17,17}])
For N = 1 To Me.Index - 1
With Sheets(N).UsedRange
V = Application.Match(Target, .Columns(1), 0)
If IsNumeric(V) Then
RowStart = R + 2 '*******************************
Cells(R + 2, 1).Value2 = .Parent.Name
Cells(R + 3, 1).Resize(, UBound(K)).Value2 = H
R = R + 4
Cells(R, 1).Resize(, UBound(K)).Value2 = Application.Index(.Rows(V), , K)
For C = 5 To 13 Step 2
If IsEmpty(.Cells(V, C)) Then Exit For
R = R + 1
Cells(R, 2).Resize(, 2).Value = .Cells(V, C).Resize(, 2).Value
Next
R = R + 1
L(2) = .Cells(V, 16).Value2: L(4) = .Cells(V, 17).Value2
Cells(R, 1).Resize(, UBound(L)).Value2 = L
'*******************************
Rws = R - RowStart + 1
With Range("A" & RowStart).Resize(Rws, 4)
.BorderAround xlContinuous
.Rows(2).BorderAround xlContinuous
.Rows(3).BorderAround xlContinuous
.Rows(Rws).BorderAround xlContinuous
.Offset(1).Resize(Rws - 1).Borders(xlInsideVertical).LineStyle = xlContinuous
End With
'*******************************
End If
End With
Next
Application.EnableEvents = True
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Rng As Range, TblRng As Range, RwRng As Range
Application.EnableEvents = False
Application.ScreenUpdating = False
Set TblRng = Columns("A:D").Cells
If Not Intersect(Target, TblRng) Is Nothing Then
For Each Rng In Target
With Rng
Set RwRng = TblRng.Rows(.Row).Cells
If WorksheetFunction.CountA(RwRng) = 0 Then
For B = 7 To 12
If B <> 8 And B <> 9 Then
RwRng.Borders(B).LineStyle = xlNone
End If
Next
ElseIf WorksheetFunction.CountA(RwRng) = 1 And .MergeCells = False Then
For B = 7 To 10
With RwRng.Borders(B)
.LineStyle = xlContinuous
.Color = RGB(0, 0, 0)
.Weight = xlThin
End With
Next
ElseIf .MergeCells = True Then
For B = 7 To 10
With .MergeArea.Borders(B)
.LineStyle = xlContinuous
.Color = RGB(0, 0, 0)
.Weight = xlThin
End With
Next
ElseIf WorksheetFunction.CountA(RwRng) = 4 Or (RwRng.Cells(1, 1) <> "" And RwRng.Cells(1, RwRng.Columns.Count) <> "") Then
For B = 7 To 12
With RwRng.Borders(B)
.LineStyle = xlContinuous
.Color = RGB(0, 0, 0)
.Weight = xlThin
End With
Next
ElseIf WorksheetFunction.CountA(RwRng) = 2 Then
For B = 7 To 12
RwRng.Borders(B).LineStyle = xlNone
Next
For B = 7 To 12
If RwRng.Cells(1, 1).Offset(-1, 0) <> "" And RwRng.Cells(1, RwRng.Columns.Count).Offset(-1, 0) <> "" Then
If B <> 9 Then
With RwRng.Borders(B)
.LineStyle = xlContinuous
.Color = RGB(0, 0, 0)
.Weight = xlThin
End With
End If
ElseIf RwRng.Cells(1, 1).Offset(1, 0) <> "" And RwRng.Cells(1, RwRng.Columns.Count).Offset(1, 0) <> "" Then
If B <> 8 Then
With RwRng.Borders(B)
.LineStyle = xlContinuous
.Color = RGB(0, 0, 0)
.Weight = xlThin
End With
End If
Else
If B <> 8 And B <> 9 Then
With RwRng.Borders(B)
.LineStyle = xlContinuous
.Color = RGB(0, 0, 0)
.Weight = xlThin
End With
End If
End If
Next
End If
End With
Next
End If
Application.ScreenUpdating = True
Application.EnableEvents = True
End Sub
Cheers. Glad you got that sorted.@Peter_SSs
Sorry for the earlier post. Your code worked perfectly!
I was having a code that was overriding yours.
You mean "Three last things .."One last thing:
.Rows(Rws).BorderAround xlContinuous
.Offset(1).Resize(Rws - 1).Borders(xlInsideVertical).LineStyle = xlContinuous
'#############
Union(.Rows("1:3"), .Rows(Rws)).Font.Bold = True
.NumberFormat = "0.00"
.Offset(1).Resize(Rws - 1).HorizontalAlignment = xlRight
'#############
End With
End If