Private Sub to Go to A2

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,566
Office Version
  1. 2021
Platform
  1. Windows
I have tried to write code to Move to A2 from Sheet BR1 Sales to Southern Sales when A2 is select on sheet BR1 Sales

When I select A2 on sheet Br1 Sales, I get "select method of class failed:

It would be appreciated if someone could kindly amend my code

Code:
rngA2.Select ' Select cell A2

Code:
 Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.Address = "$A$2" Then
        MoveSheetsToA2
    End If
End Sub

Private Sub MoveSheetsToA2()
    Dim ws As Worksheet
    Dim startSheet As Worksheet
    Dim endSheet As Worksheet
    Dim rngA2 As Range
    
    Set startSheet = ThisWorkbook.Sheets("BR1 Sales")
    Set endSheet = ThisWorkbook.Sheets("Southeern Sales")
    Set rngA2 = startSheet.Range("A2")
    
    Application.ScreenUpdating = False ' Disable screen updating for better performance
    
    ' Move all sheets from startSheet to endSheet
    For Each ws In ThisWorkbook.Sheets
        If ws.Index >= startSheet.Index And ws.Index <= endSheet.Index Then
            ws.Activate ' Activate the sheet
            rngA2.Select ' Select cell A2
        End If
    Next ws
    
    startSheet.Activate ' Activate the startSheet
    
    Application.ScreenUpdating = True ' Enable screen updating
    
    rngA2.Select ' Select cell A2
End Sub
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Hi Alex

I copied the code below in sheet BR1 Sales code page

Code:
 Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.Address = "$A$2" Then MoveSheetsToA2 Target
End Sub

I copied this in to the standard Module

Code:
 Public Sub MoveSheetsToA2(ByVal Target As Range)
    Dim ws              As Worksheet
    Dim startSheet As Worksheet, endSheet As Worksheet

    Set startSheet = Target.Parent
    Set endSheet = ThisWorkbook.Sheets("Southern Sales")
  
    ' Disable screen updating for better performance
    Application.ScreenUpdating = False
  
    ' Move all sheets from startSheet to endSheet
    For Each ws In ThisWorkbook.Worksheets
        If ws.Index >= startSheet.Index And ws.Index <= endSheet.Index Then
            ws.Activate ' Activate the sheet
            ws.Range(Target.Address).Select
        End If
    Next ws
  
    ' Activate the startSheet
    With startSheet
        .Activate
        .Range(Target.Address).Select
     End With
   
     ' Enable screen updating
    Application.ScreenUpdating = True

End Sub
 
Upvote 0
Thanks for clarifying. I don't think you had marked Dave's suggestion as the solution at the time or perhaps I just missed it ;).
Glad it is working for you now.
 
Upvote 0

Forum statistics

Threads
1,215,338
Messages
6,124,358
Members
449,155
Latest member
ravioli44

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