How make Excel select the same cell when changing sheets? - vba

I have Excel workbook with 3 sheets. I want to use a macro which will select the same cell when changing sheets.
Example:
I am in sheet1 cell A3 when I switch to sheet2. I want A3 in sheet2 to be selected. Same thing when I switch to sheet3.
Is it possible?
I tried using events sheet_activate, sheet_deactivate, and sheet_change. The last one is surely wrong.

You were close. This uses a module-level variable to store the ActiveCell address any time the SheetSelectionChange event fires:
Dim ActiveCellAddress As String
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
Application.ScreenUpdating = False
Sh.Range(ActiveCellAddress).Activate
Application.ScreenUpdating = True
End Sub
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
ActiveCellAddress = ActiveCell.Address
End Sub

Here is a one-way example. If you start on Sheet1 and select either Sheet2 or Sheet3, you will stay on the same address as you were on Sheet1.
In a standard module, include the single line:
Public addy As String
In the Sheet1 code area, include the following event macro:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
addy = ActiveCell.Address
End Sub
In both the Sheet2 and Sheet3 code areas, include the following event macro:
Private Sub Worksheet_Activate()
If addy <> "" Then
Range(addy).Select
End If
End Sub

I use the following macro to select cell A1 on all sheets within a workbook. I assigned this macro to a button on a toolbar. You can modify it to make it work for when you change sheets.
Sub Select_Cell_A1_on_all_Sheets()
Application.ScreenUpdating = False
On Error Resume Next
Dim J As Integer
Dim NumSheets As Integer
Dim SheetName As String
CurrentSheetName = ActiveSheet.Name
NumSheets = Sheets.Count
For J = 1 To NumSheets
SheetName = Sheets(J).Name
Worksheets(SheetName).Activate
Range("A1").Select
Next J
Worksheets(CurrentSheetName).Activate

Related

Excel vba when adding or deleting sheet in workbook, show/hide button in main sheet

I have a button with a simple macro that deletes certain sheets.
I'd like to show this button only when those sheets are actually there (I can use worksheets.count because I have 2 "permanent" sheets; if > 2 then I know I have a new sheet and I want to show the button to delete it if I want to).
I think I have to use "Workbook.SheetChange event" because "Worksheet.Change event" doesn't seem to work for me in this case.
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Dim foglio_parametri As Worksheet
Set foglio_parametri = ThisWorkbook.Worksheets("PARAMETRI") 'my main sheet where I want to show/hide the button
Application.ScreenUpdating = True
If Application.Worksheets.Count > 2 Then
foglio_parametri.CommandButton2.Visible = True
Else
foglio_parametri.CommandButton2.Visible = False
End If
End Sub
Thank you very much for your time.
I will not use your names as they are in a foreign language I do not understand .
Let's assume the button you are talking about is in a sheet with the name sheet3 which also has the codename sheet3. The button itself has the name CommandButton1. Let's further assume the certain sheets you are talking about have the names sheet4 and sheet5 then I would add the following code to the workbook module
Option Explicit
Private Sub Workbook_Open()
Sheet3.HidecmdBtn
End Sub
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
If Sh.Name = "Sheet3" Then
Sheet3.HidecmdBtn
End If
End Sub
In the worksheet module of sheet3 you have the following code
Option Explicit
Private Sub CommandButton1_Click()
' Your code goes here
' In case your code deletes the sheets you have to hide the button
HidecmdBtn
End Sub
Sub HidecmdBtn()
Dim Sh As CommandButton
' My button is located on sheet 3 and has the name "CommandButton1"
Set Sh = CommandButton1
Dim sh1Name As String
Dim sh2Name As String
sh1Name = "Sheet4"
sh2Name = "Sheet5"
If SheetExists(sh1Name) Or SheetExists(sh2Name) Then
Sh.Visible = msoTrue
Else
Sh.Visible = msoFalse
End If
End Sub
In a normal module you have
Public Function SheetExists(SheetName As String, Optional wrkBook As Workbook) As Boolean
If wrkBook Is Nothing Then
Set wrkBook = ActiveWorkbook 'or ThisWorkbook - whichever appropriate
End If
Dim obj As Object
On Error GoTo HandleError
Set obj = wrkBook.Sheets(SheetName)
SheetExists = True
Exit Function
HandleError:
SheetExists = False
End Function

Automatically open an excel file to cell A1 in all worksheets (using VBA)

I am trying to write VBA code so that whenever I open any file in excel, it automatically goes to Cell A1 in all sheets (no matter what cells were selected when it was last saved). I found something online that suggested putting the following code in my Personal .xlsb project:
Sub kTest()
Dim i As Long, s() As String, a As String, n As Long
With ActiveWorkbook
For i = 1 To .Worksheets.Count
a = a & .Worksheets(i).Name
n = n + 1
ReDim Preserve s(1 To n)
s(n) = .Worksheets(i).Name
If Len(a) > 224 Then
.Worksheets(s).Select
.Worksheets(s(1)).Activate
[a1].Select
n = 0: a = "": Erase s
End If
Next
If Len(a) Then
.Worksheets(s).Select
.Worksheets(s(1)).Activate
[a1].Select
End If
Application.Goto .Worksheets(1).Range("a1")
End With
End Sub
But nothing happens when I open a file. Please help!
You cannot go to Cell A1 in every sheet. But if you would like to go to Cell A1 of a single sheet you could do the following.
Create a class ExcelEvents with the following code
Option Explicit
Private WithEvents App As Application
Private Sub App_WorkbookOpen(ByVal Wb As Workbook)
App.Goto Wb.Worksheets(1).Range("A1")
End Sub
Private Sub Class_Initialize()
Set App = Application
End Sub
And in ThisWorkbook add
Option Explicit
Private xlApp As ExcelEvents
Private Sub Workbook_Open()
Set xlApp = New ExcelEvents
End Sub
Save the workbook, re-open it and the code in the workbook_open event will run and that means as soon as you open another workbook the code will goto cell A1 of sheet 1
EDIT If you really mean to select A1 in every single sheet you could change the code as follows
Private Sub App_WorkbookOpen(ByVal Wb As Workbook)
Dim sh As Worksheet
App.ScreenUpdating = False
For Each sh In Wb.Worksheets
sh.Select
sh.Range("A1").Select
Next
App.Goto Wb.Worksheets(1).Range("A1")
App.ScreenUpdating = True
End Sub
A simple solution:
For Each Sheet In ActiveWorkbook.Worksheets
Sheet.Select
Range("A1").Select
Next
Using MicScoPau's loop through the worksheets
Place the following code in the ThisWorkbook module of Personal.xlsb:
You'll have to reopen excel for this to work the first time.
If your Personal.xlsb is hidden, then you will have some issues with the each sheet in activeworkbook.
Private WithEvents app As Application
Private Sub app_WorkbookOpen(ByVal Wb As Workbook)
For Each Sheet In ActiveWorkbook.Worksheets
Sheet.Select
Range("A1").Select
Next
End Sub
Private Sub Workbook_Open()
Set app = Application
End Sub

How do I fire an Excel VBA Macro whenever a cell value is updated?

I have a Sub that I would like to run whenever cells are updated to contain a certain value.
Right now I'm using code like the following:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
If Target.Cells.Count = 1 Then
If Target.Value = XYZ Then
my_sub a, b, c
End If
End If
End Sub
The issue right now is that the macro only fires when I edit these cells directly, not when changes in other cells force these cells to change.
Additionally, these cells are not well defined, so I can not hard code "when A5 changes", for example. I need this to fire every time any cell in my workbook is updated (manually or through formulas) to meet my condition.
Provided your target is only a single cell with a formula that needs to be monitored, this will work:
Option Explicit
Dim tarVal As Variant
Private Sub Worksheet_Activate()
tarVal = ActiveSheet.Range("A1").Value ' change range parameter to the address of the target formula
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
Dim tempVal As Variant
tempVal = ActiveSheet.Range("A1").Value
If tempVal <> tarVal Then
tarVal = tempVal
' your code here
MsgBox "The value of A1 has changed" ' for testing purposes only, delete later
End If
End Sub
Edit
The following code works for an entire range of cells, but only if automatic calculation is turned on. In case the monitored cells are non-contiguous, just use union statements when defining the target range. (The target range is A1:A10 in this example). This is under the assumption that only one of formulas in the target range can change its value at a time. If multiple target formulas can do that, then remove Exit for in the Worksheet_Change subroutine.
Option Explicit
Dim tarCellCount As Long
Dim tarRng As Range
Dim tarVals As Variant
Private Sub Worksheet_Activate()
Dim i As Long
Dim cll As Range
Set tarRng = ActiveSheet.Range("A1:A10") ' change range parameter to the addresses of the target formulas
tarCellCount = tarRng.Cells.count
ReDim tarVals(1 To tarCellCount) As Variant
For Each cll In tarRng
i = i + 1
tarVals(i) = cll.Value
Next cll
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
Dim changeBool As Boolean
Dim i As Long
Dim cll As Range
Dim tempVal As Variant
For Each cll In tarRng
tempVal = cll.Value
i = i + 1
If tempVal <> tarVals(i) Then
tarVals(i) = tempVal
changeBool = True
Exit For
End If
Next cll
If changeBool Then
' your code here
MsgBox "The value of one of the cells in the target range has changed" ' for testing purposes only, delete later
End If
End Sub
Add your cells to be tracked to a named formula (named range). I used rngValue
Use a static variable to track how many times the value you want to track occurs in this range
Use the Calculate event to check if the number of occurences changes
code
Private Sub Worksheet_Calculate()
Dim StrIn As String
Static lngCnt As Long
Dim lngCnt2 As Long
StrIn = "apples"
lngCnt2 = Application.WorksheetFunction.CountIf(Range("rngValue"), StrIn)
If lngCnt2 <> lngCnt Then
lngCnt = lngCnt2
Call mysub
End If
End Sub
Target is a range that CAN contain more cells. This code should work for you.
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
For Each cell In Target.Cells
If cell.Value = XYZ Then
my_sub a, b, c
End If
Next cell
End Sub
Edit: I see that you want to fire that also when formula is updated defined value. It can be slow if you will check every cell, but really depends on the size of your file. Here is some code to give you idea how to do it.
Private Sub Workbook_SheetCalculate(ByVal sh As Object)
For Each cell In sh.Cells.SpecialCells(xlCellTypeFormulas).Cells
If cell.Value = XYZ Then
my_sub a, b, c
End If
Next cell
End Sub

Excel VBA: Move ActiveCell to Row of Newly Inactive Sheet

When I move from Sheet1 to Sheet2, what VBA can I use to have the activecell of Sheet2 be the same row as was active on Sheet1 when I switched?
For example: I have Cell B7 active on Sheet1. When I switch to Sheet2, the activecell moves to the 7th row, (and does not change columns from what it was the last time I was on Sheet2).
After really debugging hard on event sequences, I said "Eureka!". The following does what you ask:
Private activeRow As Integer, activeCol As Integer
Private sema4 As Integer
Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)
If (sema4 > 0) Then Exit Sub
sema4 = 1
Sheets(Sh.Name).Activate
End Sub
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
If (sema4 = 1) Then
activeRow = Selection.row
activeCol = Selection.Column
sema4 = 2
Exit Sub
ElseIf (sema4 = 2) Then
sema4 = 3
Sheets(Sh.Name).Activate
Exit Sub
ElseIf (sema4 = 3) Then
ActiveSheet.Cells(activeRow, activeCol).Select
sema4 = 0
End If
End Sub
Again, attach in VB editor to the Workbook.
Although the question received a downvote, it is absolutely not trivial. I have only been able to research a partial answer.
Attach the following code to the Workbook (double click on ThisWorkbook in VBA Project Explorer):
Private activeRow As Integer, activeCol As Integer
Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)
activeRow = Selection.Row
activeCol = Selection.Column
End Sub
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
ActiveSheet.Cells(activeRow, activeCol).Select
End Sub
The intention is clear: get the selection on the sheet being deactivated and then set the selection on the sheet being acivated.
There only are two problems:
Excel has only one Selection and that is the current selection on the active sheet.
The deactivate event occurs after the sheet is deactived and the new sheet activated.
As a result, it is not possible to get the last position of the user on the sheet that got deactivated and so we can't set it on the sheet being activated.
Anyone any ideas?

add a formula in a footer of excel and have it update automatically when cell range is changed

I have added a VBA in excel and it says:
Public Sub SetFooter()
Dim ws As Worksheet
Application.ScreenUpdating = False
For Each ws In Worksheets
ws.Select
With ActiveSheet
.PageSetup.CenterFooter = Evaluate("=SUM($J$6:$J$28)")
.PageSetup.LeftFooter = Evaluate("=average($J$6:$J$28)")
End With
Next
End Sub
How to get the formula to change automatically when I change the value in the cell?
You don't need to loop through all the sheets to change the footer. You can use the Workbook_SheetChange event. Whenever there is a change in the respective range in a particular sheet, the footer of that sheet will get automatically updated.
This code goes in ThisWorkbook
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Dim rng As Range
Set rng = Range("$J$6:$J$28")
If Not Intersect(Target, rng) Is Nothing Then
With Sh.PageSetup
.CenterFooter = Evaluate("=SUM($J$6:$J$28)")
.LeftFooter = Evaluate("=average($J$6:$J$28)")
End With
End If
End Sub