Running VBA Code simultaneously on other identical worksheets from one main worksheet

Jan Kalop

Active Member
Joined
Aug 3, 2012
Messages
389
How to make to run below VBA Code simultaneously on 5 other identical worksheets but with different data and name of the worksheets, from main Worksheets("MonDBS")

The other worksheet are:

Worksheets("TueDBS")
Worksheets("WedDBS")
Worksheets("ThuDBS")
Worksheets("FriDBS")
Worksheets("SatDBS")

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim A As Long
Dim B55 As Range: Set B55 = Range("B55")
If Target.CountLarge > 1 Then Exit Sub
If Not Intersect(Target, B55) Is Nothing Then
For A = 57 To 700
If Cells(A, 2).Value = B55.Value Then Cells(A, 2).Resize(, 158).Value = Worksheets("MonDBS").Range("B55:FC55").Value
Next A
Set B55 = Nothing
End If
End Sub
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
If you want to have a common Private Sub Worksheet_SelectionChange(ByVal Target As Range) event subrountine that you can use in six different worksheets, but which reference different 'DBS' worksheets then there must be a means of choosing that can be expressed in code.

1. How do you chose which of
Worksheets("MonDBS")​
Worksheets("TueDBS")​
Worksheets("WedDBS")​
Worksheets("ThuDBS")​
Worksheets("FriDBS")​
Worksheets("SatDBS")​

each one of your 6 worksheets will use?

2. What specifically do you mean by "different data"?

(Tip: when posting code, please try to use 'code tags' to format your code.
It makes the code easier to read.)
 
Upvote 0
If you want to have a common Private Sub Worksheet_SelectionChange(ByVal Target As Range) event subrountine that you can use in six different worksheets, but which reference different 'DBS' worksheets then there must be a means of choosing that can be expressed in code.

1. How do you chose which of
Worksheets("MonDBS")​
Worksheets("TueDBS")​
Worksheets("WedDBS")​
Worksheets("ThuDBS")​
Worksheets("FriDBS")​
Worksheets("SatDBS")​

each one of your 6 worksheets will use?

2. What specifically do you mean by "different data"?

(Tip: when posting code, please try to use 'code tags' to format your code.
It makes the code easier to read.)
It may help you understand what I would like to achieve. I tried COD as below but unfortunately it doesn't work. Only executes the first command for a MonDBS worksheet, ignoring the others, and I would like to be able to execute with one click using the dedicated button (B55) on the MonDBS worksheet to execute all commands for all worksheets simultaneously.

2. What specifically do you mean by "different data"?.......they are just different orders for each day of the week



"Private Sub Worksheet_SelectionChange(ByVal Target As Range)

Dim A As Long
Dim B55 As Range: Set B55 = Range("B55")
If Target.CountLarge > 1 Then Exit Sub
If Not Intersect(Target, B55) Is Nothing Then
Application.ScreenUpdating = False

For A = 57 To 700
If Worksheets("MonDBS").Cells(A, 2).Value = B55.Value Then Worksheets("MonDBS").Cells(A, 2).Resize(, 158).Value = Worksheets("MonDBS").Range("B55:FC55").Value
Next A

For B = 57 To 700
If Worksheets("TueDBS").Cells(B, 2).Value = B55.Value Then Worksheets("TueDBS").Cells(B, 2).Resize(, 158).Value = Worksheets("TueDBS").Range("B55:FC55").Value
Next B

For C = 57 To 700
If Worksheets("WedDBS").Cells(C, 2).Value = B55.Value Then Worksheets("WedDBS").Cells(C, 2).Resize(, 158).Value = Worksheets("WedDBS").Range("B55:FC55").Value
Next C

For D = 57 To 700
If Worksheets("ThuDBS").Cells(D, 2).Value = B55.Value Then Worksheets("ThuDBS").Cells(D, 2).Resize(, 158).Value = Worksheets("ThuDBS").Range("B55:FC55").Value
Next D

For E = 57 To 700
If Worksheets("FriDBS").Cells(E, 2).Value = B55.Value Then Worksheets("FriDBS").Cells(E, 2).Resize(, 158).Value = Worksheets("FriDBS").Range("B55:FC55").Value
Next E


For F = 57 To 700
If Worksheets("SatDBS").Cells(E, 2).Value = B55.Value Then Worksheets("SatDBS").Cells(E, 2).Resize(, 158).Value = Worksheets("SatDBS").Range("B55:FC55").Value
Next F

Application.ScreenUpdating = True
Set B55 = Nothing
End If
End Sub"
 
Upvote 0
I cannot help but notice that you continue not to use code tags when posting your code.
 
Upvote 0
Perhaps something like this.

VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim A As Long
    Dim B55 As Range
    Dim SelectedWorksheet As Worksheet
    Dim DBSList As Variant, DBS As Variant

    DBSList = Array("MonDBS", "TueDBS", "WedDBS", "ThuDBS", "FriDBS", "SatDBS")

    Set B55 = Me.Range("B55")

    If Not Intersect(Target, B55) Is Nothing And Target.Cells.Count = 1 Then
        For A = 57 To 700
            For Each DBS In DBSList
                With Worksheets(DBS)
                    If .Cells(A, 2).Value = B55.Value Then
                        .Cells(A, 2).Resize(, 158).Value = .Range("B55:FC55").Value
                    End If
                End With
            Next DBS
        Next A
        Set B55 = Nothing
    End If
End Sub
 
Upvote 0
Solution
Perhaps something like this.

VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim A As Long
    Dim B55 As Range
    Dim SelectedWorksheet As Worksheet
    Dim DBSList As Variant, DBS As Variant

    DBSList = Array("MonDBS", "TueDBS", "WedDBS", "ThuDBS", "FriDBS", "SatDBS")

    Set B55 = Me.Range("B55")

    If Not Intersect(Target, B55) Is Nothing And Target.Cells.Count = 1 Then
        For A = 57 To 700
            For Each DBS In DBSList
                With Worksheets(DBS)
                    If .Cells(A, 2).Value = B55.Value Then
                        .Cells(A, 2).Resize(, 158).Value = .Range("B55:FC55").Value
                    End If
                End With
            Next DBS
        Next A
        Set B55 = Nothing
    End If
End Sub
Thanks a lot, everything works like a Swiss watch and I am very happy and grateful to you for your help
 
Upvote 0

Forum statistics

Threads
1,214,822
Messages
6,121,767
Members
449,049
Latest member
greyangel23

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