Multiple If statements in one macro

sobeleg

New Member
Joined
Jun 29, 2011
Messages
40
I am try to make it so when a certain number is entered into a box, and a button is clicked, it will take you to a different place depending on what number was entered. I keep getting various errors when writing this in Visual Basic such as "Block If without End If," :End if withou block if" Else without if. and "Run-time error '13': type mismatch."

As of now this is what I have:

Sub Testbutton()
'
' If "D8"=011 Then
' Application.Goto Reference:="R28C1"
' If "D8"=701 Then
' Application.Goto Reference:="R86C1"
' End If
End Sub

Any help would be greatly appreciated.
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Try:-
Code:
Sub Testbutton()
 
   If range("D8").value=011 Then Application.Goto Reference:="R28C1"
   If range("D8").value=701 Then Application.Goto Reference:="R86C1"
 
End Sub
 
Upvote 0
Or:-
Code:
Sub Testbutton()
 
   select case range("D8").value
      case 011 
          Application.Goto Reference:="R28C1"
      case 701 
          Application.Goto Reference:="R86C1"
      case else
[COLOR=green]          ' do nothing?[/COLOR]
   end select
 
End Sub
 
Upvote 0
Try this :

Code:
Sub Testbutton()

 If Range("D8").Value = 11 Then
   Application.Goto Reference:="R28C1"
 ElseIf Range("D8").Value = 701 Then
   Application.Goto Reference:="R86C1"
 End If
End Sub
 
Upvote 0
Code:
 If Range("D8") = 11 Then Application.Goto Reference:="R28C1"
 If Range("D8") = 701 Then Application.Goto Reference:="R86C1"
or
Code:
Select Case Range("D8").Value
    Case 11: Application.Goto Reference:="R28C1"
    Case 701: Application.Goto Reference:="R86C1"
    Case Else
    MsgBox "Dunno where to go…"
End Select
edit post posting: bleedin' 'ell, I take a 'phone call and everyone piles in!
 
Last edited:
Upvote 0
One more quick question, if I wanted it to take me to a place on a different sheet, how would I have to change the goto part of it?

Thanks
 
Upvote 0
For your info, for each "IF" there must be a corresponding "Endif" easiest way to ensure you do this is when you start your code, if you know your are doing an if statement type it otu first before you put your actual code in

like


if
endif

if
elseif
elseif
endif

if
end if
if
end if

and then put your code in. Also indenting helps to make sure you have matching start and end statments.
 
Upvote 0
One more quick question, if I wanted it to take me to a place on a different sheet, how would I have to change the goto part of it?

Thanks
Application.Goto Reference:="Sheet2!R51C3"
or
Application.Goto Reference:=Sheets("Sheet2").Range("D22")
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,564
Messages
6,179,543
Members
452,924
Latest member
JackiG

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