define a label

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

It may sound basics, put I need help to define a label...

I try the following codes and it alwas get an error of label not define.

Dim ws As Worksheet
Set ws = Worksheets("data2")
If WorksheetFunction.Count(ws.Cells) = 0 Then GoTo labelNameHere
 
from the help file:

GoTo Statement Example
This example uses the GoTo statement to branch to line labels within a
procedure.

Sub GotoStatementDemo()
Dim Number, MyString
Number = 1 ' Initialize variable.
' Evaluate Number and branch to appropriate label.
If Number = 1 Then GoTo Line1 Else GoTo Line2

Line1:
MyString = "Number equals 1"
GoTo LastLine ' Go to LastLine.
Line2:
' The following statement never gets executed.
MyString = "Number equals 2"
LastLine:
Debug.Print MyString ' Print "Number equals 1" in
' the Immediate window.
End Sub
 
You have to have some line in your code that looks like a label:


Dim ws As Worksheet
Set ws = Worksheets("data2")
If WorksheetFunction.Count(ws.Cells) = 0 Then GoTo labelNameHere
'do some stuff

labelnamehere:
'do different stuff

======
But I think most would say that using labels may not be the best way to approach
it.

Can't you just write your if statement something like:

Dim ws As Worksheet
Set ws = Worksheets("data2")
If WorksheetFunction.Count(ws.Cells) = 0 Then
'do nothing
else
'do some stuff
end if

'do different stuff
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top