VBA running the same process on a second worksheet

michaeltsmith93

Board Regular
Joined
Sep 29, 2016
Messages
83
I am using the following code to remove items from a list on the current sheet and put them on another sheet. These same items exist on neighboring sheets with different names. It works fine for FindRow and SearchRange, but it's not doing anything to FindRow2 and SearchRange2. I'm not getting an error though. Thoughts?

Code:
Private Sub OK_Click()


Dim x As Long, ERow As Long
Dim PINamesArray As Variant
Dim size As Long
Dim SearchRange As Range
Dim SearchRange2 As Range
Dim FindRow As Long
Dim FindRow2 As Long
Dim k4 As Worksheet
Dim k5 As Worksheet


Set k4 = Worksheets("Dropped-NotSelected")
Set k5 = Worksheets(MovePIs.RegionName.Value)
Set SearchRange = Range("D5:D2000")
Set SearchRange2 = k5.Range("D5:D2000")
ERow = k4.Range("D" & Rows.Count).End(xlUp).Row + 1


PINamesArray = Split(Me.PINames, "; ")




For x = LBound(PINamesArray) To UBound(PINamesArray)


    
    FindRow = SearchRange.Find(What:=PINamesArray(x), LookIn:=xlValues).Row
    


    If FindRow <> 0 Then
        Rows(FindRow).Copy
        k4.Cells(ERow, 1).PasteSpecial
        Rows(FindRow).Delete Shift:=xlShiftUp
    End If
    
    FindRow2 = SearchRange2.Find(What:=PINamesArray(x), LookIn:=xlValues).Row
    
    If FindRow2 <> 0 Then
        Rows(FindRow2).Delete Shift:=xlShiftUp
    End If


Next x


Unload Me


End Sub
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
I'm not certain about this, but it's possible the find method might only work on the active sheet.

Try switch to the sheet you want to do find on right before you use it -- see below:


Code:
Private Sub OK_Click()


Dim x As Long, ERow As Long
Dim PINamesArray As Variant
Dim size As Long
Dim SearchRange As Range
Dim SearchRange2 As Range
Dim FindRow As Long
Dim FindRow2 As Long
Dim k4 As Worksheet
Dim k5 As Worksheet


Set k4 = Worksheets("Dropped-NotSelected")
Set k5 = Worksheets(MovePIs.RegionName.Value)
Set SearchRange = Range("D5:D2000")
Set SearchRange2 = k5.Range("D5:D2000")
ERow = k4.Range("D" & Rows.Count).End(xlUp).Row + 1


PINamesArray = Split(Me.PINames, "; ")




For x = LBound(PINamesArray) To UBound(PINamesArray)


    
    FindRow = SearchRange.Find(What:=PINamesArray(x), LookIn:=xlValues).Row
    


    If FindRow <> 0 Then
        Rows(FindRow).Copy
        k4.Cells(ERow, 1).PasteSpecial
        Rows(FindRow).Delete Shift:=xlShiftUp
    End If
    
[B]'ODIN ADDED THIS:
k5.activate[/B]

    FindRow2 = SearchRange2.Find(What:=PINamesArray(x), LookIn:=xlValues).Row
    
    If FindRow2 <> 0 Then
        Rows(FindRow2).Delete Shift:=xlShiftUp
    End If


Next x


Unload Me


End Sub
 
Last edited:
Upvote 0
This was it. Weird that you have to specify. I had to add in a line to activate the current sheet before the first findrow too.
 
Upvote 0

Forum statistics

Threads
1,223,910
Messages
6,175,318
Members
452,634
Latest member
cpostell

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