Combine two macros

Ruthanne

Board Regular
Joined
Mar 2, 2004
Messages
123
I have two sets of code that are working successfully thanks to Jindon & another thread but I don't know how to put them together. I am getting an error because I'm repeating the "i".
Here is the two codes:
Code:
Sub testcombinetwo()
'pasted & editted from http://www.mrexcel.com/forum/showthread.php?t=99187
FinalRowSh1 = Worksheets("Master").Range("A65536").End(xlUp).Row
FinalRowSh2 = Worksheets("WeeklyJob").Range("A65536").End(xlUp).Row

For i = FinalRowSh2 To 1 Step -1
For J = FinalRowSh1 To 1 Step -1
If Worksheets("Master").Cells(J, 1) = Worksheets("WeeklyJob").Cells(i, 1) And Worksheets("Master").Cells(J, 1) = Worksheets("WeeklyJob").Cells(i, 1) Then
Worksheets("Master").Cells(J, 3) = Worksheets("WeeklyJob").Cells(i, 3)
Worksheets("Master").Cells(J, 4) = Worksheets("WeeklyJob").Cells(i, 4)
Worksheets("Master").Cells(J, 5) = Worksheets("WeeklyJob").Cells(i, 5)
Worksheets("Master").Cells(J, 6) = Worksheets("WeeklyJob").Cells(i, 6)
End If
Next J
Next i

'following portion from from jindon's reply on mrexcel
Dim a, i As Long, ii As Integer, z As String
a = Sheets("WeeklyJob").Range("a1").CurrentRegion.Resize(, 6).Value
With CreateObject("Scripting.Dictionary")
    .CompareMode = vbTextCompare
    For i = 1 To UBound(a, 1)
        z = a(i, 1) & ";" & a(i, 2)
        If Not .exists(z) Then
            .Add z, Array(a(i, 1), a(i, 2), a(i, 3), a(i, 4), a(i, 5), a(i, 6))
        End If
    Next
    a = Sheets("Master").Range("a1").CurrentRegion.Resize(, 6).Value
    For i = 1 To UBound(a, 1)
        z = a(i, 1) & ";" & a(i, 2)
        If .exists(z) Then
            w = .Item(z)
            For ii = 3 To 6: a(i, ii) = w(ii - 1): Next
            .Remove z
        End If
    Next
    If .Count > 0 Then
        Sheets("Master").Range("a" & Rows.Count).End(xlUp)(2) _
        .Resize(.Count, 6).Value = Application.Transpose(Application.Transpose(.items))
    End If
End With
End Sub
Thank you gurus for your wisdom & constant guidance!

I am using Excel 2003.
The code is meant to:
match column A & column B data of Worksheet(WeeklyJob) to column A & column B of Worksheet(Master) then if match is found copy column C through column F into Worksheet(Master) column C through F (and overwrite any [outdated] existing data there may be in those columns [thus updating the job's weekly charges, etc.]).
If match is not found I would like it to copy entire row from Worksheet(WeeklyJobs) into first blank row at end of Worksheet(Master) (thus giving me a new record of a new job from the weekly report).
All the columns in both worksheets are labeled the same (& row 1 is headings).
(thread http://www.mrexcel.com/forum/showthread.php?p=1600311#post1600311)

Sample of my data:
Before Macro:
Worksheet(Master)
A B C D E F
Tom Jr 2 3 4 5
Tom Sr 2 3 4 5
Tom 2 3 4 5
Jen 2 3 4 5
Worksheet(WeeklyJob)
A B C D E F
Tom Sr 7 7 7 7
Xav Sr 8 8 8 8
After macro I would like it to perform/look like this:
Worksheet(Master)
A B C D E F
Tom Jr 2 3 4 5
Tom Sr 7 7 7 7
Tom 2 3 4 5
Jen 2 3 4 5
Xav Sr 8 8 8 8
Worksheet(WeeklyJob)
A B C D E F
Tom Sr 7 7 7 7
Xav Sr 8 8 8 8
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.

Ruthanne

Board Regular
Joined
Mar 2, 2004
Messages
123
I am posting "answer" for future reference.

Silly me was approaching the solution wrong (I thought I needed to clean up the code's references & arrays to combine all the code into one sub).
MrExcel's archives shows another (easier) approach:
Option A - create a new sub that runs (calls) both subs or
Option B - call the 2nd sub at the end of the first code (thread http://www.mrexcel.com/forum/showthread.php?t=258579)
Examples as follows:
Code:
'Option A
Sub ComboSub()
     Call Sub1
     Call Sub2
End Sub
 
'Option B
Sub mySub1()
'mySub1 code goes here!
mySub2
End Sub

'or
Sub mySub1()
'mySub1 code goes here!
Call mySub2
End Sub

'or
Sub mySub1()
'mySub1 code goes here!
Application.Run("mySub2")
End Sub

Thanks again to MrExcel's archive of knowledge (just as my boss was telling me to give up!)
 
Last edited:
Upvote 0

Forum statistics

Threads
1,191,171
Messages
5,985,067
Members
439,938
Latest member
MAlhash

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
Top