Johnny Thunder
Well-known Member
- Joined
- Apr 9, 2010
- Messages
- 693
- Office Version
- 2016
- Platform
- MacOS
Hello All,
I am working on a piece of code that loops through a range of cells and builds a Cell with data. I have the code working fine but I was not sure how to write the code in a way that I could have the code do 4 loops with varying column placement (C = #) . So as a workaround, I just wrote the same code 4 times and simply updated the column that the code looks at. Here is two of the 4 pieces of code so you can see what I mean
Any help is appreciated. Thanks!
So 1st Code looks at Column - C = 10
2nd - C = 12
3rd - C = 14
4th - C = 16
Here is the code
I am working on a piece of code that loops through a range of cells and builds a Cell with data. I have the code working fine but I was not sure how to write the code in a way that I could have the code do 4 loops with varying column placement (C = #) . So as a workaround, I just wrote the same code 4 times and simply updated the column that the code looks at. Here is two of the 4 pieces of code so you can see what I mean
Any help is appreciated. Thanks!
So 1st Code looks at Column - C = 10
2nd - C = 12
3rd - C = 14
4th - C = 16
Here is the code
Code:
'--------------------------------------------------------------
'--- Builds the Drama & Comedy Sheet Backlog Tiles - Resets Tile as well
'--------------------------------------------------------------
Sub MasterTimeline()
Dim ws1 As Worksheet, ws2 As Worksheet
Dim r As Long, c As Long, Lastr2 As Long
Dim Title As String, Season As String, AvailTime As String, Commitment As String, Genre As String, LastChar As String
Dim TitleLength As Long, SeasonLength As String
Dim BlockVariable As Variant, Shtname As Variant
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Set ws1 = Sheets("Master Timeline")
Set ws2 = Sheets("Master Data Entry")
Lastr2 = ws2.Range("E" & Rows.Count).End(xlUp).Row
'----------------------------- Filter Mode------------------------------------
On Error Resume Next
Sheets("Master Data Entry").ShowAllData 'Clear Filter
'----------------------------- Filter Mode------------------------------------
Lastr2 = ws2.Range("E" & Rows.Count).End(xlUp).Row
r = 7 'Start at row 7
c = 10 'Tile Start at Column 10 = J '<--------------------------------Here is where I need the revision of a loop as the code finishes one run, to go to the next C = # placement
ws1.Range("J7:P41").ClearContents 'Clear tiles at the start - This is for the sorting
For Each Cell In ws2.Range("B2:B" & Lastr2).SpecialCells(xlCellTypeVisible) 'Only grabs visible cells
If Cell.Value = "Grid" And Cell.Offset(0, 1).Value = "Q1" Then 'If Backlog, bring it over, if Grid it means its already being used
Title = Cell.Offset(0, 3).Value
TitleLength = Len(Title)
Season = Cell.Offset(0, 5).Value
SeasonLength = Len(Season)
Genre = Cell.Offset(0, 4).Value
AvailTime = Cell.Offset(0, 10).Value
Commitment = Cell.Offset(0, 11).Value
If Title <> "" Or Title <> "Title" Then
With ws1.Cells(r, c)
If SeasonLength = vbNullString Or SeasonLength = 0 Then 'If season is blank don't include the season text in Header of Title
.Value = Title & Chr(10) & Genre & Chr(10) & "Avail: " & AvailTime & Chr(10) & Commitment
SeasonLength = 0
Else
.Value = Title & " | S" & Season & Chr(10) & Genre & Chr(10) & "Avail: " & AvailTime & Chr(10) & Commitment
SeasonLength = 5
End If
.Font.Name = "SF Hello (Body)"
.Font.FontStyle = "Regular"
.Font.Size = 13
.HorizontalAlignment = xlLeft
.VerticalAlignment = xlTop
.IndentLevel = 0
LastChar = Len(ws1.Cells(r, c).Value)
With .Characters(Start:=1, Length:=TitleLength + SeasonLength).Font
.Name = "SF Hello (Body)"
.FontStyle = "Bold"
.Size = 20
End With
With .Characters(Start:=LastChar, Length:=1).Font
.Name = "Calibri (Body)"
.FontStyle = "Normal"
.Size = 1
End With
End With
r = r + 2 'Steps down two rows
End If 'If Title
End If 'If Cell.value
Next Cell 'Loop
ws1.Cells.FormatConditions.Delete 'Deletes all Conditinal Formatting on Sheet before reapplying
ws1.Range("J7").Select
With ws1.Range("J7:P33")
.FormatConditions.Add Type:=xlExpression, Formula1:="=Countif(J7,""*Y"")"
With .FormatConditions(.FormatConditions.Count)
.SetFirstPriority
With .Interior
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent6
.TintAndShade = 0.799981688894314
End With
End With
End With
Call MasterTimeline2
Call MasterTimeline3
Call MasterTimeline4
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Application.Calculation = xlCalculationAutomatic
Application.EnableEvents = True
Calculate
End Sub
'--------------------------------------------------------------
'--- Builds the Drama & Comedy Sheet Backlog Tiles - Resets Tile as well
'--------------------------------------------------------------
Sub MasterTimeline2()
Dim ws1 As Worksheet, ws2 As Worksheet
Dim r As Long, c As Long, Lastr2 As Long
Dim Title As String, Season As String, AvailTime As String, Commitment As String, Genre As String, LastChar As String
Dim TitleLength As Long, SeasonLength As String
Dim BlockVariable As Variant, Shtname As Variant
Set ws1 = Sheets("Master Timeline")
Set ws2 = Sheets("Master Data Entry")
'----------------------------- Filter Mode------------------------------------
On Error Resume Next
Sheets("Master Data Entry").ShowAllData 'Clear Filter
'----------------------------- Filter Mode------------------------------------
Lastr2 = ws2.Range("E" & Rows.Count).End(xlUp).Row
r = 7 'Start at row 7
c = 12 'Tile Start at Column 12 = L
For Each Cell In ws2.Range("B2:B" & Lastr2).SpecialCells(xlCellTypeVisible) 'Only grabs visible cells
If Cell.Value = "Grid" And Cell.Offset(0, 1).Value = "Q2" Then 'If Backlog, bring it over, if Grid it means its already being used
Title = Cell.Offset(0, 3).Value
TitleLength = Len(Title)
Season = Cell.Offset(0, 5).Value
SeasonLength = Len(Season)
Genre = Cell.Offset(0, 4).Value
AvailTime = Cell.Offset(0, 10).Value
Commitment = Cell.Offset(0, 11).Value
If Title <> "" Or Title <> "Title" Then
With ws1.Cells(r, c)
If SeasonLength = vbNullString Or SeasonLength = 0 Then 'If season is blank don't include the season text in Header of Title
.Value = Title & Chr(10) & Genre & Chr(10) & "Avail: " & AvailTime & Chr(10) & Commitment
SeasonLength = 0
Else
.Value = Title & " | S" & Season & Chr(10) & Genre & Chr(10) & "Avail: " & AvailTime & Chr(10) & Commitment
SeasonLength = 5
End If
.Font.Name = "SF Hello (Body)"
.Font.FontStyle = "Regular"
.Font.Size = 13
.Font.ThemeColor = xlThemeColorLight1
.Font.TintAndShade = 0
.HorizontalAlignment = xlLeft
.VerticalAlignment = xlTop
.IndentLevel = 0
LastChar = Len(ws1.Cells(r, c).Value)
With .Characters(Start:=1, Length:=TitleLength + SeasonLength).Font
.Name = "SF Hello (Body)"
.FontStyle = "Bold"
.Size = 20
End With
With .Characters(Start:=LastChar, Length:=1).Font
.Name = "Calibri (Body)"
.FontStyle = "Normal"
.Font.ThemeColor = xlThemeColorLight1
.Font.TintAndShade = 0
.Size = 1
End With
End With
r = r + 2 'Steps down two rows
End If 'If Title
End If 'If Cell.value
Next Cell 'Loop
Calculate
End Sub