Using OR with WITH?

cfdh_edmundo

Board Regular
Joined
Nov 9, 2005
Messages
133
I have the following lines of code


Code:
  With Sheets("(Pasted) Sidney BT")

    Dim lastrow As Long
    lastrow = .Range("B" & .Rows.Count).End(xlUp).Row

    .Range("A1:AA" & lastrow).Copy wks.Range("A" & wks.Rows.Count).End(xlUp)

   End With

   With Sheets("(Pasted) Ursula BT")

    lastrow = .Range("B" & .Rows.Count).End(xlUp).Row

    .Range("A7:AA" & lastrow).Copy wks.Range("A" & wks.Rows.Count).End(xlUp).Offset(1)

   End With

This works fine, but I want to adjust the Withs so that the first With command also runs if the Sheet is called "(Pasted) Sidney Back Testing"
And the second With will also run if the sheet is called "(Pasted) Ursula Back Testing"

I've tried to use an OR operator with two With commands, but the text becomes red suggesting there is an error in my code

Code:
  With Sheets("(Pasted) Ursula BT") or With Sheets("(Pasted) Ursula Back Testing")

Does anyone know of a neat way of using an OR within a With

Many thanks for any help!
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
You cannot do that.
How would the macro know which sheet to use?
 
Upvote 0
That's a good point, theoretically both sheets could exist in the same workbook.

In actual fact only one will occur, essentially sheets are currently being labeled as "(Pasted) Ursula BT" but around 2 years ago (and all the time before that) they were being labeled as "(Pasted) Ursula Back Testing"

I think my best approach is to loop through the sheets before these Withs and then change any "*Back Testing*" sheetnames to "*BT*"

Thanks.
 
Upvote 0
Another option would be
Code:
   Dim sid As Worksheet
   Dim lastrow As Long

   If Evaluate("isref('(Pasted) Sidney BT'!A1)") Then
      Set sid = Sheets("(Pasted) Sidney BT")
   ElseIf Evaluate("isref('(Pasted) Sidney Back Testing'!A1)") Then
      Set sid = Sheets("(Pasted) Sidney Back Testing")
   End If
   lastrow = sid.Range("B" & sid.Rows.Count).End(xlUp).Row

   sid.Range("A1:AA" & lastrow).Copy wks.Range("A" & wks.Rows.Count).End(xlUp)
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,176
Members
453,021
Latest member
Justyna P

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