Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default With statements

Simple question...

My idea is that when the user clicks a commandbutton, a userform is
displayed that is pulling dat from 2 workbooks. I wanted to use a
with.range function to populate some of the data from another workbook.
In workbook1, I do have a named range referencing data in workbook2,
however, when VBA hits that with range statement, it errors out since
the worksheet isnt an internal worksheet.

Is there a way with the "with range" statement to reference another
workbook? That workbook is open.

TIA

DS

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,058
Default With statements

Sub demo()
With Workbooks("Book1").Worksheets("Sheet1")
v = .Range("A1").Value
MsgBox (v)
End With
End Sub
--
Gary's Student


" wrote:

Simple question...

My idea is that when the user clicks a commandbutton, a userform is
displayed that is pulling dat from 2 workbooks. I wanted to use a
with.range function to populate some of the data from another workbook.
In workbook1, I do have a named range referencing data in workbook2,
however, when VBA hits that with range statement, it errors out since
the worksheet isnt an internal worksheet.

Is there a way with the "with range" statement to reference another
workbook? That workbook is open.

TIA

DS


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10,593
Default With statements

It is nothing intrinsic about With that does this, it is just a matter of
fully qualifying the range with the book and sheet.

For example

MsgBox Workbooks("example.xls").Worksheets(1).Range("Bob" ).Rows.Count

With comes into its own when you want to repeatedly reference an object, so
by using with at the start you make the code more readable, and more
efficient, as the object model doesn't have to be fully resolved for each
command.

For example

Workbooks("example.xls").Worksheets(1).Range("A1") .Value = "Column 1"
Workbooks("example.xls").Worksheets(1).Range("B1") .Value = "Column 2"
Workbooks("example.xls").Worksheets(1).Range("C1") .Value = "Column 3"
Workbooks("example.xls").Worksheets(1).Range("D1") .Value = "Column 4"
Workbooks("example.xls").Worksheets(1).Range("E1") .Value = "Column 3"

becomes

With Workbooks("example.xls").Worksheets(1)
.Range("A1").Value = "Column 1"
.Range("B1").Value = "Column 2"
.Range("C1").Value = "Column 3"
.Range("D1").Value = "Column 4"
.Range("E1").Value = "Column 3"
End With

--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)

wrote in message
ups.com...
Simple question...

My idea is that when the user clicks a commandbutton, a userform is
displayed that is pulling dat from 2 workbooks. I wanted to use a
with.range function to populate some of the data from another workbook.
In workbook1, I do have a named range referencing data in workbook2,
however, when VBA hits that with range statement, it errors out since
the worksheet isnt an internal worksheet.

Is there a way with the "with range" statement to reference another
workbook? That workbook is open.

TIA

DS



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default With statements

Thanks a bunch!

I was drawing blank..

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
IF statements profmorse Excel Discussion (Misc queries) 5 December 20th 06 07:41 PM
IF Statements (Mutliple Statements) Deezel Excel Worksheet Functions 3 October 19th 06 06:13 AM
operator statements, shorting when reusing one of the statements? KR Excel Programming 1 August 4th 05 06:20 PM
If...then statements? Julie[_4_] Excel Programming 3 October 16th 03 03:44 PM
us IF statements Anthony[_7_] Excel Programming 1 October 2nd 03 02:56 PM


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

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

About Us

"It's about Microsoft Excel"