Hi all,
I'm an absolute beginner in vba (started two weeks ago). In short, can you tell me why the code always skips the part in red font and goes straight from the "Then" statement in blue to the "Else" statement in blue, irrespective of whether the statement If Sheet.Name = UCase(szToday) is True or False? The code is supposed to copy the sheet Accounts and rename it with today's date ("ddmmm"), but before that happens the code is supposed to go through all the sheets in the workbook and check if any of the sheets' name is today's date ("ddmmm" - the sheet Accounts has therefore aldready been copied and renamed). If the statement Sheet.Name = UCase(szToday) is true, then the MsgBox should appear and ask the user if he wants to make another copy or not (without renaming it though). The code below this one does the same thing and works fine, in case you want to see how it actually works.
Sub CopySht()
Dim szToday As String
szToday = Format(Date, "ddmmm")
For Each Sheet In Worksheets
If Sheet.Name = UCase(szToday) Then
If MsgBox("Copied already! Do you want another?", vbYesNo + vbQuestion) = vbYes Then
Sheets("Accounts").Select
Sheets("Accounts").Copy After:=Sheets(Sheets.Count)
Exit Sub
Else
Worksheets(1).Select
Exit Sub
End If
Else
Sheets("Accounts").Select
Sheets("Accounts").Copy After:=Sheets(Sheets.Count)
ActiveSheet.Name = UCase(szToday)
Exit Sub
End If
Next Sheet
End Sub
The following code performs the action I was looking to do with the code above (run it twice and you see what I mean). However, the book I'm reading (John Walkenbach's Excel Power Programming with VBA) states that I should refrain from using a GoTo statement unless that's the only way to perform an action.
Sub CopySht()
Dim szToday As String
szToday = Format(Date, "ddmmm")
For Each Sheet In Worksheets
If Sheet.Name = UCase(szToday) Then GoTo AnsBox
Next Sheet
Sheets("Accounts").Select
Sheets("Accounts").Copy After:=Sheets(Sheets.Count)
ActiveSheet.Name = UCase(szToday)
Exit Sub
AnsBox:
If MsgBox("Copied already! Do you want another?", vbYesNo + vbQuestion) = vbYes Then
Sheets("Accounts").Select
Sheets("Accounts").Copy After:=Sheets(Sheets.Count)
Exit Sub
Else
Worksheets(1).Select
Exit Sub
End If
End Sub
Many thanks in advance. One more thing, in case it matters. I'm not a native speaker of English, so take that into account if some of the things I write look awkward.
Thanks,
Stefan
I'm an absolute beginner in vba (started two weeks ago). In short, can you tell me why the code always skips the part in red font and goes straight from the "Then" statement in blue to the "Else" statement in blue, irrespective of whether the statement If Sheet.Name = UCase(szToday) is True or False? The code is supposed to copy the sheet Accounts and rename it with today's date ("ddmmm"), but before that happens the code is supposed to go through all the sheets in the workbook and check if any of the sheets' name is today's date ("ddmmm" - the sheet Accounts has therefore aldready been copied and renamed). If the statement Sheet.Name = UCase(szToday) is true, then the MsgBox should appear and ask the user if he wants to make another copy or not (without renaming it though). The code below this one does the same thing and works fine, in case you want to see how it actually works.
Sub CopySht()
Dim szToday As String
szToday = Format(Date, "ddmmm")
For Each Sheet In Worksheets
If Sheet.Name = UCase(szToday) Then
If MsgBox("Copied already! Do you want another?", vbYesNo + vbQuestion) = vbYes Then
Sheets("Accounts").Select
Sheets("Accounts").Copy After:=Sheets(Sheets.Count)
Exit Sub
Else
Worksheets(1).Select
Exit Sub
End If
Else
Sheets("Accounts").Select
Sheets("Accounts").Copy After:=Sheets(Sheets.Count)
ActiveSheet.Name = UCase(szToday)
Exit Sub
End If
Next Sheet
End Sub
The following code performs the action I was looking to do with the code above (run it twice and you see what I mean). However, the book I'm reading (John Walkenbach's Excel Power Programming with VBA) states that I should refrain from using a GoTo statement unless that's the only way to perform an action.
Sub CopySht()
Dim szToday As String
szToday = Format(Date, "ddmmm")
For Each Sheet In Worksheets
If Sheet.Name = UCase(szToday) Then GoTo AnsBox
Next Sheet
Sheets("Accounts").Select
Sheets("Accounts").Copy After:=Sheets(Sheets.Count)
ActiveSheet.Name = UCase(szToday)
Exit Sub
AnsBox:
If MsgBox("Copied already! Do you want another?", vbYesNo + vbQuestion) = vbYes Then
Sheets("Accounts").Select
Sheets("Accounts").Copy After:=Sheets(Sheets.Count)
Exit Sub
Else
Worksheets(1).Select
Exit Sub
End If
End Sub
Many thanks in advance. One more thing, in case it matters. I'm not a native speaker of English, so take that into account if some of the things I write look awkward.
Thanks,
Stefan