Basic range copy, match sheetnames: logic, style, and syntax

seagreen

Board Regular
Joined
Feb 7, 2009
Messages
71
Hi,
I'm just beginning to learn VBA. I am wondering if using an "if" statement or a case statement would be good ways to check two worksheets in two different workbooks to see if their names match? I could use some help correcting my syntax too:

Code:
For 1 to ws.count
If Worksheet("one").Name = Worksheet("two").Name
      Worksheet("one").Range("A1:G84").Copy
      Worksheet("two").Range("A1:G84").Paste
Endif
Next ws.Count

I know this is basic stuff but I'm having a little trouble getting the hang of it.
Thanks in advance for your help.
 
Jindon,
I can't tell you how much I appreciate your help. 8-) You make it so clear in a way that the books have not. I am getting a "subscript out of range error" at this line :
Code:
Set wb2 = Workbooks("microscope1.xls")

Do I need to have the same number and like-named sheets in both workbooks?

Thanks.

That means the string in the brackets, microscope1.xls, is wrong.
 
Upvote 0

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Thanks Jindon. I guess I needed to have both workbooks open. I had "Microsocope1.xls" closed. It worked after I opened it. Thanks!!:beerchug:
 
Upvote 0
If you want to copy only the values without the format then you don't need to open this case.
Rich (BB code):
Sub testNotOpen()
Dim myFolder As String, fn As String, myRange As String
Dim myRows As Long, myCols As Long, myDest As String
myFolder = "c:\test\"  ' ← folder path
fn = "microscope1.xls"  ' ← file name
myRange = "A1:G84"    ' ← range address to be extracted
myDest = "A1"            ' ← address on the destination sheet
With Range(myRange)
    myRows = .Rows.Count
    myCols = .Columns.Count
End With
For Each ws In Sheets
    With ws.Range(myDest).Resize(myRows, myCols)
        .Formula = "='" & myFolder & "[" & fn & "]" & ws.Name & "'!" & Split(myRange,":")(0)
        .Value = .Value
    End With
Next
End Sub
 
Upvote 0
This is great Jindon,

I'm going to try this new code out tomorrow morning. The earlier example you gave is working perfectly!
 
Upvote 0

Forum statistics

Threads
1,223,238
Messages
6,170,939
Members
452,368
Latest member
jayp2104

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