PC Review


Reply
Thread Tools Rate Thread

Accessing another workbook's data

 
 
=?Utf-8?B?S2FydGhp?=
Guest
Posts: n/a
 
      14th Aug 2007
Hi All,
My objective is to get used range of rows and columns of another workbook
("trial_2.xls").
I will run the following macro in "trial_1.xls" workbook.
----------------------------------------------
software configuration:
Microsoft office Excel 2003 (11.8142.8132) SP2
Microsoft Visual basic 6.3
----------------------------------------------
Function two()
Dim wbk As Excel.Workbook
Dim sh As Excel.Worksheet
Dim r As Integer
Dim c As Integer
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.EnableEvents = False
Set wbk =
Workbooks.Open("D:\Karthi_Works\trial\Excel_Trials\trial_2.xls", True, True)
sh = wbk.Worksheets(1)
r = sh.UsedRange.Rows.count
c = sh.UsedRange.Columns.count
MsgBox (rc)
wbk.Close
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Application.EnableEvents = True
End Function

When I run this macro in debug mode, the value of 'wbk' shows "Nothing".
Because of this, I could not display used range of rows and columns.
The line "Set wbk =
Workbooks.Open("D:\Karthi_Works\trial\Excel_Trials\trial_2.xls", True, True)"
has
some problem. I couldnot figure it out. please, help me in this regard

Thanks
Karthi
 
Reply With Quote
 
 
 
 
=?Utf-8?B?TWlrZSBI?=
Guest
Posts: n/a
 
      14th Aug 2007
Hi,

I changed it from a fuction to a sub to make it easier for me so change it
back. Also msgbox(rc) doesn't work because it interprets rc as a new variable
and it's never populated so I changed that:-

Sub two()
Dim wbk As Excel.Workbook
Dim sh As Excel.Worksheet
Dim r As Integer
Dim c As Integer
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.EnableEvents = False
Set wbk = Workbooks.Open("c:\june.xls", True, True) 'Change back to your path
Set sh = wbk.Worksheets(1)
r = sh.UsedRange.Rows.Count
c = sh.UsedRange.Columns.Count
MsgBox ("Rows used " & r & " Columns used " & c)
wbk.Close
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Application.EnableEvents = True
End Sub


Mike

"Karthi" wrote:

> Hi All,
> My objective is to get used range of rows and columns of another workbook
> ("trial_2.xls").
> I will run the following macro in "trial_1.xls" workbook.
> ----------------------------------------------
> software configuration:
> Microsoft office Excel 2003 (11.8142.8132) SP2
> Microsoft Visual basic 6.3
> ----------------------------------------------
> Function two()
> Dim wbk As Excel.Workbook
> Dim sh As Excel.Worksheet
> Dim r As Integer
> Dim c As Integer
> Application.ScreenUpdating = False
> Application.DisplayAlerts = False
> Application.EnableEvents = False
> Set wbk =
> Workbooks.Open("D:\Karthi_Works\trial\Excel_Trials\trial_2.xls", True, True)
> sh = wbk.Worksheets(1)
> r = sh.UsedRange.Rows.count
> c = sh.UsedRange.Columns.count
> MsgBox (rc)
> wbk.Close
> Application.ScreenUpdating = True
> Application.DisplayAlerts = True
> Application.EnableEvents = True
> End Function
>
> When I run this macro in debug mode, the value of 'wbk' shows "Nothing".
> Because of this, I could not display used range of rows and columns.
> The line "Set wbk =
> Workbooks.Open("D:\Karthi_Works\trial\Excel_Trials\trial_2.xls", True, True)"
> has
> some problem. I couldnot figure it out. please, help me in this regard
>
> Thanks
> Karthi

 
Reply With Quote
 
=?Utf-8?B?S2FydGhp?=
Guest
Posts: n/a
 
      21st Aug 2007
Thanks a lot Mike.

The code is working fine when it is written in "sub two() ... end sub".
But, it is not working when it is written in "function two() .. end
function". Could you explain me the difference between Function and Sub in
excel VBA macro?

Thanks & Regards
Karthi

"Mike H" wrote:

> Hi,
>
> I changed it from a fuction to a sub to make it easier for me so change it
> back. Also msgbox(rc) doesn't work because it interprets rc as a new variable
> and it's never populated so I changed that:-
>
> Sub two()
> Dim wbk As Excel.Workbook
> Dim sh As Excel.Worksheet
> Dim r As Integer
> Dim c As Integer
> Application.ScreenUpdating = False
> Application.DisplayAlerts = False
> Application.EnableEvents = False
> Set wbk = Workbooks.Open("c:\june.xls", True, True) 'Change back to your path
> Set sh = wbk.Worksheets(1)
> r = sh.UsedRange.Rows.Count
> c = sh.UsedRange.Columns.Count
> MsgBox ("Rows used " & r & " Columns used " & c)
> wbk.Close
> Application.ScreenUpdating = True
> Application.DisplayAlerts = True
> Application.EnableEvents = True
> End Sub
>
>
> Mike
>
> "Karthi" wrote:
>
> > Hi All,
> > My objective is to get used range of rows and columns of another workbook
> > ("trial_2.xls").
> > I will run the following macro in "trial_1.xls" workbook.
> > ----------------------------------------------
> > software configuration:
> > Microsoft office Excel 2003 (11.8142.8132) SP2
> > Microsoft Visual basic 6.3
> > ----------------------------------------------
> > Function two()
> > Dim wbk As Excel.Workbook
> > Dim sh As Excel.Worksheet
> > Dim r As Integer
> > Dim c As Integer
> > Application.ScreenUpdating = False
> > Application.DisplayAlerts = False
> > Application.EnableEvents = False
> > Set wbk =
> > Workbooks.Open("D:\Karthi_Works\trial\Excel_Trials\trial_2.xls", True, True)
> > sh = wbk.Worksheets(1)
> > r = sh.UsedRange.Rows.count
> > c = sh.UsedRange.Columns.count
> > MsgBox (rc)
> > wbk.Close
> > Application.ScreenUpdating = True
> > Application.DisplayAlerts = True
> > Application.EnableEvents = True
> > End Function
> >
> > When I run this macro in debug mode, the value of 'wbk' shows "Nothing".
> > Because of this, I could not display used range of rows and columns.
> > The line "Set wbk =
> > Workbooks.Open("D:\Karthi_Works\trial\Excel_Trials\trial_2.xls", True, True)"
> > has
> > some problem. I couldnot figure it out. please, help me in this regard
> >
> > Thanks
> > Karthi

 
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
accessing data in backend workbook over vpn, is it possible? Jeff VA Microsoft Excel Programming 0 4th Nov 2007 10:14 PM
loop through a column on a workbook copying data on each row to another workbook, then copy data back to the original workbook burl_rfc Microsoft Excel Programming 1 1st Apr 2006 08:48 PM
accessing data from other workbook Tony Microsoft Excel Programming 4 24th Jun 2004 12:38 PM
Accessing Data from Closed Workbook Wolf Microsoft Excel Programming 0 5th Sep 2003 09:39 PM
Re: Accessing Data from Closed Workbook Andy Wiggins Microsoft Excel Programming 0 5th Sep 2003 06:42 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:13 PM.