Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 40
Default Workbook.Range Problem

I have a workbook that when I click a button it runs a macro that
allows me to open up another workbook of my choice, then copy a
certain range in the newly opened workbook and paste it into my
workbook that ran the macro. My question is, How can I set a range in
my newly opened workbook as a variable. It works when I do it like
this.

______________________________________________

Windows("MyNewlyOpenedWorkbook").Activate
Range("A1:A5").Select
Selection.Copy

Windows("MyWorkbookWithMacro").Activate
Range("A1:A5").Select
Selection.Paste
______________________________________________

What I would like to be able to do is something like this:


Dim rCopyRange As Range
Dim rPasteRange As Range

Set rCopyRange = MyNewlyOpenedWorkbook.Range("A1:A5")
Set rPasteRange = Range("A1:A5")


Windows(sOpenJobFileName).Activate
rCopyRange.Select
Selection.Copy

Windows("BIDPROG9.xls").Activate
rPasteRange.Select
Selection.Paste

_____________________________________________

So again I ask. How can I name a range on my newly opened workbook?
Any input would relieve this building headache I'm working on.

Thanks

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Workbook.Range Problem

I'd use a variable that represented that newly opened workbook:

Dim myFileName as variant
dim newWkbk as workbook
dim RngToCopy as range
dim DestCell as range

myfilename = application.getopenfilename("Excel files, *.xls")

if myfilename = false then
exit sub 'user hit cancel
end if

set newwkbk = workbooks.open(filename:=myfilename)

set rngtocopy = newwkbk.worksheets("sheet111").range("a1:a5")

set destcell _
= workbooks("MyWorkbookWithMacro.xls").worksheets("s heet999").range("a1")

rngtocopy.copy _
destination:=destcell




wrote:

I have a workbook that when I click a button it runs a macro that
allows me to open up another workbook of my choice, then copy a
certain range in the newly opened workbook and paste it into my
workbook that ran the macro. My question is, How can I set a range in
my newly opened workbook as a variable. It works when I do it like
this.

______________________________________________

Windows("MyNewlyOpenedWorkbook").Activate
Range("A1:A5").Select
Selection.Copy

Windows("MyWorkbookWithMacro").Activate
Range("A1:A5").Select
Selection.Paste
______________________________________________

What I would like to be able to do is something like this:

Dim rCopyRange As Range
Dim rPasteRange As Range

Set rCopyRange = MyNewlyOpenedWorkbook.Range("A1:A5")
Set rPasteRange = Range("A1:A5")

Windows(sOpenJobFileName).Activate
rCopyRange.Select
Selection.Copy

Windows("BIDPROG9.xls").Activate
rPasteRange.Select
Selection.Paste

_____________________________________________

So again I ask. How can I name a range on my newly opened workbook?
Any input would relieve this building headache I'm working on.

Thanks


--

Dave Peterson
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 40
Default Workbook.Range Problem

Thanks Dave that worked great.

Now my worksheet name in the newly opened workbook is different
depending on the workbook opened but there is always only one
worksheet in the workbook. Is there a variable I can use to get the
worksheet name

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Workbook.Range Problem

If it's always the first (and only sheet):

set rngtocopy = newwkbk.worksheets(1).range("a1:a5")



wrote:

Thanks Dave that worked great.

Now my worksheet name in the newly opened workbook is different
depending on the workbook opened but there is always only one
worksheet in the workbook. Is there a variable I can use to get the
worksheet name


--

Dave Peterson
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 40
Default Workbook.Range Problem

Thanks Dave, again that worked great.

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Copy a Range from each workbook - Ron de Bruin VBA - a problem Philip[_2_] Excel Worksheet Functions 5 March 11th 07 06:07 AM
Name assigned in workbook refers to range problem!!! brucemc[_16_] Excel Programming 4 June 25th 06 10:12 PM
Problem copying a range to a different workbook [email protected] Excel Programming 3 December 8th 04 01:43 AM
Excel VBA - Range.Replace within workbook over several sheets problem mika Excel Programming 1 July 1st 04 03:01 PM
Excel Macro Problem, Add-in need to work in every workbook & Error:9 Subscript out of range Burak[_2_] Excel Programming 1 October 31st 03 08:09 PM


All times are GMT +1. The time now is 05:43 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"