I want to go to specific cell when a worksheet is selected
Private Sub Worksheet_Cellselection()
ActiveSheet Goto:="D5"
End Sub
That one does not work
You need to use the Worksheet_Activate event of the relevant worksheet:
Private Sub Worksheet_Activate()
Range("D5").Select
End Sub
To use for several worksheets in a Workbook, move the code to Workbook_SheetActivate evenrt (inside the Workbook level):
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
Select Case Sh.Name
Case "Sheet1", "Sheet2", "Sheet4" '<-- run it only for sheet's with these names
Range("D5").Select
End Select
End Sub
Application.Goto ActiveSheet.Range("D5")
Related
I have this VBA written. Essentially what it does is; I've used 'Define Range' ("Test") on a few cells in a worksheet. The VBA makes sure that whenever the workbook is opened, it automatically zooms in to fit that range. Right now, I think it only triggers when the workbook is opened, and it gives an error message if that worksheet is not selected when the workbook is opened. Now, I would like it to run only if the specific worksheet is selected. I've literally copied it from someone else so don't really know how I would amend this...
Thanks in advance!
Private Sub Workbook_Open()
Range("Test").Select
ActiveWindow.Zoom = True
Cells(1, 1).Select
End Sub
Zoom & Scroll To Cell When Worksheet Is Activated...
... and if the worksheet is active when the workbook is opened.
It is assumed that the range is of Workbook scope.
ThisWorkbook Module
Option Explicit
Private Sub Workbook_Open()
Const WorksheetName As String = "Sheet1"
If ActiveSheet.Name = WorksheetName Then SelectMyRange
End Sub
Sheet Module, e.g. Sheet1...
... where Sheet1 is the code name i.e. the name not in parentheses, e.g. Sheet1(Data), seen in the VBE Project Explorer window.
Option Explicit
Private Sub Worksheet_Activate()
SelectMyRange
End Sub
Standard Module, e.g. Module1...
... created by using Insert Module.
Option Explicit
Sub SelectMyRange()
Const RangeName As String = "Test"
With Range(RangeName)
.Select
ActiveWindow.Zoom = True
Application.Goto .Cells(1), True
End With
End Sub
I created this VBA that linked to a comboBox which is on the main worksheet called "ActiveX". This comboBox populates the name of each existing worksheet automatically even if worksheet names are changed, worksheets are added or deleted. When a dropdown item of the combobox representing a worksheet is selected, the user is taken to the selected worksheet.
What I am working on, is trying to add to this:
Once on the selected worksheet, I want to add a button that takes me back to the main worksheet called “ActiveX”
Hide all worksheets apart from the selected worksheet and the worksheet called “activeX” (which is the main menu worksheet)
Since additional worksheets can occasionally be added or removed by the user, is it possible to automatically insert a button to the selected worksheet only. Instead of having to add a button per worksheet?
I am not proefficient at creating VBA code so I try to copy, amend and adapt from sites like this one.
`Private Sub cbSheet_Change()
If cbSheet.Value <> "Select Item" Then
Worksheets(cbSheet.Value).Select
End If
cbSheet.Value = "Select Item"
End Sub
Private Sub Worksheet_Activate()
Dim Sh As Worksheet
Me.cbSheet.Clear
For Each Sh In ThisWorkbook.Worksheets
Me.cbSheet.AddItem Sh.Name
Next Sh
End Sub
Hide and Unhide sheets
Sub Hide_SH()
Dim sh As Worksheet
For Each sh In Sheets
If sh.Name <> "ActiveX" Then sh.Visible = False
Next sh
End Sub
Sub UnHide_SH()
Dim sh As Worksheet
For Each sh In Sheets
sh.Visible = True
Next sh
End Sub
You can add a hyperlink to the selected sheet.
Private Sub cbSheet_Change()
Dim ws As Worksheet
If cbSheet.Value <> "Select Item" Then
Set ws = Sheets(Me.cbSheet.Value)
With ws
.Visible = True
.Select
.Hyperlinks.Add Anchor:=.Range("A1"), Address:="", SubAddress:= _
"ActiveX!A1", TextToDisplay:="ActiveX!A1"
End With
End If
cbSheet.Value = "Select Item"
End Sub
I have a combobox with 9 sheetnames.
When I select a name the macro finds the sheet.
However, I cant copy a range to my active worksheet,
its gives the error 438 on the row "wb.blad.CodeName.Range("A1:J80").Select"
All the worksheets have a codename.
I can't find the solution. Here is mij code;
Private Sub discipline_Change()
blad = databaas.discipline.Text
Set wb = Workbooks.Open("C:\Users\Genius\Desktop\db.xlsx")
'wb.Worksheets.blad.Range("B1:J80").Copy sh2.Range("B1")
For Each ws In wb.Worksheets
If ws.CodeName = blad Then
wb.blad.CodeName.Range("A1:J80").Select
Selection.Copy
sh2.Range("A1").Select
sh2.Paste
End If
Next
ActiveWindow.Close
Unload Me
End Sub
wb.blad.CodeName.Range("A1:J80").Select doesn't make sense.
Use ws.Range("A1:J80").Select
Suppose I have a workbook with two worksheets: Sheet1 and Sheet2. I want a message to appear when a user goes from Sheet2 back to Sheet1.
I'm not sure how to approach it - so far I've only been meddling with Worksheet_Change sub, but the problem doesn't seem like something that could be solved inside that sub. Right now, I can only think of setting some global variable
Dim previousWorksheet As Variant
Set previousWorksheet = ActiveSheet.Name
And then checking what sheet is the active one:
If previousWorksheet = "Sheet2" And ActiveSheet.Name = "Sheet1" Then
MsgBox("DETECTED")
End If
But what would trigger that code, I don't know.
What is the best way to accomplish it?
This is what worked for me (inside ThisWorkbook module):
Option Explicit
Dim previousWorksheet As String
Private Sub Workbook_open()
previousWorksheet = ActiveSheet.Name
End Sub
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
MsgBox ("changed")
If ActiveSheet.Name = "Sheet1" And previousWorksheet = "Sheet2" Then
MsgBox ("the switch")
End If
previousWorksheet = ActiveSheet.Name
End Sub
Suppose I have a workbook with two worksheets: Sheet1 and Sheet2
then to get a message
when a user goes from Sheet2 back to Sheet1
you could simply go:
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
If Sh.Name = "Sheet1" Then MsgBox "The switch"
End Sub
I cannot seem to get a combobox to work. I'm trying to add a dropdown combobox that displays all worksheets in my workbook.
My first step was to create a combobox which adds the sheets but the box does not add any sheetnames
Here is my code:
Sub ComboBox1_Change()
Dim WS As Worksheet
For Each WS In Worksheets
ComboBox1.AddItem (WS.Name)
Next WS
End Sub
The problem is that you add items through the Event 'Change'. If there is no change in the combobox (because there aren't any items) this event never fires.
Instead you can add your code to the Initialize or Activate Event of your Form. For example:
Private Sub UserForm_Activate()
Dim WS As Worksheet
For Each WS In Worksheets
ComboBox1.AddItem (WS.Name)
Next WS
End Sub
This will proper result
Private Sub UserForm_Initialize()
Dim ws As Worksheet
For Each ws In Worksheets
If ws.Name <> "Main" Then
Me.ComboBoxpgname.AddItem ws.Name
End If
Next ws
End Sub