Combining 2 macros into 1

Knoxzy

New Member
Joined
Mar 25, 2013
Messages
2
Hey all,

I got 2 macros that I want to run in one go. So to explain:

Here is the data of Sheet1 before the macro is run:

ABCDEFGHIJK
1StyleSGridCol01Col02Col03Col04Col05Col06Col07Col08Col09
2CD7879 478347844895
3CL3737 20192135463046314665466647874890
4CL4407 4888
5CL4453 32564631472647874890
6CL4630C1646994703
7CL4851 478448854897
8CL6577 4882489048924893
9CL6845 46914788486348834884
10CL6948 31974691489148944896
11CL7521 48874889
12CL7522 4886
13CL7597CB53197
14CL7598 3256
15CL7599 46994890
16CL7600 47844788488348944896
17CL7601C448824691
18CL7602 4691
19CL7603 319748944896
20CL7604C564229
21CL7605 42234890
22CL7606 42164788488348944896
23CL7607C2342104691

<COLGROUP><COL style="WIDTH: 19pt; mso-width-source: userset; mso-width-alt: 914" width=25><COL style="WIDTH: 48pt" span=11 width=64><TBODY>
</TBODY>

<TBODY>
</TBODY>

And here is the result after the macro is run (gets put into Sheet2):


CD7879 478347844895
CL3737 20192135463046314665466647874890
CL4407 4888
CL4453 32564631472647874890
CL4851 478448854897
CL6577 4882489048924893
CL6845 46914788486348834884
CL6948 31974691489148944896
CL7521 48874889
CL7522 4886
CL7598 3256
CL7599 46994890
CL7600 47844788488348944896
CL7602 4691
CL7603 319748944896
CL7605 42234890
CL7606 42174788488348944896

<COLGROUP><COL style="WIDTH: 48pt" span=10 width=64><TBODY>
</TBODY>

<TBODY>
</TBODY>

So what the macro does is pretty much if a cell is empty on Column B, grabs the entire row and puts it
into Sheet2.

Here is the macro I use to run it:



Sub Macro1()

Dim cell As Range
Dim lastRow As Long, i As Long

lastRow = Range("A" & Rows.Count).End(xlUp).Row
i = 1

For Each cell In Sheets(1).Range("B1:B" & lastRow)
If cell.Value = "" Then
cell.EntireRow.Copy Sheets(2).Cells(i, 1)
i = i + 1

End If
Next

End Sub




So after that, I run my second macro on Sheet2 which is:


Sub Macro2()

'for Macro to: Grab data from a row/s then putting it into a column/s then sort by smallest to largest and remove duplicates.
Dim LR As Long
Dim BC As Integer
Dim BC1 As Integer
Dim C As Range
Application.ScreenUpdating = False

'step1 Grab the data from the rows on Column C to V, going down the rows as highlighted and ignoring Columns A & B then put them into column W.
BC = 20 'Number of columns between C & V
LR = ActiveSheet.UsedRange.Rows.Count
Set C = ActiveSheet.Range("C1:C" & LR)
A = 1
B = 3
P = 1
For Each cell In C
BC1 = Application.CountBlank(Range("C" & A & ":V" & A))
Cells(A, B).Resize(, BC - BC1).Copy
Range("W" & P).Select
Selection.PasteSpecial Paste:=xlPasteValues, Transpose:=True
P = P + (BC - BC1)
A = A + 1
Next

'step2 Sort them by smallest to largest.
LR = Range("W" & Rows.Count).End(xlUp).Row
Set C = ActiveSheet.Range("W1:W" & LR)
ActiveWorkbook.ActiveSheet.Sort.SortFields.Clear
ActiveWorkbook.ActiveSheet.Sort.SortFields.Add Key:=Range("W1:W" & LR) _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.ActiveSheet.Sort
.SetRange Range("W1:W" & LR)
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With

'Step3 Remove duplicates
ActiveSheet.Range("$W$1:$W$" & LR).RemoveDuplicates Columns:=1, Header:=xlNo

'Step4
LR = Range("W" & Rows.Count).End(xlUp).Row
A = 24
For i = 21 To LR Step 20
Cells(i, 23).Resize(20).Cut Destination:=Cells(1, A)
A = A + 1
Next i
Application.ScreenUpdating = True
End Sub



Here is the data before Macro2 is run:


ABCDEFGHIJ
1CD7879 478347844895
2CL3737 20192135463046314665466647874890
3CL4407 4888
4CL4453 32564631472647874890
5CL4851 478448854897
6CL6577 4882489048924893
7CL6845 46914788486348834884
8CL6948 31974691489148944896
9CL7521 48874889
10CL7522 4886
11CL7598 3256
12CL7599 46994890
13CL7600 47844788488348944896
14CL7602 4691
15CL7603 319748944896
16CL7605 42234890
17CL7606 42174788488348944896

<COLGROUP><COL style="WIDTH: 19pt; mso-width-source: userset; mso-width-alt: 914" width=25><COL style="WIDTH: 48pt" span=10 width=64><TBODY>
</TBODY>

<TBODY>
</TBODY>

And this is after data after Macro2 is run:


<o:p></o:p>
W<o:p></o:p>
X<o:p></o:p>
1<o:p></o:p>
2019<o:p></o:p>
4884<o:p></o:p>
2<o:p></o:p>
2135<o:p></o:p>
4885<o:p></o:p>
3<o:p></o:p>
3197<o:p></o:p>
4886<o:p></o:p>
4<o:p></o:p>
3256<o:p></o:p>
4887<o:p></o:p>
5<o:p></o:p>
4217<o:p></o:p>
4888<o:p></o:p>
6<o:p></o:p>
4223<o:p></o:p>
4889<o:p></o:p>
7<o:p></o:p>
4630<o:p></o:p>
4890<o:p></o:p>
8<o:p></o:p>
4631<o:p></o:p>
4891<o:p></o:p>
9<o:p></o:p>
4665<o:p></o:p>
4892<o:p></o:p>
10<o:p></o:p>
4666<o:p></o:p>
4893<o:p></o:p>
11<o:p></o:p>
4691<o:p></o:p>
4894<o:p></o:p>
12<o:p></o:p>
4699<o:p></o:p>
4895<o:p></o:p>
13<o:p></o:p>
4726<o:p></o:p>
4896<o:p></o:p>
14<o:p></o:p>
4783<o:p></o:p>
4897<o:p></o:p>
15<o:p></o:p>
4784<o:p></o:p>
<o:p></o:p>
16<o:p></o:p>
4787<o:p></o:p>
<o:p></o:p>
17<o:p></o:p>
4788<o:p></o:p>
<o:p></o:p>
18<o:p></o:p>
4863<o:p></o:p>
<o:p></o:p>
19<o:p></o:p>
4882<o:p></o:p>
<o:p></o:p>
20<o:p></o:p>
4883<o:p></o:p>
<o:p></o:p>

<TBODY>
</TBODY>

<TBODY>
</TBODY>


Keep in mind, when Macro2 is run it puts the data on Column W then X then Y etc as soon as the Column reaches 20 it moves on to the next one.

So my question is:

Is there anyway to combine both macros to just run it once instead of running it individually?


Any questions or if I wasn't to clear on what Im trying to achieve, please don't hesitate to ask.

Thank you all in advance,

Knoxzy
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.

JoeMo

MrExcel MVP
Joined
May 26, 2009
Messages
18,144
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
Just before the End Sub line in Macro1 put

Code:
Sheets("Sheet2").Select
Call Macro2
 
Upvote 0

Knoxzy

New Member
Joined
Mar 25, 2013
Messages
2
Hey JoeMo,

Thanks for the reply.

Did you mean like this?:

Sub Macro1()

Dim cell As Range
Dim lastRow As Long, i As Long

lastRow = Range("A" & Rows.Count).End(xlUp).Row
i = 1
For Each cell In Sheets(1).Range("B1:B" & lastRow)
If cell.Value = "" Then
cell.EntireRow.Copy Sheets(2).Cells(i, 1)
i = i + 1

End If
Next

Sheets("Sheet2").Select
Call Macro2

End Sub




If it is, it comes up with an "Compile Error: Sub or Function not defined" and highlights "Call Macro2" as the error.
I am still pretty new at this so I apologize for not understanding it.
 
Upvote 0

Forum statistics

Threads
1,195,993
Messages
6,012,745
Members
441,724
Latest member
Aalbid

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