combining code and assigning it to different sheets

apor

New Member
Joined
Dec 20, 2021
Messages
25
Office Version
  1. 2016
Platform
  1. Windows
This is my set of subs that i am currently using:

Sub copy3171()
Set range_to_filter = Range("Table4")
range_to_filter.AutoFilter 1, "*3171"
range_to_filter.Copy Sheet2.Cells(1)
range_to_filter.AutoFilter
End Sub

Sub left()
Dim Formul As String
Formul = "=left(RC[-1], 27)"
Range("B1:B" & Cells(Rows.Count, 1).End(xlUp).Row) = [Formul]
End Sub

Sub right()
Dim Formul As String
Formul = "=right(RC[-1], 20)"
Range("C1:C" & Cells(Rows.Count, 1).End(xlUp).Row) = [Formul]
End Sub
Sub removeduplicates()
ActiveSheet.Range("A:BI").removeduplicates Columns:=3, Header:=xlNo
End Sub
Sub duplicates1()
Dim lr As Long, lr2 As Long, i As Long, w As Long
lr = Worksheets("Sheet2").Cells(Rows.Count, "C").End(xlUp).Row
lr2 = Worksheets("Sheet1").Cells(Rows.Count, "C").End(xlUp).Row
For i = 1 To lr
w = Application.WorksheetFunction.Match(Worksheets("Sheet2").Cells(i, "C"), Worksheets("Sheet1").Range("B1:B" & lr2), 0)
' no error checking
Worksheets("Sheet1").Cells(i, "F") = Worksheets("Sheet1").Cells(w, "B")
Worksheets("Sheet1").Cells(i, "G") = Worksheets("Sheet1").Cells(w, "C")
Next i
End Sub

At the moment, i am applying each sub individually and switching between sheets as i do so.

i need sub copy3171 to run first, the result automatically copies to sheet 2.
Problem: i then need to switch to sheet 2 using my mouse to let sub left, right and remove duplicates run.
then, when i let sub duplicates run, the result appears in sheet 1.
Problem: i want this entire set of subs to run in order all while i stay in sheet 1 and watch the final result pop up.

how would i go about doing this????
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
How about:

VBA Code:
Sub copy3171()
'
    Dim lr As Long, lr2 As Long, i As Long, w As Long
    Dim Formul As String
'
    Set range_to_filter = Range("Table4")
'
    range_to_filter.AutoFilter 1, "*3171"
    range_to_filter.Copy Sheet2.Cells(1)
    range_to_filter.AutoFilter
'
    lr2 = Worksheets("Sheet1").Cells(Rows.Count, "C").End(xlUp).Row
'
'   left
    With Sheet2
        Formul = "=left(RC[-1], 27)"
        .Range("B1:B" & .Cells(.Rows.Count, 1).End(xlUp).Row) = [Formul]
'
'   right
        Formul = "=right(RC[-1], 20)"
        .Range("C1:C" & .Cells(.Rows.Count, 1).End(xlUp).Row) = [Formul]
'
'   removeduplicates
        .Range("A:BI").removeduplicates Columns:=3, Header:=xlNo
'
'   duplicates1
        lr = .Cells(.Rows.Count, "C").End(xlUp).Row
'
        For i = 1 To lr
            w = Application.WorksheetFunction.Match(Worksheets("Sheet2").Cells(i, "C"), Worksheets("Sheet1").Range("B1:B" & lr2), 0)
'
'           no error checking
            Worksheets("Sheet1").Cells(i, "F") = Worksheets("Sheet1").Cells(w, "B")
            Worksheets("Sheet1").Cells(i, "G") = Worksheets("Sheet1").Cells(w, "C")
        Next i
    End With
End Sub
 
Upvote 0
How about:

VBA Code:
Sub copy3171()
'
    Dim lr As Long, lr2 As Long, i As Long, w As Long
    Dim Formul As String
'
    Set range_to_filter = Range("Table4")
'
    range_to_filter.AutoFilter 1, "*3171"
    range_to_filter.Copy Sheet2.Cells(1)
    range_to_filter.AutoFilter
'
    lr2 = Worksheets("Sheet1").Cells(Rows.Count, "C").End(xlUp).Row
'
'   left
    With Sheet2
        Formul = "=left(RC[-1], 27)"
        .Range("B1:B" & .Cells(.Rows.Count, 1).End(xlUp).Row) = [Formul]
'
'   right
        Formul = "=right(RC[-1], 20)"
        .Range("C1:C" & .Cells(.Rows.Count, 1).End(xlUp).Row) = [Formul]
'
'   removeduplicates
        .Range("A:BI").removeduplicates Columns:=3, Header:=xlNo
'
'   duplicates1
        lr = .Cells(.Rows.Count, "C").End(xlUp).Row
'
        For i = 1 To lr
            w = Application.WorksheetFunction.Match(Worksheets("Sheet2").Cells(i, "C"), Worksheets("Sheet1").Range("B1:B" & lr2), 0)
'
'           no error checking
            Worksheets("Sheet1").Cells(i, "F") = Worksheets("Sheet1").Cells(w, "B")
            Worksheets("Sheet1").Cells(i, "G") = Worksheets("Sheet1").Cells(w, "C")
        Next i
    End With
End Sub
thank you :) it worked perfectly
 
Upvote 0

Forum statistics

Threads
1,214,392
Messages
6,119,254
Members
448,879
Latest member
oksanana

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