Defining a workbook

Adrianz

Board Regular
Joined
Jun 22, 2007
Messages
66
Code:
Sub GetDataFromClosedWorkbook()
    Dim wb As Workbook
    Application.ScreenUpdating = False ' turn off the screen updating
    PathName = Range("D3").Value
     'Make path selections below
    Set wb1 = ActiveWorkbook
    Set wb = Workbooks.Open(PathName)
     ' the sheet in this workbook to copy to
    With ThisWorkbook.Worksheets("Members")
         ' read data from the source workbook
         'the range to copy to in this workbook-name of sheet to copy FROM-range in closed workbook to copy
        .Range("A1", "B65536").Formula = wb.Worksheets("Members").Range("A1", "B65536").Formula
    End With 
If wb.Sheets.Count > 6 Then
        wb1.Sheets.Add.Name = "Members6"
        wb1.Sheets("Members6").Move After:=Sheets(7)
        With wb1.Worksheets("Members6")
        .Range("A1", "B65536").Formula = wb.Worksheets("Members6").Range("A1", "B65536").Formula
        End With
        With wb1.Worksheets("Members6")
        .Range("C1", "H65536").Formula = wb.Worksheets("Members5").Range("F1", "K65536").Formula
        End With
End If
    wb.Close False ' close the source workbook without saving any changes
    Set wb = Nothing ' free memory
    Sheets("Menu").Select
    Range("D8").Select
    ActiveCell.Value = "Last Update was:"
    Range("F8").Select
    Selection.Formula = "=text(now(),""mmm dd yyyy hh:mm"")"
    Range("D9").Select
    Application.ScreenUpdating = True ' turn on the screen updating
End Sub

The IF section of my macro is supposed to count the number of sheets in a predefined workbook an then if that number exceeds 6, it will copy the data from a sheet in that workbook and paste it into a newly created sheet in my original workbook. I'm running into some problems with defining the original workbook. As you see I set wb1 = ActiveWorkbook (the original workbook) at the beginning of the macro. What is happening is the new sheet is being created in the secondary workbook instead of the main one. I think this is because prior to the creation of the new sheet, the ActiveWorkbook is the secondary workbook, therefore wb1 represents the secondary workbook at the time. What should I change the early line using ActiveWorkbook to so that it holds its value throughout the macro (the original workbook). I don't wish to use any hard path names as these may change when other users use the file.


fyi this is xposted but I will keep it up to date with any developments
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Are you 100% sure about this behaviour?

I can't immediately see anything in the code you refer to that would cause it.

When you set wb here,
Code:
Set wb1 = ActiveWorkbook
it will create a reference to the currently active workbook, and that won't change even if you open another workbook.

One problem I do see though is that the dot qualifier is missing here for Sheets(7).
Code:
wb1.Sheets("Members6").Move After:=Sheets(7)
That could be the problem.:)
 
Upvote 0
Code:
wb1.Sheets("Members6").Move After:=Sheets(7)

How would I go about changing this line? Other than that this is somewhat of a mystery.
 
Upvote 0
I am getting the error "Subscript out of range" and the line
Code:
With wb1.Worksheets("Members6")
is highlighted.

Even though the macro can't complete, there is a new sheet created in the secondary workbook named Members6 (and it is moved to the exact loc that I specified). THe macro is continuously creating the new worksheet in the wrong file and this is why the aforementioned line can't find a sheet named Members6. All my code seems to point to the original workbook yet it's just not happening.
 
Upvote 0
As far as I can see the code isn't doing that.

It creates the new worksheet here.
Code:
wb1.Sheets.Add.Name = "Members6"
And it creates it in wb1.

However because of the dot qualifier problem I mentioned here,
Code:
wb1.Sheets("Members6").Move After:=Sheets(7)
the new worksheet could be getting moved to the currently active workbook, which is probably wb rather than wb1.
The code should probaby be this.
Code:
wb1.Sheets("Members6").Move After:=.Sheets(7) ' only change is the . in front of Sheets(7)
 
Upvote 0
Adding that dot gave me this error:

"Compile Error: Invalid or unqualified reference"

I tried something new in the meantime... I deleted the With statement that was producing the error. What happened was that the If statement executed correctly and I found a new worksheet with the right name and in the intended location. The With statement can't locate this sheet for some reason.
 
Upvote 0
Oops, my bad.:oops:

You still have an unqualified reference but I assumed it was within the With End With structure.

Try this.
Code:
With wb1
    .Sheets.Add.Name = "Members6"
    .Sheets("Members6").Move After:=.Sheets(7)
    .Range("A1", "B65536").Formula = wb.Worksheets("Members6").Range("A1", "B65536").Formula
    .Range("C1", "H65536").Formula = wb.Worksheets("Members5").Range("F1", "K65536").Formula
End With
 
Upvote 0
Code:
If wb.Sheets.Count > 6 Then
    wb1.Sheets.Add.Name = "Members6"
    wb1.Sheets("Members6").Move After:=wb1.Sheets(7)
    With wb1.Worksheets("Members6")
    .Range("A1", "B65536").Formula = wb.Worksheets("Members6").Range("A1", "B65536").Formula
    .Range("C1", "H65536").Formula = wb.Worksheets("Members6").Range("F1", "K65536").Formula
    End With
    End If

Ended up with this and it works perfectly. Thanks for all the help Norie!
 
Upvote 0

Forum statistics

Threads
1,216,540
Messages
6,131,255
Members
449,638
Latest member
ygdalvi

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