Compile Error: Sub or Function not Defined - GoTo

SBF12345

Well-known Member
Joined
Jul 26, 2014
Messages
614
Greetings,

This is what I'm working with...

Code:
If Range(Cells(k, b)) <> "" Then GoTo First Else GoTo Second
    
    First
    
        Set h = Range(Cells(k, k.End(xlToRight)))
        k = k - 1
        
        End If
        
        Loop
          
          
    Second
    
        h.Copy
        Range.Cells(k, b).PasteSpecial Paste:=xlPasteValues
        
        k = k - 1
        
        End If
        
        Loop

I am getting the compile error at "First"

How can I reference these two GoTo chunks properly?

Thanks!
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
You can't have a Goto...Else in VBA, not sure I've seen that in any language, though I could be wrong.

What is it you are trying to do with this piece of code?
 
Upvote 0
You can't have a Goto...Else in VBA,
Actually, there is nothing wrong with this line of code (so long as the variable k and b are assigned positive, non-zero integer values in the lead-in code the OP did not show us)...

If Range(Cells(k, b)) <> "" Then GoTo First Else GoTo Second

The reason the OP is getting the error he reports is because his First and Second "statements" later in the code are missing the trailing colon that would make them Labels so that the GoTo statements had targets to actually go to. As written, VB thinks those lines of code are calls to subroutines. Of course, adding the colons to make them proper Labels will only unveil the other errors in the OP's code. Those free-floating Loop statements have no corresponding Do statements. Of course, the OP only showed us a snippet of code, so the Do statement might be in the lead-in code he did not show us; however, he will still get an error because each Do statement can only have one Loop statement to close it off and the OP looks like he is trying to close it off with whichever Label the If statement send execution to, which, of course, you cannot do.
 
Last edited:
Upvote 0
Rick

I realised the problem with the Goto was the labels, just got a bit confused by the overall syntax/structure.

I tried re-writing it with If...Else...End If but the Loop/k=k-1 part threw me off.
 
Upvote 0

Forum statistics

Threads
1,223,248
Messages
6,171,021
Members
452,374
Latest member
keccles

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top