The following code will format my template correctly the way I want. However, in the event the template is empty and a user hits the prep upload button on the sheet, I will receive an Overflow Error 6. Is there any way to remove what is causing this error?
Sub PrepForUpload()
Dim cel As Range, rng As Range
Set rng = Range("A2", Range("A65536").End(xlUp))
For Each cel In rng
If cel.Value = "" Then
If cel.Offset(, 2).Value = "" Then
cel.EntireRow.Delete
End If
End If
Next cel
Dim rowNumber As Integer
With Sheets("Initiatives")
If Len(.Cells(2, 1)) = 0 Then
rowNumber = .Cells(2, 1).End(xlDown).End(xlDown).Row + 1
Else: rowNumber = .Cells(2, 1).End(xlDown).Row + 1
End If
.Rows(rowNumber & ":" & .Rows.Count).Clear
End With
End Sub
Debug points to the following line as the issue:
rowNumber = .Cells(2, 1).End(xlDown).End(xlDown).Row + 1
Thanks
Ryan
You are getting an overflow because
Integerin VBA is a 16 bit signed number (max value of 32767). Regardless of the version of excel, you have a minimum of 65535 rows and most likely more if you are using the XLSX file format. You need to changerowNumberto aLongAnd you also have to code around the blank worksheet scenerio. When you call this line:
and the worksheet is blank,
.End(xlDown)will return the last possible row in the worksheet, which in the case of Excel 2010 (and Excel 2007) is 1048576. Once you changerowNumberto aLongyou will no longer get the overflow error, but you will run into a problem with this line:This is because you are trying to select a range (row 1048577) that does not exist (hence the type mismatch). You need to add a line of code to work around this scenario. Either check initially for a blank worksheet, or check for row > 1048576.
The simplest thing to do is to just add a line to check this: