Excel Header Macro maxes out at 3 lines - vba

This macro is to set a four line left header for all the worksheets in the workbook. The point of this macro is to have control of each line's font size, and attribute i.e bold. I have a separate sheet called header that has the values I use. It will work for 3 lines, but when I add the fourth it bugs out.
I get this error.
Run-time error '1004':
Unable to set the LeftHeader property of the PageSetup class
Also note I am running Excel 2010 64 bit.
Sub Header()
'
' Header Macro
'
lHeader = "&""Calibri,Regular""&10" & Worksheets("Header").Range("B2").Value
lHeader = lHeader & Chr(13) & "&""Calibri,Regular""&10" & Worksheets("Header").Range("B3").Value
lHeader = lHeader & Chr(13) & "&""Calibri,Regular""&10" & Worksheets("Header").Range("B4").Value
lHeader = lHeader & Chr(13) & "&""Calibri,Regular""&10" & Worksheets("Header").Range("B5").Value
Dim Naam As String, NameFile() As String
ReDim NameFile(1 To Sheets.Count)
With Application
.ScreenUpdating = False
i = 1
For Each shtNext In Sheets
With Sheets(i).PageSetup
.LeftHeader = lHeader
.LeftFooter = Format(Now, "mmmm d, yyyy")
.CenterFooter = "&""Calibri,Regular""&10" & Format(Worksheets("Header").Range("C8").Value)
.RightFooter = "&""Calibri,Regular""&10" & Format(Worksheets("Header").Range("D8").Value) & "&p of &N"
End With
i = i + 1
Next shtNext
End With
End Sub

You are getting that error because you are exceeding the 255 character limit. Unfortunately I couldn't find an MSDN article to back it up but it can be easily re-produced.
The current lengths that you have is
HeaderL - 121
HeaderC - 112
HeaderR - 121
Total - 354
Try this code. Here you can experiment it to reduce the characters.
Sub Header()
HeaderL = "&""Calibri,Regular""&10" & Worksheets("Header").Range("B2").Value
HeaderL = HeaderL & Chr(10) & "&""Calibri,Regular""&10" & Worksheets("Header").Range("B3").Value
HeaderL = HeaderL & Chr(10) & "&""Calibri,Regular""&10" & Worksheets("Header").Range("B4").Value
HeaderL = HeaderL & Chr(10) & "&""Calibri,Regular""&10" & Worksheets("Header").Range("B5").Value
HeaderC = "&""Calibri,Bold""&14" & Worksheets("Header").Range("C2").Value
HeaderC = HeaderC & Chr(10) & "&""Calibri,Bold""&14" & Worksheets("Header").Range("C3").Value
HeaderC = HeaderC & Chr(10) & "&""Calibri,Bold""&14" & Worksheets("Header").Range("C4").Value
HeaderC = HeaderC & Chr(10) & "&""Calibri,Regular""&14" & Worksheets("Header").Range("B5").Value
HeaderR = "&""Calibri,Regular""&10" & Worksheets("Header").Range("D2").Value
HeaderR = HeaderR & Chr(10) & "&""Calibri,Regular""&10" & Worksheets("Header").Range("D3").Value
HeaderR = HeaderR & Chr(10) & "&""Calibri,Regular""&10" & Worksheets("Header").Range("D4").Value
HeaderR = HeaderR & Chr(10) & "&""Calibri,Regular""&10" & Worksheets("Header").Range("D5").Value
If Len(HeaderL) + Len(HeaderC) + Len(HeaderR) > 255 Then
MsgBox "Oops, You have exceeded the character limit. Please reduce it and try again"
Exit Sub
End If
Dim ws As WorkSheet
Application.ScreenUpdating = False
For Each ws In ThisWorkbook.Sheets
With ws.PageSetup
.LeftHeader = HeaderL
.CenterHeader = HeaderC
.RightHeader = HeaderR
.LeftFooter = Format(Now, "mmmm d, yyyy")
.CenterFooter = "&""Calibri,Regular""&10" & Format(Worksheets("Header").Range("C8").Value)
.RightFooter = "&""Calibri,Regular""&10" & Format(Worksheets("Header").Range("D8").Value) & "&p of &N"
End With
Next ws
Application.ScreenUpdating = True
End Sub

The 255 character limit is for real, even when separating the macros. The font values are a big part of the total. So I removed them all execpt the last line. But If I ever need a report that has different font sizes. I have a little wiggle room to change 2 of the lines without it getting too big.
LHeader = Worksheets("Header").Range("B2").Value
LHeader = LHeader & Chr(10) & Worksheets("Header").Range("B3").Value
LHeader = LHeader & Chr(10) & Worksheets("Header").Range("B4").Value
LHeader = "&""Calibri,Regular""&10" & LHeader & Chr(10) & Worksheets("Header").Range("B5").Value
CHeader = Worksheets("Header").Range("C2").Value
CHeader = CHeader & Chr(10) & Worksheets("Header").Range("C3").Value
CHeader = CHeader & Chr(10) & Worksheets("Header").Range("C4").Value
CHeader = "&""Calibri,Bold""&14" & CHeader & Chr(10) & Worksheets("Header").Range("B5").Value
RHeader = Worksheets("Header").Range("D2").Value
RHeader = RHeader & Chr(10) & Worksheets("Header").Range("D3").Value
RHeader = RHeader & Chr(10) & Worksheets("Header").Range("D4").Value
RHeader = "&""Calibri,Regular""&10" & RHeader & Chr(10) & Worksheets("Header").Range("D5").Value

Related

If Then to un-protect and protect document

I have a userform that unprotects a document to let info be entered then protects the document. All of my subs work aside one.
Ranges with if/thens don't work but basic if then works.
Example of sub that works:
Private Sub ComboBox5_Change()
ActiveDocument.Unprotect "password"
Dim ComboBox5 As Range
Set ComboBox5 = ActiveDocument.Bookmarks("bmragpd").Range
ComboBox5.Text = Me.ComboBox5.Value
If Me.ComboBox5.Value = "No" Then
ComboBox5.Text = "205.55a"
End If
If Me.ComboBox5.Value = "Yes" Then
ComboBox5.Text = ""
End If
ActiveDocument.Protect wdAllowOnlyFormFields, NoReset:=True, Password:="password"
End Sub
This sub will say the document is already unprotected.
I tried removing the unprotect on combobox6:
Private Sub ComboBox6_Change()
ActiveDocument.Unprotect "password"
Dim rngComboBox6 As Range
Dim sssaText As String
Dim iiia As Integer
Set rngComboBox6 = ActiveDocument.Bookmarks("bmfcs").Range
sssaText = ComboBox6.Value
If Me.ComboBox6.Value = "Yes" Then
For iiia = 1 To 1
sssaText = sssaText & Chr(13) & "200" _
& Chr(13) & "200.1" _
& Chr(13) & "" _
& Chr(13) & "OEBS" _
& Chr(13) & "" _
& Chr(13) & "21c" _
& Chr(13) & "" _
& Chr(13) & "22c" _
& Chr(13) & "Yes" _
& Chr(13) & "" _
& Chr(13) & "Yes" _
& Chr(13) & "Two" _
& Chr(13) & "" _
& Chr(13) & "ES2a.1" _
& Chr(13) & "" _
& Chr(13) & "222" _
& Chr(13) & "" _
& Chr(13) & "222a" _
& Chr(13) & "222b" _
& Chr(13) & "" _
& Chr(13) & "3.a.1" _
& Chr(13) & "" _
& Chr(13) & "NA" _
& Chr(13) & "" _
& Chr(13) & "I. TuuVa"
Next iiia
sssaText = sssaText & Chr(13) & "717217" _
& Chr(13) & "" _
& Chr(13) & "1212" _
& Chr(13) & "" _
& Chr(13) & "D.1" _
& Chr(13) & "F2B-4"
End If
rngComboBox6.Text = sssaText
ActiveDocument.Bookmarks.Add "bmfcs", rngComboBox6
If Me.ComboBox6.Value = "No" Then
ComboBox6.Text = ""
End If
ActiveDocument.Protect wdAllowOnlyFormFields, NoReset:=True, Password:="password"
End Sub
The first End If is in the wrong place. You are adding text to the document regardless of the value of the combo box.
It is not good practice to use a control’s change event to commit changes to a document. Apart from anything else it doesn’t allow the user to cancel without making changes.
Instead use an OK/Apply/Finish button.
Then you only need to unprotect/reprotect the document once.

Invalid qualifier Error Message in vba code

This code is designed to detect the columns of start and finish of a shape which is used and displayed onto the caption of the shape itself. The following code is the problematic code:
Sub Take_Baseline()
Dim forcast_weeksStart() As String
Dim forcast_weeksEnd() As String
Dim forcastDate As String
Dim shp As Shape
Dim split_text() As String
'cycle through all the shapes in the worsheet and enter the forcast date for all the projects into their respective boxes
For Each shp In ActiveSheet.Shapes
'initialize forcast date by parsing
forcast_weeksStart = Split(shp.TopLeftCell.Column.Text, " ")
forcast_weeksEnd = Split(shp.BottomRightCell.Column.Text, " ")
forcastDate = forcast_weeksStart(1) & "-" & forcast_weeksEnd(1)
temp = shp.OLEFormat.Object.Object.Caption
If InStr(temp, "/-/") > 0 & InStr(temp, "In Prog") Then
split_text = Split(shp.OLEFormat.Object.Caption, " ")
For i = 0 To (i = 3)
shp.TextFrame.Characters.Caption = split_text(i) & vbNewLine
Next i
ActiveSheet.Shapes(Sheet4.Range("B1")).TextFrame.Characters.Caption = ActiveSheet.Shapes(Sheet4.Range("B1")).TextFrame.Characters.Caption & vbNewLine & ActiveSheet.Cells(4, AShape.TopLeftCell.Column).Text & " - " & ActiveSheet.Cells(4, AShape.BottomRightCell.Column).Text & vbNewLine & "dates: " & forcast_weeksStart(1) & " - " & forcast_weeksEnd(1) & "/" & forcast_weeksStart(1) & " - " & forcast_weeksEnd(1) & "/" & "/" & "actualDate"
' ElseIf InStr(temp, "/-/") > 0 & InStr(temp, "In Prog") = 0 Then
'split_text = Split(shp.OLEFormat.Object.Object.Caption, " ")
' For i = 0 To (i = 2)
' shp.OLEFormat.Object.Caption = split_text(i) & vbNewLine
' Next i
'ActiveSheet.Shapes(Sheet4.Range("B1")).TextFrame.Characters.Caption = ActiveSheet.Shapes(Sheet4.Range("B1")).TextFrame.Characters.Caption & vbNewLine & "In Prog" & vbNewLine & ActiveSheet.Cells(4, AShape.TopLeftCell.Column).Text & " - " & ActiveSheet.Cells(4, AShape.BottomRightCell.Column).Text & vbNewLine & "dates: " & forcast_weeksStart(1) & " - " & forcast_weeksEnd(1) & "/" & forcast_weeksStart(1) & " - " & forcast_weeksEnd(1) & "/" & "actualDate"
End If
Next shp
'For testing purposes
Sheet4.Range("A20").Value = forcast_weeksStart(1)
Sheet4.Range("A21").Value = forcast_weeksEnd(1) End Sub
The error is an
"invalid qualifier"
message which occurs on line
forcast_weeksStart = Split(shp.TopLeftCell.Column.Text, " ")
Right on the "column" word. I don't get why this is happening since the actual drop down menu has the column operation which i can select. I have tried everything from changing it to the OLEformat.Object.Caption etc etc. But nothing has worked. I am still relatively new to vba so any help will be appreciated. Thanks

How to dynamically add Checkbox-Buttons to userform in VBA

I have a userform with 5 checkbox buttons for 5 pdf versions.
Well, when the user calls the userform, then the userform initializes 5 checkbox buttons to select one of them. At the moment, the code is very static and not so good.
Here the example:
If rs.EOF = False Then
Do Until rs.EOF Or i = 5
Select Case i
Case Is = 0
frmOne.Version5.Visible = True
frmOne.Version5.Caption = rs!versNo & "#" & rs!versFrom
frmOne.Version5.tag = rs!versNo & "_" & rs!FiD & ".pdf"
Case Is = 1
frmOne.Version4.Visible = True
frmOne.Version4.Caption = rs!versNo & "#" & rs!versFrom
frmOne.Version4.tag = rs!versNo & "_" & rs!FiD & ".pdf"
Case Is = 2
frmOne.Version3.Visible = True
frmOne.Version3.Caption = rs!versNo & "#" & rs!versFrom
frmOne.Version3.tag = rs!versNo & "_" & rs!FiD & ".pdf"
Case Is = 3
frmOne.Version2.Visible = True
frmOne.Version2.Caption = rs!versNo & "#" & rs!versFrom
frmOne.Version2.tag = rs!versNo & "_" & rs!FiD & ".pdf"
Case Is = 4
frmOne.Version1.Visible = True
frmOne.Version1.Caption = rs!versNo & "#" & rs!versFrom
frmOne.Version1.tag = rs!versNo & "_" & rs!FiD & ".pdf"
End Select
i = i + 1
rs.MoveNext
Loop
End If
To much code I think. So my intention was to define it like the example below, but this doesn't work:
If rs.EOF = False Then
For i = 1 To 5
With frmOne
.Version & i &.Visible = True
.Version & i &.Caption = rs!versNo & "#" & rs!versFrom
.Version & i &.tag = rs!versNo & "_" & rs!FiD & ".pdf"
End With
rs.MoveNext
Next i
End If
Do have anyone an idea how could I fix that?
You can refer to the Controls collection using the name:
If rs.EOF = False Then
For i = 1 To 5
With frmOne.Controls("Version" & i)
.Visible = True
.Caption = rs!versNo & "#" & rs!versFrom
.tag = rs!versNo & "_" & rs!FiD & ".pdf"
End With
rs.MoveNext
Next i
End If
To actually add the controls at runtime too:
Do While not rs.EOF
i = i + 1
With frmOne.Controls.Add("Forms.CheckBox.1", "Version" & i, True)
.Caption = rs!versNo & "#" & rs!versFrom
.tag = rs!versNo & "_" & rs!FiD & ".pdf"
End With
rs.MoveNext
Loop
go like follows:
If rs.EOF = False Then
For i = 1 To 5
With frmOne.Controls("Version" & i) '<~~ use Controls collection of Userform object
.Visible = True
.Caption = rs!versNo & "#" & rs!versFrom
.Tag = rs!versNo & "_" & rs!FiD & ".pdf"
End With
rs.MoveNext
Next i
End If

Excel VBA Comment Query

So I have tried and failed many times to do this.
I'm trying to write a VBA script that when run will insert a comment in the active cell that says:
[Surname], [First Name]
[Todays date] was [current cell value]
Surnames and First Names should be bold.
If the cell is edited again it would edit the comment and put the above format below the previous comment.
Here is what I have at the moment (bearing in mind it could be very wrong)
Sub Test()
Dim UserN As String
Dim commt As String
Dim cmt As Comment
UserN = Application.UserName
Set cmt = ActiveCell.Comment
If ActiveCell.Comment Is Nothing Then
commt = UserN & Chr(10) & Chr(10) _
& Chr(10) & Format(Now, strDate) & " was £" & ActiveCell
With Selection
With Cells(Selection.Row, Selection.Column)
.ClearComments
.AddComment
.Comment.Visible = False
.Comment.Shape.AutoShapeType = msoShapeRoundedRectangle
.Comment.Text Text:=commt
With .Comment.Shape.TextFrame
' Username
With .Characters(1, Len(UserN)).Font
.Bold = True
End With
With .Characters.Font
.Size = 12
End With
End With
.Comment.Shape.TextFrame.AutoSize = True
End With
End With
Else
commt.txt = commt.txt & Chr(10) UserN & Chr(10) & Chr(10)
& Chr(10) & Format(Now, strDate) & " was £" & ActiveCell
With Selection
With Cells(Selection.Row, Selection.Column)
.ClearComments
.AddComment
.Comment.Visible = False
.Comment.Shape.AutoShapeType = msoShapeRoundedRectangle
.Comment.Text Text:=commt
With .Comment.Shape.TextFrame
' Username
With .Characters(1, Len(UserN)).Font
.Bold = True
End With
With .Characters.Font
.Size = 12
End With
End With
.Comment.Shape.TextFrame.AutoSize = True
End With
End With
End If
End Sub
I think the only major issue is with this line:
commt.txt = commt.txt & Chr(10) UserN & Chr(10) & Chr(10)
& Chr(10) & Format(Now, strDate) & " was £" & ActiveCell
It is using the variable 'commt' as if it were a comment object, but is simply a string. It's also missing a '&' and a '_' line separator. I believe the line should be:
commt = cmt.text & Chr(10) & UserN & Chr(10) & Chr(10) & Chr(10) & Format(Now, strDate) & " was £" & ActiveCell

Output doesn't match input

I've created a macro that's meant to created a lump of CSS & HTML from a set of values in each sheet of a spreadsheet.
It's a little untidy as I created the function to write it from one sheet first as a proof of concept, and then updated it.
It doesn't throw any obvious errors, but the output varies, sometimes it shows the same thing both times, and then depending on where I've got debug MsgBoxs or watches in VBA seems to alter the output.
Any ideas what on earth i'm doing wrong?
Sub createCode()
Dim myWorkbook As Workbook
Dim mySheet As Worksheet
Set myWorkbook = Application.ActiveWorkbook
For Each mySheet In myWorkbook.Worksheets
Dim bannerCount As Integer
Dim BannerCollection() As Banner
Dim r As Range
Dim lastRow, lastCol
Dim allCells As Range
bannerCount = 0
lastCol = mySheet.Range("a2").End(xlToRight).Column
lastRow = mySheet.Range("a2").End(xlDown).Row
Set allCells = mySheet.Range("a2", mySheet.Cells(lastRow, lastCol))
' MsgBox (mySheet.Name)
' MsgBox ("lastRow:" & lastRow & "lastCol:" & lastCol)
ReDim BannerCollection(allCells.Rows.Count)
For Each r In allCells.Rows
Dim thisBanner As Banner
thisBanner.imagePath = ""
thisBanner.retImagePath = ""
thisBanner.bannerTitle = ""
thisBanner.urlPath = ""
bannerCount = bannerCount + 1
' MsgBox (bannerCount)
thisBanner.imagePath = Cells(r.Row, 2).Value
thisBanner.retImagePath = Cells(r.Row, 3).Value
thisBanner.bannerTitle = Cells(r.Row, 4).Value
thisBanner.urlPath = Cells(r.Row, 5).Value
'MsgBox (Cells(r.Row, 2).Value)
'MsgBox (Cells(r.Row, 3).Value)
'MsgBox (Cells(r.Row, 4).Value)
'MsgBox (Cells(r.Row, 5).Value)
BannerCollection(bannerCount - 1) = thisBanner
Next r
Dim i As Variant
Dim retinaCSS, imgCSS, firstBannerCode, otherBannersCode, bannerTracking As String
retinaCSS = ""
imgCSS = ""
firstBannerCode = ""
otherBannersCode = ""
bannerTracking = ""
For i = 0 To bannerCount - 1
bannerTracking = BannerCollection(i).bannerTitle
bannerTracking = Replace(bannerTracking, " ", "+")
bannerTracking = Replace(bannerTracking, "&", "And")
bannerTracking = Replace(bannerTracking, "%", "PC")
bannerTracking = Replace(bannerTracking, "!", "")
bannerTracking = Replace(bannerTracking, "£", "")
bannerTracking = Replace(bannerTracking, ",", "")
bannerTracking = Replace(bannerTracking, "'", "")
bannerTracking = Replace(bannerTracking, "#", "")
bannerTracking = Replace(bannerTracking, ".", "")
retinaCSS = retinaCSS & "#sliderTarget .banner-" & i + 1 & "{background-image: url('/assets/static/" & BannerCollection(i).retImagePath & "');}" & vbNewLine
imgCSS = imgCSS & "#sliderTarget .banner-" & i + 1 & "{background-image: url('/assets/static/" & BannerCollection(i).imagePath & "');}" & vbNewLine
If i = 0 Then
firstBannerCode = firstBannerCode & "<div class=" & Chr(34) & "banner banner-" & i + 1 & " staticBanner" & Chr(34) & ">" & vbNewLine
firstBannerCode = firstBannerCode & "" & vbNewLine
firstBannerCode = firstBannerCode & "</div>" & vbNewLine
Else
otherBannersCode = otherBannersCode & "<div class=" & Chr(34) & "banner banner-" & i + 1 & " staticBanner" & Chr(34) & ">" & vbNewLine
otherBannersCode = otherBannersCode & "" & vbNewLine
otherBannersCode = otherBannersCode & "</div>" & vbNewLine
End If
' MsgBox (BannerCollection(i).retImagePath & vbNewLine & BannerCollection(i).imagePath & vbNewLine & BannerCollection(i).bannerTitle & vbNewLine & BannerCollection(i).urlPath)
Next i
CodeString = ""
CodeString = CodeString & "<style type=" & Chr(34) & "text/css" & Chr(34) & ">" & vbNewLine
CodeString = CodeString & "/* Banners */" & vbNewLine
CodeString = CodeString & imgCSS
CodeString = CodeString & "/* Retina Banners */" & vbNewLine
CodeString = CodeString & "#media only screen and (-webkit-min-device-pixel-ratio: 2) {" & vbNewLine
CodeString = CodeString & retinaCSS
CodeString = CodeString & "}" & vbNewLine
CodeString = CodeString & "</style>" & vbNewLine
CodeString = CodeString & "<div id=" & Chr(34) & "sliderTarget" & Chr(34) & " class=" & Chr(34) & "slides" & Chr(34) & ">" & vbNewLine
CodeString = CodeString & firstBannerCode
CodeString = CodeString & "</div>" & vbNewLine
CodeString = CodeString & "<script id=" & Chr(34) & "sliderTemplate" & Chr(34) & " type=" & Chr(34) & "text/template" & Chr(34) & ">" & vbNewLine
CodeString = CodeString & otherBannersCode
CodeString = CodeString & "</script>"
FilePath = Application.DefaultFilePath & "\" & mySheet.Name & "code.txt"
Open FilePath For Output As #2
Print #2, CodeString
Close #2
MsgBox ("code.txt contains:" & CodeString)
MsgBox (Application.DefaultFilePath & "\" & mySheet.Name & "code.txt")
Erase BannerCollection
Next mySheet
End Sub
and here is the Banner type:
Public Type Banner
imagePath As String
retImagePath As String
urlPath As String
bannerTitle As String
End Type
I ended up doing a bit of a code review (oops spent too much time on the Code Review site). I'll post this here in addition to #Jeeped answer in case you get some value from it.
Option Explicit
You should specify Option Explicit at the top of each code module. What this does is tell the VBA compiler to check that every variable that you are trying to use has been declared (i.e. you've got Dim blah as String, Public blah as String or Private blah as String for each blah you're using).
If you attempt to use a variable which hasn't been declared, the compiler will give you a compilation error where the first problem occurs. This helps if you mistype a variable name, otherwise the compiler will think you are talking about something new.
Adding this to the top of your code requires a couple of declarations in your code but nothing major.
Multiple variable declaration on a single line
Don't do it. You have the following line: Dim retinaCSS, imgCSS, firstBannerCode, otherBannersCode, bannerTracking As String which declares 5 variables. The first 4 are declared as variants and the last one is a String. Now your code may work like this but you were probably expecting all 5 to be Strings. Other languages I believe do operate this way but VBA doesn't.
Declare them separately like:
Dim retinaCSS As String
Dim imgCSS As String
Dim firstBannerCode As String
Dim otherBannersCode As String
Dim bannerTracking As String
Don't initialise variables unnecessarily
I see code like:
CodeString = ""
CodeString = CodeString & "<style type=" & Chr(34) & "text/css" & Chr(34) & ">" & vbNewLine
Now the problem with this is that you're assigning the empty string value to CodeString but then you are immediately assigning something else to it in the very next line. The risk is that you might try to use a variable before you have assigned something to it. This isn't a risk for the string type since it implicitly assigned an empty string value when it is created.
You can safely remove the first assignment to it. The danger could come from object references. Say if you have a reference to a worksheet but do not assign a worksheet to the variable before you try to use it. In any case you want to make sure that your variable has the required value before you attempt to use the value it holds.
Use Collection instead of an array
The array code is cumbersome and inflexible. VBA has a simple collection type which allows you to add and remove items to and from it without having to declare a fixed size.
You can also iterate through the contents using a For Each loop.
Here is the code I'm recommending:
Dim BannerCollection As Collection
Set BannerCollection = New Collection
' ...
For Each r In allCells.Rows
Dim thisBanner As Banner
Set thisBanner = New Banner
' ...
BannerCollection.Add thisBanner
Next r
' ...
Dim b As Banner
For Each b In BannerCollection
' do something with the banner.
Next
Now to do this, Banner must be a Class not a Type. I think it makes life a lot easier though.
Split a big method up into single purpose methods.
For instance I extracted a method as follows:
Private Function UrlEncode(ByVal text As String) As String
text = Replace(text, " ", "+")
text = Replace(text, "&", "And")
text = Replace(text, "%", "PC")
text = Replace(text, "!", "")
text = Replace(text, "£", "")
text = Replace(text, ",", "")
text = Replace(text, "'", "")
text = Replace(text, "#", "")
text = Replace(text, ".", "")
UrlEncode = text
End Function
Now this can be referenced like bannerTracking = UrlEncode(b.bannerTitle).
You are setting allCells to a distinct range of cells correctly.
Set allCells = mySheet.Range("a2", mySheet.Cells(lastRow, lastCol))
Then you loop through each row in the allCells range.
For Each r In allCells.Rows
But when you actually go to use r, it is only to use the row number.
thisBanner.imagePath = Cells(r.Row, 2).Value
r.Row is a number between 1 and 1,048,576, nothing more. There is no guarantee that Cells(r.Row, 2).Value refers to something on mySheet; only that whatever worksheet it is coming from it will using whatever worksheet's row number that corresponds to r.row. You need to define some parentage. An With ... End With block within the For ... Next and properly annotated .Range and .Cell references should suffice.
Sub createCode()
Dim myWorkbook As Workbook
Dim mySheet As Worksheet
Dim bannerCount As Integer
Dim BannerCollection() As Banner
Dim r As Range
Dim lastRow, lastCol
Dim allCells As Range
Set myWorkbook = Application.ActiveWorkbook
For Each mySheet In myWorkbook.Worksheets
With mySheet
'declare your vars outside the loop and zero/null then here if necessary.
bannerCount = 0
lastCol = .Range("a2").End(xlToRight).Column
lastRow = .Range("a2").End(xlDown).Row
Set allCells = .Range("a2", .Cells(lastRow, lastCol))
' MsgBox (mySheet.Name)
' MsgBox ("lastRow:" & lastRow & "lastCol:" & lastCol)
ReDim BannerCollection(allCells.Rows.Count)
For Each r In allCells.Rows
Dim thisBanner As Banner
thisBanner.imagePath = ""
thisBanner.retImagePath = ""
thisBanner.bannerTitle = ""
thisBanner.urlPath = ""
bannerCount = bannerCount + 1
' MsgBox (bannerCount)
thisBanner.imagePath = .Cells(r.Row, 2).Value
thisBanner.retImagePath = .Cells(r.Row, 3).Value
thisBanner.bannerTitle = .Cells(r.Row, 4).Value
thisBanner.urlPath = .Cells(r.Row, 5).Value
'MsgBox (.Cells(r.Row, 2).Value)
'MsgBox (.Cells(r.Row, 3).Value)
'MsgBox (.Cells(r.Row, 4).Value)
'MsgBox (.Cells(r.Row, 5).Value)
BannerCollection(bannerCount - 1) = thisBanner
Next r
Dim i As Variant
Dim retinaCSS, imgCSS, firstBannerCode, otherBannersCode, bannerTracking As String
retinaCSS = ""
imgCSS = ""
firstBannerCode = ""
otherBannersCode = ""
bannerTracking = ""
For i = 0 To bannerCount - 1
bannerTracking = BannerCollection(i).bannerTitle
bannerTracking = Replace(bannerTracking, " ", "+")
bannerTracking = Replace(bannerTracking, "&", "And")
bannerTracking = Replace(bannerTracking, "%", "PC")
bannerTracking = Replace(bannerTracking, "!", "")
bannerTracking = Replace(bannerTracking, "£", "")
bannerTracking = Replace(bannerTracking, ",", "")
bannerTracking = Replace(bannerTracking, "'", "")
bannerTracking = Replace(bannerTracking, "#", "")
bannerTracking = Replace(bannerTracking, ".", "")
retinaCSS = retinaCSS & "#sliderTarget .banner-" & i + 1 & "{background-image: url('/assets/static/" & BannerCollection(i).retImagePath & "');}" & vbNewLine
imgCSS = imgCSS & "#sliderTarget .banner-" & i + 1 & "{background-image: url('/assets/static/" & BannerCollection(i).imagePath & "');}" & vbNewLine
If i = 0 Then
firstBannerCode = firstBannerCode & "<div class=" & Chr(34) & "banner banner-" & i + 1 & " staticBanner" & Chr(34) & ">" & vbNewLine
firstBannerCode = firstBannerCode & "" & vbNewLine
firstBannerCode = firstBannerCode & "</div>" & vbNewLine
Else
otherBannersCode = otherBannersCode & "<div class=" & Chr(34) & "banner banner-" & i + 1 & " staticBanner" & Chr(34) & ">" & vbNewLine
otherBannersCode = otherBannersCode & "" & vbNewLine
otherBannersCode = otherBannersCode & "</div>" & vbNewLine
End If
' MsgBox (BannerCollection(i).retImagePath & vbNewLine & BannerCollection(i).imagePath & vbNewLine & BannerCollection(i).bannerTitle & vbNewLine & BannerCollection(i).urlPath)
Next i
CodeString = ""
CodeString = CodeString & "<style type=" & Chr(34) & "text/css" & Chr(34) & ">" & vbNewLine
CodeString = CodeString & "/* Banners */" & vbNewLine
CodeString = CodeString & imgCSS
CodeString = CodeString & "/* Retina Banners */" & vbNewLine
CodeString = CodeString & "#media only screen and (-webkit-min-device-pixel-ratio: 2) {" & vbNewLine
CodeString = CodeString & retinaCSS
CodeString = CodeString & "}" & vbNewLine
CodeString = CodeString & "</style>" & vbNewLine
CodeString = CodeString & "<div id=" & Chr(34) & "sliderTarget" & Chr(34) & " class=" & Chr(34) & "slides" & Chr(34) & ">" & vbNewLine
CodeString = CodeString & firstBannerCode
CodeString = CodeString & "</div>" & vbNewLine
CodeString = CodeString & "<script id=" & Chr(34) & "sliderTemplate" & Chr(34) & " type=" & Chr(34) & "text/template" & Chr(34) & ">" & vbNewLine
CodeString = CodeString & otherBannersCode
CodeString = CodeString & "</script>"
FilePath = Application.DefaultFilePath & "\" & mySheet.Name & "code.txt"
Open FilePath For Output As #2
Print #2, CodeString
Close #2
MsgBox ("code.txt contains:" & CodeString)
MsgBox (Application.DefaultFilePath & "\" & mySheet.Name & "code.txt")
Erase BannerCollection
End With
Next mySheet
End Sub