PC Review


Reply
Thread Tools Rate Thread

Activate an variable located workbook and copy 2 ranges?

 
 
will-d
Guest
Posts: n/a
 
      17th Jan 2008
Dear experts,

I have a program need 2 ranges of data from a file that need to be located
by user.I need to copy and paste them into thisworkbook.

Here is the code to located and get the directory and filename of the
source(it does not open that file for now):

Private Sub CommandButton5_Click()
Dim Filter, Title As String
Filter = "Excel Files (*.xls),*.xls, CSV Files (*.CSV), *.csv,"
Title = "Select Files to Calculate"
With Application
Fname5 = .GetOpenFilename(Filter, FilerIndex, Title, , ture)
End With
TextBox5.Value = Fname5
End Sub

Now I start to copy the first range to thisworkbook, (lrow1 is a variant
stores the length of the first range): First open source file, copy the
range, then activate thisworkbook, and paste the first range into a loaction.

Dim Workbook5 As Workbook
Set WookBook5 = Workbooks.Open(Fname5)
Worksheets("Sum_Page").Activate
ActiveSheet.Range("A2:A" & lrow1).Copy
ThisWorkbook.Activate
Sheets("Last_Mth").Activate
ActiveSheet.Paste Destination:=Worksheets("Last_Mth").Range("A2:A" & lrow1)

Now I need to activate the source file again to copy the 2nd range, but i
can not do it without closing and reopening it. following code does not
working:

Workbook5.activate
or
workbooks(workbook5).activate
or
workbooks.open (Workbook5)
or
workbooks(Fname5).activate

What is the right code to re-activate it without openning it twice?

Thanks

Will

 
Reply With Quote
 
 
 
 
Dave Peterson
Guest
Posts: n/a
 
      17th Jan 2008
What happens when you use:

Workbook5.activate



will-d wrote:
>
> Dear experts,
>
> I have a program need 2 ranges of data from a file that need to be located
> by user.I need to copy and paste them into thisworkbook.
>
> Here is the code to located and get the directory and filename of the
> source(it does not open that file for now):
>
> Private Sub CommandButton5_Click()
> Dim Filter, Title As String
> Filter = "Excel Files (*.xls),*.xls, CSV Files (*.CSV), *.csv,"
> Title = "Select Files to Calculate"
> With Application
> Fname5 = .GetOpenFilename(Filter, FilerIndex, Title, , ture)
> End With
> TextBox5.Value = Fname5
> End Sub
>
> Now I start to copy the first range to thisworkbook, (lrow1 is a variant
> stores the length of the first range): First open source file, copy the
> range, then activate thisworkbook, and paste the first range into a loaction.
>
> Dim Workbook5 As Workbook
> Set WookBook5 = Workbooks.Open(Fname5)
> Worksheets("Sum_Page").Activate
> ActiveSheet.Range("A2:A" & lrow1).Copy
> ThisWorkbook.Activate
> Sheets("Last_Mth").Activate
> ActiveSheet.Paste Destination:=Worksheets("Last_Mth").Range("A2:A" & lrow1)
>
> Now I need to activate the source file again to copy the 2nd range, but i
> can not do it without closing and reopening it. following code does not
> working:
>
> Workbook5.activate
> or
> workbooks(workbook5).activate
> or
> workbooks.open (Workbook5)
> or
> workbooks(Fname5).activate
>
> What is the right code to re-activate it without openning it twice?
>
> Thanks
>
> Will


--

Dave Peterson
 
Reply With Quote
 
will-d
Guest
Posts: n/a
 
      17th Jan 2008
Thanks. it is
Runtime error "91"
Object variable or with block variable not set.

Will

"Dave Peterson" wrote:

> What happens when you use:
>
> Workbook5.activate
>
>
>
> will-d wrote:
> >
> > Dear experts,
> >
> > I have a program need 2 ranges of data from a file that need to be located
> > by user.I need to copy and paste them into thisworkbook.
> >
> > Here is the code to located and get the directory and filename of the
> > source(it does not open that file for now):
> >
> > Private Sub CommandButton5_Click()
> > Dim Filter, Title As String
> > Filter = "Excel Files (*.xls),*.xls, CSV Files (*.CSV), *.csv,"
> > Title = "Select Files to Calculate"
> > With Application
> > Fname5 = .GetOpenFilename(Filter, FilerIndex, Title, , ture)
> > End With
> > TextBox5.Value = Fname5
> > End Sub
> >
> > Now I start to copy the first range to thisworkbook, (lrow1 is a variant
> > stores the length of the first range): First open source file, copy the
> > range, then activate thisworkbook, and paste the first range into a loaction.
> >
> > Dim Workbook5 As Workbook
> > Set WookBook5 = Workbooks.Open(Fname5)
> > Worksheets("Sum_Page").Activate
> > ActiveSheet.Range("A2:A" & lrow1).Copy
> > ThisWorkbook.Activate
> > Sheets("Last_Mth").Activate
> > ActiveSheet.Paste Destination:=Worksheets("Last_Mth").Range("A2:A" & lrow1)
> >
> > Now I need to activate the source file again to copy the 2nd range, but i
> > can not do it without closing and reopening it. following code does not
> > working:
> >
> > Workbook5.activate
> > or
> > workbooks(workbook5).activate
> > or
> > workbooks.open (Workbook5)
> > or
> > workbooks(Fname5).activate
> >
> > What is the right code to re-activate it without openning it twice?
> >
> > Thanks
> >
> > Will

>
> --
>
> Dave Peterson
>

 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      17th Jan 2008
If this line is executed successfully:
Set WookBook5 = Workbooks.Open(Fname5)
Then the workbook5 object variable will have been set.

Maybe it's a problem in the code you didn't share.

will-d wrote:
>
> Thanks. it is
> Runtime error "91"
> Object variable or with block variable not set.
>
> Will
>
> "Dave Peterson" wrote:
>
> > What happens when you use:
> >
> > Workbook5.activate
> >
> >
> >
> > will-d wrote:
> > >
> > > Dear experts,
> > >
> > > I have a program need 2 ranges of data from a file that need to be located
> > > by user.I need to copy and paste them into thisworkbook.
> > >
> > > Here is the code to located and get the directory and filename of the
> > > source(it does not open that file for now):
> > >
> > > Private Sub CommandButton5_Click()
> > > Dim Filter, Title As String
> > > Filter = "Excel Files (*.xls),*.xls, CSV Files (*.CSV), *.csv,"
> > > Title = "Select Files to Calculate"
> > > With Application
> > > Fname5 = .GetOpenFilename(Filter, FilerIndex, Title, , ture)
> > > End With
> > > TextBox5.Value = Fname5
> > > End Sub
> > >
> > > Now I start to copy the first range to thisworkbook, (lrow1 is a variant
> > > stores the length of the first range): First open source file, copy the
> > > range, then activate thisworkbook, and paste the first range into a loaction.
> > >
> > > Dim Workbook5 As Workbook
> > > Set WookBook5 = Workbooks.Open(Fname5)
> > > Worksheets("Sum_Page").Activate
> > > ActiveSheet.Range("A2:A" & lrow1).Copy
> > > ThisWorkbook.Activate
> > > Sheets("Last_Mth").Activate
> > > ActiveSheet.Paste Destination:=Worksheets("Last_Mth").Range("A2:A" & lrow1)
> > >
> > > Now I need to activate the source file again to copy the 2nd range, but i
> > > can not do it without closing and reopening it. following code does not
> > > working:
> > >
> > > Workbook5.activate
> > > or
> > > workbooks(workbook5).activate
> > > or
> > > workbooks.open (Workbook5)
> > > or
> > > workbooks(Fname5).activate
> > >
> > > What is the right code to re-activate it without openning it twice?
> > >
> > > Thanks
> > >
> > > Will

> >
> > --
> >
> > Dave Peterson
> >


--

Dave Peterson
 
Reply With Quote
 
will-d
Guest
Posts: n/a
 
      17th Jan 2008
I have this problem for a while, is it possible my version is too old? I am
using Excel 2002 SP3, and VB 6.3

"Dave Peterson" wrote:

> If this line is executed successfully:
> Set WookBook5 = Workbooks.Open(Fname5)
> Then the workbook5 object variable will have been set.
>
> Maybe it's a problem in the code you didn't share.
>
> will-d wrote:
> >
> > Thanks. it is
> > Runtime error "91"
> > Object variable or with block variable not set.
> >
> > Will
> >
> > "Dave Peterson" wrote:
> >
> > > What happens when you use:
> > >
> > > Workbook5.activate
> > >
> > >
> > >
> > > will-d wrote:
> > > >
> > > > Dear experts,
> > > >
> > > > I have a program need 2 ranges of data from a file that need to be located
> > > > by user.I need to copy and paste them into thisworkbook.
> > > >
> > > > Here is the code to located and get the directory and filename of the
> > > > source(it does not open that file for now):
> > > >
> > > > Private Sub CommandButton5_Click()
> > > > Dim Filter, Title As String
> > > > Filter = "Excel Files (*.xls),*.xls, CSV Files (*.CSV), *.csv,"
> > > > Title = "Select Files to Calculate"
> > > > With Application
> > > > Fname5 = .GetOpenFilename(Filter, FilerIndex, Title, , ture)
> > > > End With
> > > > TextBox5.Value = Fname5
> > > > End Sub
> > > >
> > > > Now I start to copy the first range to thisworkbook, (lrow1 is a variant
> > > > stores the length of the first range): First open source file, copy the
> > > > range, then activate thisworkbook, and paste the first range into a loaction.
> > > >
> > > > Dim Workbook5 As Workbook
> > > > Set WookBook5 = Workbooks.Open(Fname5)
> > > > Worksheets("Sum_Page").Activate
> > > > ActiveSheet.Range("A2:A" & lrow1).Copy
> > > > ThisWorkbook.Activate
> > > > Sheets("Last_Mth").Activate
> > > > ActiveSheet.Paste Destination:=Worksheets("Last_Mth").Range("A2:A" & lrow1)
> > > >
> > > > Now I need to activate the source file again to copy the 2nd range, but i
> > > > can not do it without closing and reopening it. following code does not
> > > > working:
> > > >
> > > > Workbook5.activate
> > > > or
> > > > workbooks(workbook5).activate
> > > > or
> > > > workbooks.open (Workbook5)
> > > > or
> > > > workbooks(Fname5).activate
> > > >
> > > > What is the right code to re-activate it without openning it twice?
> > > >
> > > > Thanks
> > > >
> > > > Will
> > >
> > > --
> > >
> > > Dave Peterson
> > >

>
> --
>
> Dave Peterson
>

 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      17th Jan 2008
I would bet that your code is wrong.

But without seeing it, I don't think you'll get any really good answers.

will-d wrote:
>
> I have this problem for a while, is it possible my version is too old? I am
> using Excel 2002 SP3, and VB 6.3
>
> "Dave Peterson" wrote:
>
> > If this line is executed successfully:
> > Set WookBook5 = Workbooks.Open(Fname5)
> > Then the workbook5 object variable will have been set.
> >
> > Maybe it's a problem in the code you didn't share.
> >
> > will-d wrote:
> > >
> > > Thanks. it is
> > > Runtime error "91"
> > > Object variable or with block variable not set.
> > >
> > > Will
> > >
> > > "Dave Peterson" wrote:
> > >
> > > > What happens when you use:
> > > >
> > > > Workbook5.activate
> > > >
> > > >
> > > >
> > > > will-d wrote:
> > > > >
> > > > > Dear experts,
> > > > >
> > > > > I have a program need 2 ranges of data from a file that need to be located
> > > > > by user.I need to copy and paste them into thisworkbook.
> > > > >
> > > > > Here is the code to located and get the directory and filename of the
> > > > > source(it does not open that file for now):
> > > > >
> > > > > Private Sub CommandButton5_Click()
> > > > > Dim Filter, Title As String
> > > > > Filter = "Excel Files (*.xls),*.xls, CSV Files (*.CSV), *.csv,"
> > > > > Title = "Select Files to Calculate"
> > > > > With Application
> > > > > Fname5 = .GetOpenFilename(Filter, FilerIndex, Title, , ture)
> > > > > End With
> > > > > TextBox5.Value = Fname5
> > > > > End Sub
> > > > >
> > > > > Now I start to copy the first range to thisworkbook, (lrow1 is a variant
> > > > > stores the length of the first range): First open source file, copy the
> > > > > range, then activate thisworkbook, and paste the first range into a loaction.
> > > > >
> > > > > Dim Workbook5 As Workbook
> > > > > Set WookBook5 = Workbooks.Open(Fname5)
> > > > > Worksheets("Sum_Page").Activate
> > > > > ActiveSheet.Range("A2:A" & lrow1).Copy
> > > > > ThisWorkbook.Activate
> > > > > Sheets("Last_Mth").Activate
> > > > > ActiveSheet.Paste Destination:=Worksheets("Last_Mth").Range("A2:A" & lrow1)
> > > > >
> > > > > Now I need to activate the source file again to copy the 2nd range, but i
> > > > > can not do it without closing and reopening it. following code does not
> > > > > working:
> > > > >
> > > > > Workbook5.activate
> > > > > or
> > > > > workbooks(workbook5).activate
> > > > > or
> > > > > workbooks.open (Workbook5)
> > > > > or
> > > > > workbooks(Fname5).activate
> > > > >
> > > > > What is the right code to re-activate it without openning it twice?
> > > > >
> > > > > Thanks
> > > > >
> > > > > Will
> > > >
> > > > --
> > > >
> > > > Dave Peterson
> > > >

> >
> > --
> >
> > Dave Peterson
> >


--

Dave Peterson
 
Reply With Quote
 
will-d
Guest
Posts: n/a
 
      17th Jan 2008
Thank you Dave for your patience, I changed "Workbook5" into "Wb5" and it
works now, have a great day!

"Dave Peterson" wrote:

> I would bet that your code is wrong.
>
> But without seeing it, I don't think you'll get any really good answers.
>
> will-d wrote:
> >
> > I have this problem for a while, is it possible my version is too old? I am
> > using Excel 2002 SP3, and VB 6.3
> >
> > "Dave Peterson" wrote:
> >
> > > If this line is executed successfully:
> > > Set WookBook5 = Workbooks.Open(Fname5)
> > > Then the workbook5 object variable will have been set.
> > >
> > > Maybe it's a problem in the code you didn't share.
> > >
> > > will-d wrote:
> > > >
> > > > Thanks. it is
> > > > Runtime error "91"
> > > > Object variable or with block variable not set.
> > > >
> > > > Will
> > > >
> > > > "Dave Peterson" wrote:
> > > >
> > > > > What happens when you use:
> > > > >
> > > > > Workbook5.activate
> > > > >
> > > > >
> > > > >
> > > > > will-d wrote:
> > > > > >
> > > > > > Dear experts,
> > > > > >
> > > > > > I have a program need 2 ranges of data from a file that need to be located
> > > > > > by user.I need to copy and paste them into thisworkbook.
> > > > > >
> > > > > > Here is the code to located and get the directory and filename of the
> > > > > > source(it does not open that file for now):
> > > > > >
> > > > > > Private Sub CommandButton5_Click()
> > > > > > Dim Filter, Title As String
> > > > > > Filter = "Excel Files (*.xls),*.xls, CSV Files (*.CSV), *.csv,"
> > > > > > Title = "Select Files to Calculate"
> > > > > > With Application
> > > > > > Fname5 = .GetOpenFilename(Filter, FilerIndex, Title, , ture)
> > > > > > End With
> > > > > > TextBox5.Value = Fname5
> > > > > > End Sub
> > > > > >
> > > > > > Now I start to copy the first range to thisworkbook, (lrow1 is a variant
> > > > > > stores the length of the first range): First open source file, copy the
> > > > > > range, then activate thisworkbook, and paste the first range into a loaction.
> > > > > >
> > > > > > Dim Workbook5 As Workbook
> > > > > > Set WookBook5 = Workbooks.Open(Fname5)
> > > > > > Worksheets("Sum_Page").Activate
> > > > > > ActiveSheet.Range("A2:A" & lrow1).Copy
> > > > > > ThisWorkbook.Activate
> > > > > > Sheets("Last_Mth").Activate
> > > > > > ActiveSheet.Paste Destination:=Worksheets("Last_Mth").Range("A2:A" & lrow1)
> > > > > >
> > > > > > Now I need to activate the source file again to copy the 2nd range, but i
> > > > > > can not do it without closing and reopening it. following code does not
> > > > > > working:
> > > > > >
> > > > > > Workbook5.activate
> > > > > > or
> > > > > > workbooks(workbook5).activate
> > > > > > or
> > > > > > workbooks.open (Workbook5)
> > > > > > or
> > > > > > workbooks(Fname5).activate
> > > > > >
> > > > > > What is the right code to re-activate it without openning it twice?
> > > > > >
> > > > > > Thanks
> > > > > >
> > > > > > Will
> > > > >
> > > > > --
> > > > >
> > > > > Dave Peterson
> > > > >
> > >
> > > --
> > >
> > > Dave Peterson
> > >

>
> --
>
> Dave Peterson
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Macro copy of variable ranges in one sheet to table in another she Kaare Microsoft Excel Programming 1 24th Jan 2008 09:01 PM
Named ranges scope / workbook/worksheet level named ranges- changeswith variable use... christian_spaceman Microsoft Excel Programming 3 24th Dec 2007 01:15 PM
Recording a macro to copy into a series of variable ranges =?Utf-8?B?cGV0ZXJz?= Microsoft Excel Programming 3 10th Aug 2007 10:44 PM
copy Ranges to other workbook. =?Utf-8?B?TWlyaQ==?= Microsoft Excel Misc 4 30th May 2007 01:38 PM
Copy variable ranges fron difernte shets =?Utf-8?B?b3B0aW11c18xOTcz?= Microsoft Excel Worksheet Functions 2 8th Jan 2006 04:39 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:40 PM.