How Do I adjust VBA code to only combine 2 worksheets rather than all worksheets...

NHagedorn

New Member
Joined
May 11, 2012
Messages
37
Office Version
  1. 365
Platform
  1. Windows
Trying to only combine sheet ("XPO" and "DDD"). I tried this exclusion
If ws.Name <> "Stuff" And ws.Name <> "Data" And ws.Name <> "Next Injection" And ws.Name <> "Pull Through" And ws.Name <> "CDR" And ws.Name <> "Acct-W-Syr" And ws.Name <> "Pres-W-Syr" Then

but I'm getting error and it is still adding the last 2 (Acct-W-Syr and Pres-W-Syr)

How can I adjust the VBA code?



Sub CDRCombine()
Application.ScreenUpdating = False
Dim wsCDR As Worksheet
Dim LastRowWs As Long
Dim LastRowCDR As Long
Dim StartRowCDR As Long

Set wsCDR = ThisWorkbook.Worksheets("CDR")
LastRowCDR = wsCDR.Cells(wsCDR.Rows.Count, "A").End(xlUp).Row + 1
wsCDR.Range("A2:R" & LastRowCDR).Clear

For Each ws In ThisWorkbook.Worksheets
If ws.Name <> "Stuff" And ws.Name <> "Data" And ws.Name <> "Next Injection" And ws.Name <> "Pull Through" And ws.Name <> "CDR" And ws.Name <> "Acct-W-Syr-Prolia" And ws.Name <> "Pres-W-Syr-Prolia" Then

LastRowWs = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
StartRowCDR = wsCDR.Cells(wsCDR.Rows.Count, "A").End(xlUp).Row + 1 'first empty row
ws.Range("A2:R" & LastRowWs).Copy Destination:=wsCDR.Range("A" & StartRowCDR)
LastRowCDR = wsCDR.Cells(wsCDR.Rows.Count, "A").End(xlUp).Row
wsCDR.Range("E" & StartRowCDR & ":E" & LastRowCDR) = ws.Name
End If
Next
Application.ScreenUpdating = True
End Sub
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Code in module is

If ws.Name <> "Acct-W-Syr-Prolia" And ws.Name <> "Pres-W-Syr-Prolia"

But your worksheet name is "Acct-W-Syr" and "Pres-W-Syr" instead of "Acct-W-Syr-Prolia" and "Pres-W-Syr-Prolia" respectively. Either add "Prolia" in your worksheet name or remove it from your code. Then it will work.
 
Upvote 0
Try this line:

Code:
If ws.Name = "XPO" Or ws.Name = "DDD" Then

HTH

Robert
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,410
Messages
6,124,756
Members
449,187
Latest member
hermansoa

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