This is my first vba project so please be patient. I have created an Excel Userform to add info to a database. When I click "add" I have vba script that should find the first open worksheet for that year (ex. Name_2018_1, Name_2018_2, Name_2018_3) or create a new consecutively named worksheet.
I keep receiving Compile Errors based on where I place my "then" and "exit for" statements.
I want the counter to either find the first worksheet with an available row or loop all the way through i and then make a new sheet based on the i value.
How do I correctly set up a for loop after my else? If anyone could point out what I am doing wrong it would be appreciated.
'Dim myFirstBlankRow As Long
'Dim i As Long
'Dim WsName As String
'Dim WsNamei As String
'Dim ws As Worksheet
'Dim counter As Integer
'Declare worksheet names
WsName = "Name_" & Year(Me.DTPicker1.value) & "*"
WsNamei = "Name_" & Year(Me.DTPicker1.value) & "_" & i
For Each ws In Sheets
If ws.Name = WsName Or ws.Name Like WsName & "*" Then i = i + 1
Next
'If no worksheet exists, then make a new one, else loop through until an empty row is found
If i = 0 Then 'No worksheet with the year selected by DTPicker
With Sheets("Template")
.Copy After:=Sheets(Sheets.Count) 'Add new worksheet
ActiveSheet.Name = WsName & "_1"
End With
*If I place the "Then Exit For" on one line I receive a Compile Error "Else without If"
Else:
For counter = 1 To i Step 1
Worksheets(WsName & "_" & counter).Activate
If IsEmpty(Range("A1048576").value) = True Then Exit For
Next counter
Else 'No recognizing the original If statement
With Sheets("Template")
.Copy After:=Sheets(Sheets.Count) 'Add worksheet
ActiveSheet.Name = WsName & "_" & i
End With
End If
*However, when I place the "Then" & "Exit For" on separate lines I receive a Compile Error"Next without For"
Else:
For counter = 1 To i Step 1
Worksheets(WsName & "_" & counter).Activate
If IsEmpty(Range("A1048576").value) = True Then
Exit For
Next counter
Else
With Sheets("Template")
.Copy After:=Sheets(Sheets.Count) 'Add worksheet
ActiveSheet.Name = WsName & "_" & i
End With
End If
'Added for my modifications I want the code to (1st) check for an original worksheet and make one if there is not. (2nd) If that original worksheet exists but is full, I want to make sure there is not already a new worksheet. If there is it becomes the active sheet. (3rd) if there is not another available worksheet I need the script to make one.
I was originally having issues with a new worksheet being created and only one entry being applied to that worksheet prior to another worksheet being created, hence the 3 steps.
Should I split off the first If and then have the last 2 as their own entity for if the first worksheet is full?
If i = 0 Then 'No worksheet with the year selected by DTPicker
With Sheets("Template")
.Copy After:=Sheets(Sheets.Count) 'Add worksheet
i = 1
ActiveSheet.Name = WsName & "_" & i
End With
ElseIf
For counter = 1 To i Step 1
Worksheets(WsName & "_" & counter).Activate
If IsEmpty(Range("A1048576").value) = True Then
Exit For
End If
Next counter
Else
With Sheets("Template")
.Copy After:=Sheets(Sheets.Count) 'Add worksheet
ActiveSheet.Name = WsName & "_" & i
End With
End If
Else:should beElseIf- you have 2Elses in yourIfstatement.If / Else / Else / Else / ... / End If. You needIf / ElseIf / ElseIf / End If. So for your finalElse, when would that trigger? Ifi <> 0? or is that the secondElse, and you want to fire that final one regardless whatiis?Select Caseinstead ofIfwhen there are multiple conditions.