Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 99
Default Getting the LastRow in ExcelSheet from Word

I'm trying to find the last used row in column C in a sheet, using
automation from Word.

I use the following code:


Dim rk As Integer
On Error Resume Next
Set xlapp = GetObject(, "Excel.application")
If Err.Number < 0 Then
Set xlapp = CreateObject("excel.Application")
End If
xlapp.Workbooks.Open FileName:= _
"C:\Documents and Settings\Administrator\Dokumenter\Deltagere.xls"
xlapp.Visible = True
rk = xlapp.activeworkbook.sheets(1).Range("c65536").End (xlUp).Row
It opens the workbook and selects the last used cell in column C, but rk
return a 0 in stead of thec expected 27 in this case. How can I Find the row
number of last used cell in column C?

Jan


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27
Default Getting the LastRow in ExcelSheet from Word

Works fine with me, but it should retrun at least 1 if cell has data.
Is it really sheet 1 you want to check ?
--
Regards

Jean-Yves Tfelt
Europe


"Jan Kronsell" wrote:

I'm trying to find the last used row in column C in a sheet, using
automation from Word.

I use the following code:


Dim rk As Integer
On Error Resume Next
Set xlapp = GetObject(, "Excel.application")
If Err.Number < 0 Then
Set xlapp = CreateObject("excel.Application")
End If
xlapp.Workbooks.Open FileName:= _
"C:\Documents and Settings\Administrator\Dokumenter\Deltagere.xls"
xlapp.Visible = True
rk = xlapp.activeworkbook.sheets(1).Range("c65536").End (xlUp).Row
It opens the workbook and selects the last used cell in column C, but rk
return a 0 in stead of thec expected 27 in this case. How can I Find the row
number of last used cell in column C?

Jan



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 99
Default Getting the LastRow in ExcelSheet from Word

Strange! If I run these lines from Word:

rk = xlapp.ActiveWorkbook.Sheets(1).Range("c65536").End (xlUp).Row
MsgBox rk

it opens the correct workbook, finds the correct sheet, selects the last
cell in row c, containing data, but MsGBOX rk displays a 0. Debug.print
also returns a 0.

If I run these lines, directly from Excel

rk = ActiveWorkbook.Sheets(1).Range("c65536").End(xlUp) .Row
MsgBox rk

It do not select the last cell in row, but stays in C65536m but 27 is
displayed in the MSGBOX as supposed to. So somehow it got to have something
to do with running the code from Word.

Jan

"Nigel" skrev i en meddelelse
...
It works for me, check your references to a worksheet. You might also
like to Dim rk as type long rather than integer (limiting it to 32k rows)

--

Regards,
Nigel




"Jan Kronsell" wrote in message
...
I'm trying to find the last used row in column C in a sheet, using
automation from Word.

I use the following code:


Dim rk As Integer
On Error Resume Next
Set xlapp = GetObject(, "Excel.application")
If Err.Number < 0 Then
Set xlapp = CreateObject("excel.Application")
End If
xlapp.Workbooks.Open FileName:= _
"C:\Documents and Settings\Administrator\Dokumenter\Deltagere.xls"
xlapp.Visible = True
rk = xlapp.activeworkbook.sheets(1).Range("c65536").End (xlUp).Row
It opens the workbook and selects the last used cell in column C, but rk
return a 0 in stead of thec expected 27 in this case. How can I Find the
row number of last used cell in column C?

Jan




  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 735
Default Getting the LastRow in ExcelSheet from Word


I tried running the following from Word and Excel (2007), same code with
different workbook reference, the MsgBox shows in this case 10 as expected.

Sub test()
Dim rk As Integer
On Error Resume Next
Set xlapp = GetObject(, "Excel.application")
If Err.Number < 0 Then
Set xlapp = CreateObject("excel.Application")
End If
xlapp.Workbooks.Open FileName:="D:\Book1.xls"
xlapp.Visible = True

rk = xlapp.ActiveWorkbook.Sheets(1).Range("c65536").End (xlUp).Row

MsgBox rk

End Sub
--

Regards,
Nigel




"Jan Kronsell" wrote in message
...
Strange! If I run these lines from Word:

rk = xlapp.ActiveWorkbook.Sheets(1).Range("c65536").End (xlUp).Row
MsgBox rk

it opens the correct workbook, finds the correct sheet, selects the last
cell in row c, containing data, but MsGBOX rk displays a 0. Debug.print
also returns a 0.

If I run these lines, directly from Excel

rk = ActiveWorkbook.Sheets(1).Range("c65536").End(xlUp) .Row
MsgBox rk

It do not select the last cell in row, but stays in C65536m but 27 is
displayed in the MSGBOX as supposed to. So somehow it got to have
something to do with running the code from Word.

Jan

"Nigel" skrev i en meddelelse
...
It works for me, check your references to a worksheet. You might also
like to Dim rk as type long rather than integer (limiting it to 32k rows)

--

Regards,
Nigel




"Jan Kronsell" wrote in message
...
I'm trying to find the last used row in column C in a sheet, using
automation from Word.

I use the following code:


Dim rk As Integer
On Error Resume Next
Set xlapp = GetObject(, "Excel.application")
If Err.Number < 0 Then
Set xlapp = CreateObject("excel.Application")
End If
xlapp.Workbooks.Open FileName:= _
"C:\Documents and Settings\Administrator\Dokumenter\Deltagere.xls"
xlapp.Visible = True
rk = xlapp.activeworkbook.sheets(1).Range("c65536").End (xlUp).Row
It opens the workbook and selects the last used cell in column C, but
rk return a 0 in stead of thec expected 27 in this case. How can I Find
the row number of last used cell in column C?

Jan







  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 735
Default Getting the LastRow in ExcelSheet from Word

One thought, is the scope of rk catching you out? rk is only valid in this
procedure, you could make it public or pass it back from a function?

--

Regards,
Nigel




"Nigel" wrote in message
...

I tried running the following from Word and Excel (2007), same code with
different workbook reference, the MsgBox shows in this case 10 as
expected.

Sub test()
Dim rk As Integer
On Error Resume Next
Set xlapp = GetObject(, "Excel.application")
If Err.Number < 0 Then
Set xlapp = CreateObject("excel.Application")
End If
xlapp.Workbooks.Open FileName:="D:\Book1.xls"
xlapp.Visible = True

rk = xlapp.ActiveWorkbook.Sheets(1).Range("c65536").End (xlUp).Row

MsgBox rk

End Sub
--

Regards,
Nigel




"Jan Kronsell" wrote in message
...
Strange! If I run these lines from Word:

rk = xlapp.ActiveWorkbook.Sheets(1).Range("c65536").End (xlUp).Row
MsgBox rk

it opens the correct workbook, finds the correct sheet, selects the last
cell in row c, containing data, but MsGBOX rk displays a 0. Debug.print
also returns a 0.

If I run these lines, directly from Excel

rk = ActiveWorkbook.Sheets(1).Range("c65536").End(xlUp) .Row
MsgBox rk

It do not select the last cell in row, but stays in C65536m but 27 is
displayed in the MSGBOX as supposed to. So somehow it got to have
something to do with running the code from Word.

Jan

"Nigel" skrev i en meddelelse
...
It works for me, check your references to a worksheet. You might also
like to Dim rk as type long rather than integer (limiting it to 32k
rows)

--

Regards,
Nigel




"Jan Kronsell" wrote in message
...
I'm trying to find the last used row in column C in a sheet, using
automation from Word.

I use the following code:


Dim rk As Integer
On Error Resume Next
Set xlapp = GetObject(, "Excel.application")
If Err.Number < 0 Then
Set xlapp = CreateObject("excel.Application")
End If
xlapp.Workbooks.Open FileName:= _
"C:\Documents and Settings\Administrator\Dokumenter\Deltagere.xls"
xlapp.Visible = True
rk = xlapp.activeworkbook.sheets(1).Range("c65536").End (xlUp).Row
It opens the workbook and selects the last used cell in column C, but
rk return a 0 in stead of thec expected 27 in this case. How can I Find
the row number of last used cell in column C?

Jan






  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 99
Default Getting the LastRow in ExcelSheet from Word

I tried exactly the same, wih the same workbook as you, 10 rows of data, and
stills get 0. So maybe it's a setting thatas not correct. I don't know where
to look though. I'm running Word and Excel 2003 though.

Jan


"Nigel" skrev i en meddelelse
...

I tried running the following from Word and Excel (2007), same code with
different workbook reference, the MsgBox shows in this case 10 as
expected.

Sub test()
Dim rk As Integer
On Error Resume Next
Set xlapp = GetObject(, "Excel.application")
If Err.Number < 0 Then
Set xlapp = CreateObject("excel.Application")
End If
xlapp.Workbooks.Open FileName:="D:\Book1.xls"
xlapp.Visible = True

rk = xlapp.ActiveWorkbook.Sheets(1).Range("c65536").End (xlUp).Row

MsgBox rk

End Sub
--

Regards,
Nigel




"Jan Kronsell" wrote in message
...
Strange! If I run these lines from Word:

rk = xlapp.ActiveWorkbook.Sheets(1).Range("c65536").End (xlUp).Row
MsgBox rk

it opens the correct workbook, finds the correct sheet, selects the last
cell in row c, containing data, but MsGBOX rk displays a 0. Debug.print
also returns a 0.

If I run these lines, directly from Excel

rk = ActiveWorkbook.Sheets(1).Range("c65536").End(xlUp) .Row
MsgBox rk

It do not select the last cell in row, but stays in C65536m but 27 is
displayed in the MSGBOX as supposed to. So somehow it got to have
something to do with running the code from Word.

Jan

"Nigel" skrev i en meddelelse
...
It works for me, check your references to a worksheet. You might also
like to Dim rk as type long rather than integer (limiting it to 32k
rows)

--

Regards,
Nigel




"Jan Kronsell" wrote in message
...
I'm trying to find the last used row in column C in a sheet, using
automation from Word.

I use the following code:


Dim rk As Integer
On Error Resume Next
Set xlapp = GetObject(, "Excel.application")
If Err.Number < 0 Then
Set xlapp = CreateObject("excel.Application")
End If
xlapp.Workbooks.Open FileName:= _
"C:\Documents and Settings\Administrator\Dokumenter\Deltagere.xls"
xlapp.Visible = True
rk = xlapp.activeworkbook.sheets(1).Range("c65536").End (xlUp).Row
It opens the workbook and selects the last used cell in column C, but
rk return a 0 in stead of thec expected 27 in this case. How can I Find
the row number of last used cell in column C?

Jan







  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Getting the LastRow in ExcelSheet from Word

I'd turn error checking back on before you open the workbook.

Maybe you're masking the error with that "on error resume next" line.



Jan Kronsell wrote:

I'm trying to find the last used row in column C in a sheet, using
automation from Word.

I use the following code:

Dim rk As Integer
On Error Resume Next
Set xlapp = GetObject(, "Excel.application")
If Err.Number < 0 Then
Set xlapp = CreateObject("excel.Application")
End If
xlapp.Workbooks.Open FileName:= _
"C:\Documents and Settings\Administrator\Dokumenter\Deltagere.xls"
xlapp.Visible = True
rk = xlapp.activeworkbook.sheets(1).Range("c65536").End (xlUp).Row
It opens the workbook and selects the last used cell in column C, but rk
return a 0 in stead of thec expected 27 in this case. How can I Find the row
number of last used cell in column C?

Jan


--

Dave Peterson
  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 99
Default Getting the LastRow in ExcelSheet from Word

You got it! Turning error handling back on showed that I was indeed masking
an error. I t was a reference, that was not set properly.

Thank you to Dave and to the rest of you, that have being very helpful.

Jan


"Dave Peterson" skrev i en meddelelse
...
I'd turn error checking back on before you open the workbook.

Maybe you're masking the error with that "on error resume next" line.



Jan Kronsell wrote:

I'm trying to find the last used row in column C in a sheet, using
automation from Word.

I use the following code:

Dim rk As Integer
On Error Resume Next
Set xlapp = GetObject(, "Excel.application")
If Err.Number < 0 Then
Set xlapp = CreateObject("excel.Application")
End If
xlapp.Workbooks.Open FileName:= _
"C:\Documents and Settings\Administrator\Dokumenter\Deltagere.xls"
xlapp.Visible = True
rk = xlapp.activeworkbook.sheets(1).Range("c65536").End (xlUp).Row
It opens the workbook and selects the last used cell in column C, but rk
return a 0 in stead of thec expected 27 in this case. How can I Find the
row
number of last used cell in column C?

Jan


--

Dave Peterson



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
call excelsheet within excelsheet when user clicks excelsheet ico mohini Excel Worksheet Functions 0 November 20th 06 05:07 AM
How to use a macro from another excelsheet Jeroen[_2_] Excel Programming 4 May 24th 06 03:21 PM
Protected Excelsheet ngmahesh Excel Programming 2 January 6th 06 01:31 PM
how to use an excelsheet that is already in my userform Jean-Pierre D via OfficeKB.com Excel Programming 0 August 19th 05 08:19 AM
Go to lastrow using other column's lastrow stakar[_14_] Excel Programming 5 April 16th 04 03:42 PM


All times are GMT +1. The time now is 08:48 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"