VBA index out of bounds error, but not on debugging












1















I have some code which takes data from 2 different sheets, and creates and output sheet. The problem is, if a new page in the data is created, it has to do a little additional information, and changes where the page break is. When I run the code with debugging active to see which line of code is causing the error, it works just fine.



When I let it run without debugging, it will give me an error that an index is out of bounds.



I have uploaded the excel sheet itself to https://www.dropbox.com/s/pcl5zwuna8g7wrf/Test.xlsm?dl=0 but I just don't understand why it results in different outputs depending on stepping through it or not, as it is single threaded anyway?



It happens when pressing the button on the fourth sheet.



I uploaded the code to pastebin at: https://pastebin.com/rMwi7c7G



Public Function SeitenNr(rngZelle As Range) As Integer
Dim wksHor As Integer, wksVert As Integer, SeiteNr As Integer
Dim VertPb As Object, HortPb As Object
Dim lngOrder As Long

lngOrder = rngZelle.Parent.PageSetup.Order
wksVert = rngZelle.Parent.VPageBreaks.Count + 1
wksHor = rngZelle.Parent.HPageBreaks.Count + 1

SeiteNr = 1
For Each VertPb In rngZelle.Parent.VPageBreaks
If VertPb.Location.Column > rngZelle.Column Then Exit For
SeiteNr = SeiteNr + IIf(lngOrder = xlDownThenOver, wksHor, 1)
Next VertPb
For Each HortPb In rngZelle.Parent.HPageBreaks
If HortPb.Location.Row > rngZelle.Row Then Exit For
SeiteNr = SeiteNr + IIf(lngOrder = xlDownThenOver, 1, wksVert)
Next HortPb
SeitenNr = SeiteNr
End Function

Sub updateOutput()
'Sheets("Print-Macro").UsedRange.ClearContents
'Sheets("Print-Macro").Cells.UnMerge
Application.DisplayAlerts = False
Sheets("Print-Macro").Delete
Application.DisplayAlerts = True
Dim sheet As Worksheet
Set sheet = Sheets.Add
sheet.Name = "Print-Macro"

Dim indexMain As Integer
Dim currentIndex As Integer

Dim artistName As String
Dim artistNameLast As String
Dim cellIndexOutput As Integer
Dim birthdate As String
Dim deathdate As String
Dim originalPage As Integer
Dim currentPage As Integer
Dim latestPage As Integer
Dim lastArtistPage As Integer
Dim birthIndex As Integer
Dim firstPageArtist As Integer

indexMain = 2
cellIndexOutput = 1
Set f = ThisWorkbook.Worksheets("Print-Macro")

Do
Sheets("Print-Macro").Rows(cellIndexOutput).RowHeight = 15
Set artistNameCell = Sheets("Was").Cells(indexMain, 1)
If IsEmpty(artistNameCell.Value) Then
Exit Do
End If

'Code only gets here if a valid entry is found. So create the output now
artistName = artistNameCell.Value
birthdate = ""
deathdate = ""

If artistNameLast <> artistName Then
birthIndex = 2
Do
Dim tempName As String
tempName = Sheets("Geboren").Cells(birthIndex, 1).Value
If IsEmpty(Sheets("Geboren").Cells(birthIndex, 1).Value) Then
Exit Do
End If

If (tempName = artistName) Then
birthdate = Sheets("Geboren").Cells(birthIndex, 2).Value
deathdate = Sheets("Geboren").Cells(birthIndex, 3).Value
End If
birthIndex = birthIndex + 1
Loop

Sheets("Print-Macro").Range("A" & cellIndexOutput & ":" & "C" & cellIndexOutput).Merge
Sheets("Print-Macro").Cells(cellIndexOutput, 1).Value = artistName & " (" & birthdate & "-" & deathdate & ")"
Sheets("Print-Macro").Cells(cellIndexOutput, 1).Font.Underline = xlUnderlineStyleSingle
lastArtistPage = Sheets("Print-Macro").HPageBreaks.Count
firstPageArtist = cellIndexOutput
cellIndexOutput = cellIndexOutput + 1
Sheets("Print-Macro").Rows(cellIndexOutput).RowHeight = 15
End If

Sheets("Print-Macro").Rows(cellIndexOutput).RowHeight = 20

Sheets("Print-Macro").Cells(cellIndexOutput, 2).Value = Sheets("Was").Cells(indexMain, 2).Value
Sheets("Print-Macro").Cells(cellIndexOutput, 2).Font.Underline = xlUnderlineStyleNone

Sheets("Print-Macro").Cells(cellIndexOutput, 3).Value = Sheets("Was").Cells(indexMain, 3).Value
Sheets("Print-Macro").Cells(cellIndexOutput, 3).Font.Underline = xlUnderlineStyleNone

cellIndexOutput = cellIndexOutput + 1
Sheets("Print-Macro").Rows(cellIndexOutput).RowHeight = 15

Sheets("Print-Macro").Cells(cellIndexOutput, 2).Value = Sheets("Was").Cells(indexMain, 4).Value
Sheets("Print-Macro").Cells(cellIndexOutput, 2).Font.Underline = xlUnderlineStyleNone

Sheets("Print-Macro").Cells(cellIndexOutput, 3).Value = Sheets("Was").Cells(indexMain, 5).Value
Sheets("Print-Macro").Cells(cellIndexOutput, 3).Font.Underline = xlUnderlineStyleNone


' A page break happened in the last two lines it appears
If lastArtistPage <> Sheets("Print-Macro").HPageBreaks.Count Then
If cellIndexOutput = firstPageArtist + 2 Then
f.Rows(firstPageArtist).PageBreak = xlPageBreakManual
Else
Set f = ThisWorkbook.Worksheets("Print-Macro")
Dim lastBreak As Integer
lastBreak = f.HPageBreaks(f.HPageBreaks.Count).Location.Row
If lastBreak = cellIndexOutput Then
Sheets("Print-Macro").Range("A" & f.HPageBreaks(f.HPageBreaks.Count).Location.Row - 1).EntireRow.Insert
cellIndexOutput = cellIndexOutput + 1
End If

Sheets("Print-Macro").Range("A" & f.HPageBreaks(f.HPageBreaks.Count).Location.Row).EntireRow.Insert

f.Rows(lastBreak).PageBreak = xlPageBreakManual

Sheets("Print-Macro").Range("A" & lastBreak & ":" & "C" & lastBreak).Merge
Sheets("Print-Macro").Range("A" & lastBreak & ":" & "C" & lastBreak).Value = "Noch " & artistName
Sheets("Print-Macro").Range("A" & lastBreak & ":" & "C" & lastBreak).Font.Underline = xlUnderlineStyleSingle
cellIndexOutput = cellIndexOutput + 1
End If
End If

lastArtistPage = Sheets("Print-Macro").HPageBreaks.Count

For i = 1 To f.HPageBreaks.Count
Worksheets("Print-Macro").Cells(i, 4).Value = f.HPageBreaks(i).Location.Row
Next

latestPage = currentPage

cellIndexOutput = cellIndexOutput + 1
artistNameLast = artistName
indexMain = indexMain + 1
Loop
End Sub









share|improve this question


















  • 2





    Couple of comments for starters. You do a Do loop but then you put an If in there to exit the loop, why not just use a Do Until loop? Do Until IsEmpty(Sheets("Was").Cells(indexMain, 1)) Also I notice a lot of Integers being setup, use Long in VBA, there are well documented reasons why we don't use integers in VBA. Lastly, Dim sheet As Worksheet I wouldn't be using sheet as a variable name, use sht like everyone else or MySheet. Try to avoid using words that are used for Objects, Properties or Methods.

    – Dan Donoghue
    Nov 25 '18 at 21:43
















1















I have some code which takes data from 2 different sheets, and creates and output sheet. The problem is, if a new page in the data is created, it has to do a little additional information, and changes where the page break is. When I run the code with debugging active to see which line of code is causing the error, it works just fine.



When I let it run without debugging, it will give me an error that an index is out of bounds.



I have uploaded the excel sheet itself to https://www.dropbox.com/s/pcl5zwuna8g7wrf/Test.xlsm?dl=0 but I just don't understand why it results in different outputs depending on stepping through it or not, as it is single threaded anyway?



It happens when pressing the button on the fourth sheet.



I uploaded the code to pastebin at: https://pastebin.com/rMwi7c7G



Public Function SeitenNr(rngZelle As Range) As Integer
Dim wksHor As Integer, wksVert As Integer, SeiteNr As Integer
Dim VertPb As Object, HortPb As Object
Dim lngOrder As Long

lngOrder = rngZelle.Parent.PageSetup.Order
wksVert = rngZelle.Parent.VPageBreaks.Count + 1
wksHor = rngZelle.Parent.HPageBreaks.Count + 1

SeiteNr = 1
For Each VertPb In rngZelle.Parent.VPageBreaks
If VertPb.Location.Column > rngZelle.Column Then Exit For
SeiteNr = SeiteNr + IIf(lngOrder = xlDownThenOver, wksHor, 1)
Next VertPb
For Each HortPb In rngZelle.Parent.HPageBreaks
If HortPb.Location.Row > rngZelle.Row Then Exit For
SeiteNr = SeiteNr + IIf(lngOrder = xlDownThenOver, 1, wksVert)
Next HortPb
SeitenNr = SeiteNr
End Function

Sub updateOutput()
'Sheets("Print-Macro").UsedRange.ClearContents
'Sheets("Print-Macro").Cells.UnMerge
Application.DisplayAlerts = False
Sheets("Print-Macro").Delete
Application.DisplayAlerts = True
Dim sheet As Worksheet
Set sheet = Sheets.Add
sheet.Name = "Print-Macro"

Dim indexMain As Integer
Dim currentIndex As Integer

Dim artistName As String
Dim artistNameLast As String
Dim cellIndexOutput As Integer
Dim birthdate As String
Dim deathdate As String
Dim originalPage As Integer
Dim currentPage As Integer
Dim latestPage As Integer
Dim lastArtistPage As Integer
Dim birthIndex As Integer
Dim firstPageArtist As Integer

indexMain = 2
cellIndexOutput = 1
Set f = ThisWorkbook.Worksheets("Print-Macro")

Do
Sheets("Print-Macro").Rows(cellIndexOutput).RowHeight = 15
Set artistNameCell = Sheets("Was").Cells(indexMain, 1)
If IsEmpty(artistNameCell.Value) Then
Exit Do
End If

'Code only gets here if a valid entry is found. So create the output now
artistName = artistNameCell.Value
birthdate = ""
deathdate = ""

If artistNameLast <> artistName Then
birthIndex = 2
Do
Dim tempName As String
tempName = Sheets("Geboren").Cells(birthIndex, 1).Value
If IsEmpty(Sheets("Geboren").Cells(birthIndex, 1).Value) Then
Exit Do
End If

If (tempName = artistName) Then
birthdate = Sheets("Geboren").Cells(birthIndex, 2).Value
deathdate = Sheets("Geboren").Cells(birthIndex, 3).Value
End If
birthIndex = birthIndex + 1
Loop

Sheets("Print-Macro").Range("A" & cellIndexOutput & ":" & "C" & cellIndexOutput).Merge
Sheets("Print-Macro").Cells(cellIndexOutput, 1).Value = artistName & " (" & birthdate & "-" & deathdate & ")"
Sheets("Print-Macro").Cells(cellIndexOutput, 1).Font.Underline = xlUnderlineStyleSingle
lastArtistPage = Sheets("Print-Macro").HPageBreaks.Count
firstPageArtist = cellIndexOutput
cellIndexOutput = cellIndexOutput + 1
Sheets("Print-Macro").Rows(cellIndexOutput).RowHeight = 15
End If

Sheets("Print-Macro").Rows(cellIndexOutput).RowHeight = 20

Sheets("Print-Macro").Cells(cellIndexOutput, 2).Value = Sheets("Was").Cells(indexMain, 2).Value
Sheets("Print-Macro").Cells(cellIndexOutput, 2).Font.Underline = xlUnderlineStyleNone

Sheets("Print-Macro").Cells(cellIndexOutput, 3).Value = Sheets("Was").Cells(indexMain, 3).Value
Sheets("Print-Macro").Cells(cellIndexOutput, 3).Font.Underline = xlUnderlineStyleNone

cellIndexOutput = cellIndexOutput + 1
Sheets("Print-Macro").Rows(cellIndexOutput).RowHeight = 15

Sheets("Print-Macro").Cells(cellIndexOutput, 2).Value = Sheets("Was").Cells(indexMain, 4).Value
Sheets("Print-Macro").Cells(cellIndexOutput, 2).Font.Underline = xlUnderlineStyleNone

Sheets("Print-Macro").Cells(cellIndexOutput, 3).Value = Sheets("Was").Cells(indexMain, 5).Value
Sheets("Print-Macro").Cells(cellIndexOutput, 3).Font.Underline = xlUnderlineStyleNone


' A page break happened in the last two lines it appears
If lastArtistPage <> Sheets("Print-Macro").HPageBreaks.Count Then
If cellIndexOutput = firstPageArtist + 2 Then
f.Rows(firstPageArtist).PageBreak = xlPageBreakManual
Else
Set f = ThisWorkbook.Worksheets("Print-Macro")
Dim lastBreak As Integer
lastBreak = f.HPageBreaks(f.HPageBreaks.Count).Location.Row
If lastBreak = cellIndexOutput Then
Sheets("Print-Macro").Range("A" & f.HPageBreaks(f.HPageBreaks.Count).Location.Row - 1).EntireRow.Insert
cellIndexOutput = cellIndexOutput + 1
End If

Sheets("Print-Macro").Range("A" & f.HPageBreaks(f.HPageBreaks.Count).Location.Row).EntireRow.Insert

f.Rows(lastBreak).PageBreak = xlPageBreakManual

Sheets("Print-Macro").Range("A" & lastBreak & ":" & "C" & lastBreak).Merge
Sheets("Print-Macro").Range("A" & lastBreak & ":" & "C" & lastBreak).Value = "Noch " & artistName
Sheets("Print-Macro").Range("A" & lastBreak & ":" & "C" & lastBreak).Font.Underline = xlUnderlineStyleSingle
cellIndexOutput = cellIndexOutput + 1
End If
End If

lastArtistPage = Sheets("Print-Macro").HPageBreaks.Count

For i = 1 To f.HPageBreaks.Count
Worksheets("Print-Macro").Cells(i, 4).Value = f.HPageBreaks(i).Location.Row
Next

latestPage = currentPage

cellIndexOutput = cellIndexOutput + 1
artistNameLast = artistName
indexMain = indexMain + 1
Loop
End Sub









share|improve this question


















  • 2





    Couple of comments for starters. You do a Do loop but then you put an If in there to exit the loop, why not just use a Do Until loop? Do Until IsEmpty(Sheets("Was").Cells(indexMain, 1)) Also I notice a lot of Integers being setup, use Long in VBA, there are well documented reasons why we don't use integers in VBA. Lastly, Dim sheet As Worksheet I wouldn't be using sheet as a variable name, use sht like everyone else or MySheet. Try to avoid using words that are used for Objects, Properties or Methods.

    – Dan Donoghue
    Nov 25 '18 at 21:43














1












1








1








I have some code which takes data from 2 different sheets, and creates and output sheet. The problem is, if a new page in the data is created, it has to do a little additional information, and changes where the page break is. When I run the code with debugging active to see which line of code is causing the error, it works just fine.



When I let it run without debugging, it will give me an error that an index is out of bounds.



I have uploaded the excel sheet itself to https://www.dropbox.com/s/pcl5zwuna8g7wrf/Test.xlsm?dl=0 but I just don't understand why it results in different outputs depending on stepping through it or not, as it is single threaded anyway?



It happens when pressing the button on the fourth sheet.



I uploaded the code to pastebin at: https://pastebin.com/rMwi7c7G



Public Function SeitenNr(rngZelle As Range) As Integer
Dim wksHor As Integer, wksVert As Integer, SeiteNr As Integer
Dim VertPb As Object, HortPb As Object
Dim lngOrder As Long

lngOrder = rngZelle.Parent.PageSetup.Order
wksVert = rngZelle.Parent.VPageBreaks.Count + 1
wksHor = rngZelle.Parent.HPageBreaks.Count + 1

SeiteNr = 1
For Each VertPb In rngZelle.Parent.VPageBreaks
If VertPb.Location.Column > rngZelle.Column Then Exit For
SeiteNr = SeiteNr + IIf(lngOrder = xlDownThenOver, wksHor, 1)
Next VertPb
For Each HortPb In rngZelle.Parent.HPageBreaks
If HortPb.Location.Row > rngZelle.Row Then Exit For
SeiteNr = SeiteNr + IIf(lngOrder = xlDownThenOver, 1, wksVert)
Next HortPb
SeitenNr = SeiteNr
End Function

Sub updateOutput()
'Sheets("Print-Macro").UsedRange.ClearContents
'Sheets("Print-Macro").Cells.UnMerge
Application.DisplayAlerts = False
Sheets("Print-Macro").Delete
Application.DisplayAlerts = True
Dim sheet As Worksheet
Set sheet = Sheets.Add
sheet.Name = "Print-Macro"

Dim indexMain As Integer
Dim currentIndex As Integer

Dim artistName As String
Dim artistNameLast As String
Dim cellIndexOutput As Integer
Dim birthdate As String
Dim deathdate As String
Dim originalPage As Integer
Dim currentPage As Integer
Dim latestPage As Integer
Dim lastArtistPage As Integer
Dim birthIndex As Integer
Dim firstPageArtist As Integer

indexMain = 2
cellIndexOutput = 1
Set f = ThisWorkbook.Worksheets("Print-Macro")

Do
Sheets("Print-Macro").Rows(cellIndexOutput).RowHeight = 15
Set artistNameCell = Sheets("Was").Cells(indexMain, 1)
If IsEmpty(artistNameCell.Value) Then
Exit Do
End If

'Code only gets here if a valid entry is found. So create the output now
artistName = artistNameCell.Value
birthdate = ""
deathdate = ""

If artistNameLast <> artistName Then
birthIndex = 2
Do
Dim tempName As String
tempName = Sheets("Geboren").Cells(birthIndex, 1).Value
If IsEmpty(Sheets("Geboren").Cells(birthIndex, 1).Value) Then
Exit Do
End If

If (tempName = artistName) Then
birthdate = Sheets("Geboren").Cells(birthIndex, 2).Value
deathdate = Sheets("Geboren").Cells(birthIndex, 3).Value
End If
birthIndex = birthIndex + 1
Loop

Sheets("Print-Macro").Range("A" & cellIndexOutput & ":" & "C" & cellIndexOutput).Merge
Sheets("Print-Macro").Cells(cellIndexOutput, 1).Value = artistName & " (" & birthdate & "-" & deathdate & ")"
Sheets("Print-Macro").Cells(cellIndexOutput, 1).Font.Underline = xlUnderlineStyleSingle
lastArtistPage = Sheets("Print-Macro").HPageBreaks.Count
firstPageArtist = cellIndexOutput
cellIndexOutput = cellIndexOutput + 1
Sheets("Print-Macro").Rows(cellIndexOutput).RowHeight = 15
End If

Sheets("Print-Macro").Rows(cellIndexOutput).RowHeight = 20

Sheets("Print-Macro").Cells(cellIndexOutput, 2).Value = Sheets("Was").Cells(indexMain, 2).Value
Sheets("Print-Macro").Cells(cellIndexOutput, 2).Font.Underline = xlUnderlineStyleNone

Sheets("Print-Macro").Cells(cellIndexOutput, 3).Value = Sheets("Was").Cells(indexMain, 3).Value
Sheets("Print-Macro").Cells(cellIndexOutput, 3).Font.Underline = xlUnderlineStyleNone

cellIndexOutput = cellIndexOutput + 1
Sheets("Print-Macro").Rows(cellIndexOutput).RowHeight = 15

Sheets("Print-Macro").Cells(cellIndexOutput, 2).Value = Sheets("Was").Cells(indexMain, 4).Value
Sheets("Print-Macro").Cells(cellIndexOutput, 2).Font.Underline = xlUnderlineStyleNone

Sheets("Print-Macro").Cells(cellIndexOutput, 3).Value = Sheets("Was").Cells(indexMain, 5).Value
Sheets("Print-Macro").Cells(cellIndexOutput, 3).Font.Underline = xlUnderlineStyleNone


' A page break happened in the last two lines it appears
If lastArtistPage <> Sheets("Print-Macro").HPageBreaks.Count Then
If cellIndexOutput = firstPageArtist + 2 Then
f.Rows(firstPageArtist).PageBreak = xlPageBreakManual
Else
Set f = ThisWorkbook.Worksheets("Print-Macro")
Dim lastBreak As Integer
lastBreak = f.HPageBreaks(f.HPageBreaks.Count).Location.Row
If lastBreak = cellIndexOutput Then
Sheets("Print-Macro").Range("A" & f.HPageBreaks(f.HPageBreaks.Count).Location.Row - 1).EntireRow.Insert
cellIndexOutput = cellIndexOutput + 1
End If

Sheets("Print-Macro").Range("A" & f.HPageBreaks(f.HPageBreaks.Count).Location.Row).EntireRow.Insert

f.Rows(lastBreak).PageBreak = xlPageBreakManual

Sheets("Print-Macro").Range("A" & lastBreak & ":" & "C" & lastBreak).Merge
Sheets("Print-Macro").Range("A" & lastBreak & ":" & "C" & lastBreak).Value = "Noch " & artistName
Sheets("Print-Macro").Range("A" & lastBreak & ":" & "C" & lastBreak).Font.Underline = xlUnderlineStyleSingle
cellIndexOutput = cellIndexOutput + 1
End If
End If

lastArtistPage = Sheets("Print-Macro").HPageBreaks.Count

For i = 1 To f.HPageBreaks.Count
Worksheets("Print-Macro").Cells(i, 4).Value = f.HPageBreaks(i).Location.Row
Next

latestPage = currentPage

cellIndexOutput = cellIndexOutput + 1
artistNameLast = artistName
indexMain = indexMain + 1
Loop
End Sub









share|improve this question














I have some code which takes data from 2 different sheets, and creates and output sheet. The problem is, if a new page in the data is created, it has to do a little additional information, and changes where the page break is. When I run the code with debugging active to see which line of code is causing the error, it works just fine.



When I let it run without debugging, it will give me an error that an index is out of bounds.



I have uploaded the excel sheet itself to https://www.dropbox.com/s/pcl5zwuna8g7wrf/Test.xlsm?dl=0 but I just don't understand why it results in different outputs depending on stepping through it or not, as it is single threaded anyway?



It happens when pressing the button on the fourth sheet.



I uploaded the code to pastebin at: https://pastebin.com/rMwi7c7G



Public Function SeitenNr(rngZelle As Range) As Integer
Dim wksHor As Integer, wksVert As Integer, SeiteNr As Integer
Dim VertPb As Object, HortPb As Object
Dim lngOrder As Long

lngOrder = rngZelle.Parent.PageSetup.Order
wksVert = rngZelle.Parent.VPageBreaks.Count + 1
wksHor = rngZelle.Parent.HPageBreaks.Count + 1

SeiteNr = 1
For Each VertPb In rngZelle.Parent.VPageBreaks
If VertPb.Location.Column > rngZelle.Column Then Exit For
SeiteNr = SeiteNr + IIf(lngOrder = xlDownThenOver, wksHor, 1)
Next VertPb
For Each HortPb In rngZelle.Parent.HPageBreaks
If HortPb.Location.Row > rngZelle.Row Then Exit For
SeiteNr = SeiteNr + IIf(lngOrder = xlDownThenOver, 1, wksVert)
Next HortPb
SeitenNr = SeiteNr
End Function

Sub updateOutput()
'Sheets("Print-Macro").UsedRange.ClearContents
'Sheets("Print-Macro").Cells.UnMerge
Application.DisplayAlerts = False
Sheets("Print-Macro").Delete
Application.DisplayAlerts = True
Dim sheet As Worksheet
Set sheet = Sheets.Add
sheet.Name = "Print-Macro"

Dim indexMain As Integer
Dim currentIndex As Integer

Dim artistName As String
Dim artistNameLast As String
Dim cellIndexOutput As Integer
Dim birthdate As String
Dim deathdate As String
Dim originalPage As Integer
Dim currentPage As Integer
Dim latestPage As Integer
Dim lastArtistPage As Integer
Dim birthIndex As Integer
Dim firstPageArtist As Integer

indexMain = 2
cellIndexOutput = 1
Set f = ThisWorkbook.Worksheets("Print-Macro")

Do
Sheets("Print-Macro").Rows(cellIndexOutput).RowHeight = 15
Set artistNameCell = Sheets("Was").Cells(indexMain, 1)
If IsEmpty(artistNameCell.Value) Then
Exit Do
End If

'Code only gets here if a valid entry is found. So create the output now
artistName = artistNameCell.Value
birthdate = ""
deathdate = ""

If artistNameLast <> artistName Then
birthIndex = 2
Do
Dim tempName As String
tempName = Sheets("Geboren").Cells(birthIndex, 1).Value
If IsEmpty(Sheets("Geboren").Cells(birthIndex, 1).Value) Then
Exit Do
End If

If (tempName = artistName) Then
birthdate = Sheets("Geboren").Cells(birthIndex, 2).Value
deathdate = Sheets("Geboren").Cells(birthIndex, 3).Value
End If
birthIndex = birthIndex + 1
Loop

Sheets("Print-Macro").Range("A" & cellIndexOutput & ":" & "C" & cellIndexOutput).Merge
Sheets("Print-Macro").Cells(cellIndexOutput, 1).Value = artistName & " (" & birthdate & "-" & deathdate & ")"
Sheets("Print-Macro").Cells(cellIndexOutput, 1).Font.Underline = xlUnderlineStyleSingle
lastArtistPage = Sheets("Print-Macro").HPageBreaks.Count
firstPageArtist = cellIndexOutput
cellIndexOutput = cellIndexOutput + 1
Sheets("Print-Macro").Rows(cellIndexOutput).RowHeight = 15
End If

Sheets("Print-Macro").Rows(cellIndexOutput).RowHeight = 20

Sheets("Print-Macro").Cells(cellIndexOutput, 2).Value = Sheets("Was").Cells(indexMain, 2).Value
Sheets("Print-Macro").Cells(cellIndexOutput, 2).Font.Underline = xlUnderlineStyleNone

Sheets("Print-Macro").Cells(cellIndexOutput, 3).Value = Sheets("Was").Cells(indexMain, 3).Value
Sheets("Print-Macro").Cells(cellIndexOutput, 3).Font.Underline = xlUnderlineStyleNone

cellIndexOutput = cellIndexOutput + 1
Sheets("Print-Macro").Rows(cellIndexOutput).RowHeight = 15

Sheets("Print-Macro").Cells(cellIndexOutput, 2).Value = Sheets("Was").Cells(indexMain, 4).Value
Sheets("Print-Macro").Cells(cellIndexOutput, 2).Font.Underline = xlUnderlineStyleNone

Sheets("Print-Macro").Cells(cellIndexOutput, 3).Value = Sheets("Was").Cells(indexMain, 5).Value
Sheets("Print-Macro").Cells(cellIndexOutput, 3).Font.Underline = xlUnderlineStyleNone


' A page break happened in the last two lines it appears
If lastArtistPage <> Sheets("Print-Macro").HPageBreaks.Count Then
If cellIndexOutput = firstPageArtist + 2 Then
f.Rows(firstPageArtist).PageBreak = xlPageBreakManual
Else
Set f = ThisWorkbook.Worksheets("Print-Macro")
Dim lastBreak As Integer
lastBreak = f.HPageBreaks(f.HPageBreaks.Count).Location.Row
If lastBreak = cellIndexOutput Then
Sheets("Print-Macro").Range("A" & f.HPageBreaks(f.HPageBreaks.Count).Location.Row - 1).EntireRow.Insert
cellIndexOutput = cellIndexOutput + 1
End If

Sheets("Print-Macro").Range("A" & f.HPageBreaks(f.HPageBreaks.Count).Location.Row).EntireRow.Insert

f.Rows(lastBreak).PageBreak = xlPageBreakManual

Sheets("Print-Macro").Range("A" & lastBreak & ":" & "C" & lastBreak).Merge
Sheets("Print-Macro").Range("A" & lastBreak & ":" & "C" & lastBreak).Value = "Noch " & artistName
Sheets("Print-Macro").Range("A" & lastBreak & ":" & "C" & lastBreak).Font.Underline = xlUnderlineStyleSingle
cellIndexOutput = cellIndexOutput + 1
End If
End If

lastArtistPage = Sheets("Print-Macro").HPageBreaks.Count

For i = 1 To f.HPageBreaks.Count
Worksheets("Print-Macro").Cells(i, 4).Value = f.HPageBreaks(i).Location.Row
Next

latestPage = currentPage

cellIndexOutput = cellIndexOutput + 1
artistNameLast = artistName
indexMain = indexMain + 1
Loop
End Sub






excel vba






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 25 '18 at 21:13









SinisterMJSinisterMJ

2,1332138




2,1332138








  • 2





    Couple of comments for starters. You do a Do loop but then you put an If in there to exit the loop, why not just use a Do Until loop? Do Until IsEmpty(Sheets("Was").Cells(indexMain, 1)) Also I notice a lot of Integers being setup, use Long in VBA, there are well documented reasons why we don't use integers in VBA. Lastly, Dim sheet As Worksheet I wouldn't be using sheet as a variable name, use sht like everyone else or MySheet. Try to avoid using words that are used for Objects, Properties or Methods.

    – Dan Donoghue
    Nov 25 '18 at 21:43














  • 2





    Couple of comments for starters. You do a Do loop but then you put an If in there to exit the loop, why not just use a Do Until loop? Do Until IsEmpty(Sheets("Was").Cells(indexMain, 1)) Also I notice a lot of Integers being setup, use Long in VBA, there are well documented reasons why we don't use integers in VBA. Lastly, Dim sheet As Worksheet I wouldn't be using sheet as a variable name, use sht like everyone else or MySheet. Try to avoid using words that are used for Objects, Properties or Methods.

    – Dan Donoghue
    Nov 25 '18 at 21:43








2




2





Couple of comments for starters. You do a Do loop but then you put an If in there to exit the loop, why not just use a Do Until loop? Do Until IsEmpty(Sheets("Was").Cells(indexMain, 1)) Also I notice a lot of Integers being setup, use Long in VBA, there are well documented reasons why we don't use integers in VBA. Lastly, Dim sheet As Worksheet I wouldn't be using sheet as a variable name, use sht like everyone else or MySheet. Try to avoid using words that are used for Objects, Properties or Methods.

– Dan Donoghue
Nov 25 '18 at 21:43





Couple of comments for starters. You do a Do loop but then you put an If in there to exit the loop, why not just use a Do Until loop? Do Until IsEmpty(Sheets("Was").Cells(indexMain, 1)) Also I notice a lot of Integers being setup, use Long in VBA, there are well documented reasons why we don't use integers in VBA. Lastly, Dim sheet As Worksheet I wouldn't be using sheet as a variable name, use sht like everyone else or MySheet. Try to avoid using words that are used for Objects, Properties or Methods.

– Dan Donoghue
Nov 25 '18 at 21:43












2 Answers
2






active

oldest

votes


















4














This is a known Excel bug.



A workaround is to select a far enough cell, e.g. the bottom right one, before accessing the HPageBreaks collection:



Dim previousActiveCell As Range
Set previousActiveCell = ActiveCell
f.Cells(f.Rows.Count, f.Columns.Count).Activate
Dim lastBreak As Integer
lastBreak = f.HPageBreaks(f.HPageBreaks.Count).Location.Row
previousActiveCell.Activate





share|improve this answer































    0














    This is where it crashes:



    lastBreak = f.HPageBreaks(f.HPageBreaks.Count).Location.Row


    But it only does it when indexMain = 58



    After your first do loop starts straight after this line:



        Set artistNameCell = Sheets("Was").Cells(indexMain, 1)


    Put this:



        If indexMain = 58 Then Stop


    This will put the code into debug mode, then step through line by line with F8 and you will see it crash.



    If this doesn't give you enough direction to find the issue post back and I will dig further into your code.






    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53472041%2fvba-index-out-of-bounds-error-but-not-on-debugging%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      4














      This is a known Excel bug.



      A workaround is to select a far enough cell, e.g. the bottom right one, before accessing the HPageBreaks collection:



      Dim previousActiveCell As Range
      Set previousActiveCell = ActiveCell
      f.Cells(f.Rows.Count, f.Columns.Count).Activate
      Dim lastBreak As Integer
      lastBreak = f.HPageBreaks(f.HPageBreaks.Count).Location.Row
      previousActiveCell.Activate





      share|improve this answer




























        4














        This is a known Excel bug.



        A workaround is to select a far enough cell, e.g. the bottom right one, before accessing the HPageBreaks collection:



        Dim previousActiveCell As Range
        Set previousActiveCell = ActiveCell
        f.Cells(f.Rows.Count, f.Columns.Count).Activate
        Dim lastBreak As Integer
        lastBreak = f.HPageBreaks(f.HPageBreaks.Count).Location.Row
        previousActiveCell.Activate





        share|improve this answer


























          4












          4








          4







          This is a known Excel bug.



          A workaround is to select a far enough cell, e.g. the bottom right one, before accessing the HPageBreaks collection:



          Dim previousActiveCell As Range
          Set previousActiveCell = ActiveCell
          f.Cells(f.Rows.Count, f.Columns.Count).Activate
          Dim lastBreak As Integer
          lastBreak = f.HPageBreaks(f.HPageBreaks.Count).Location.Row
          previousActiveCell.Activate





          share|improve this answer













          This is a known Excel bug.



          A workaround is to select a far enough cell, e.g. the bottom right one, before accessing the HPageBreaks collection:



          Dim previousActiveCell As Range
          Set previousActiveCell = ActiveCell
          f.Cells(f.Rows.Count, f.Columns.Count).Activate
          Dim lastBreak As Integer
          lastBreak = f.HPageBreaks(f.HPageBreaks.Count).Location.Row
          previousActiveCell.Activate






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 25 '18 at 22:09









          PragmateekPragmateek

          9,23685488




          9,23685488

























              0














              This is where it crashes:



              lastBreak = f.HPageBreaks(f.HPageBreaks.Count).Location.Row


              But it only does it when indexMain = 58



              After your first do loop starts straight after this line:



                  Set artistNameCell = Sheets("Was").Cells(indexMain, 1)


              Put this:



                  If indexMain = 58 Then Stop


              This will put the code into debug mode, then step through line by line with F8 and you will see it crash.



              If this doesn't give you enough direction to find the issue post back and I will dig further into your code.






              share|improve this answer




























                0














                This is where it crashes:



                lastBreak = f.HPageBreaks(f.HPageBreaks.Count).Location.Row


                But it only does it when indexMain = 58



                After your first do loop starts straight after this line:



                    Set artistNameCell = Sheets("Was").Cells(indexMain, 1)


                Put this:



                    If indexMain = 58 Then Stop


                This will put the code into debug mode, then step through line by line with F8 and you will see it crash.



                If this doesn't give you enough direction to find the issue post back and I will dig further into your code.






                share|improve this answer


























                  0












                  0








                  0







                  This is where it crashes:



                  lastBreak = f.HPageBreaks(f.HPageBreaks.Count).Location.Row


                  But it only does it when indexMain = 58



                  After your first do loop starts straight after this line:



                      Set artistNameCell = Sheets("Was").Cells(indexMain, 1)


                  Put this:



                      If indexMain = 58 Then Stop


                  This will put the code into debug mode, then step through line by line with F8 and you will see it crash.



                  If this doesn't give you enough direction to find the issue post back and I will dig further into your code.






                  share|improve this answer













                  This is where it crashes:



                  lastBreak = f.HPageBreaks(f.HPageBreaks.Count).Location.Row


                  But it only does it when indexMain = 58



                  After your first do loop starts straight after this line:



                      Set artistNameCell = Sheets("Was").Cells(indexMain, 1)


                  Put this:



                      If indexMain = 58 Then Stop


                  This will put the code into debug mode, then step through line by line with F8 and you will see it crash.



                  If this doesn't give you enough direction to find the issue post back and I will dig further into your code.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 25 '18 at 21:56









                  Dan DonoghueDan Donoghue

                  4,6761631




                  4,6761631






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53472041%2fvba-index-out-of-bounds-error-but-not-on-debugging%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Contact image not getting when fetch all contact list from iPhone by CNContact

                      count number of partitions of a set with n elements into k subsets

                      A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks