VBA to Goto column reference number pre-typed in cell A1

omairhe

Well-known Member
Joined
Mar 26, 2009
Messages
2,040
Office Version
  1. 2019
Platform
  1. Windows
Hey everyone, hope all is good,

I need a VBA code that would run on a worksheet when it gets selected.

The user will select a sheet and right after it is selected the code must check the value in cell A1 of sheet named= "Hosting". The value is going to be numerical, for example 1, 2, 3 , 4 and so on. If value is 3 then take the user to column no. 3 which is column C of the selected sheet. If value is 9 then take me to column 9 which is column I in the newly selected sheet.

My workbook consists of multiple worksheets so perhaps a Workbook code will be a better option, instead of inserting the code in each of the sheets?

Thank you, help on the matter will be highly appreciated.
 

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.
How about
Code:
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
   Sh.Cells(1, Sheets("Hosting").Range("A1").Value).Activate
End Sub
 
Upvote 0
How about
Code:
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
   Sh.Cells(1, Sheets("Hosting").Range("A1").Value).Activate
End Sub


Dear Fluff,

Thank you for the help. Also the row number should remain undisturbed. meaning if cell value A1 = 5
and new sheet was at B16 , the code should now take me to E16, not E1

Sorry that I couldn't make that clear in my original post.
 
Last edited:
Upvote 0
How about
Code:
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
   Sh.Cells(ActiveCell.Row, Sheets("today").Range("A1").Value).Activate
End Sub
 
Upvote 0
How about
Code:
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
   Sh.Cells(ActiveCell.Row, Sheets("today").Range("A1").Value).Activate
End Sub

Great. Thanks again.
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,907
Messages
6,122,183
Members
449,071
Latest member
cdnMech

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