Please help explain this code

moshin123

New Member
Joined
Jan 23, 2017
Messages
7
Can someone explain what each line of code does? I program in C++ as a living, but i haven't learnt VBA. I learn code by explaining code, so please explain.

sSheet = InputBox( _
Prompt:="Sheet name or number?", _
Title:="Input Sheet")
On Error Resume Next
If Val(sSheet) > 0 Then
Worksheets(Val(sSheet)).Activate
Else
Worksheets(sSheet).Activate
End If
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Hello Moshin,

I assume you are familiar with IF Statements, so I wont explain those. But each line is commented below with what it's doing.

Code:
Sub Explain()


sSheet = InputBox(Prompt:="Sheet name or number?", Title:="Input Sheet")
[COLOR=#ff0000]' Pop Up box for user to input a sheet number
' Number entered is stored in variable sSheet[/COLOR]


On Error Resume Next
[COLOR=#ff0000]'If the user enters a sheet number that doesnt exist, ignore error[/COLOR]


If Val(sSheet) > 0 Then
[COLOR=#ff0000]'If the user inputted sheet number is greater than 0[/COLOR]
    Worksheets(Val(sSheet)).Activate
[COLOR=#ff0000]    'Val() returns the first part of the input regonised as a number
    'eg Val("2 or 3") will return '2'
    'Activate the Sheet with that number[/COLOR]
Else
    Worksheets(sSheet).Activate
 [COLOR=#ff0000]   'Activate the Sheet with that number[/COLOR]
End If


End Sub

Hope that helps
Caleeco
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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