Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista - Finding the last row of and excel worksheet.

Reply
 
Old 03-31-2009   #1 (permalink)
GBPackerBacker


 
 

Finding the last row of and excel worksheet.

Hi all, I have a script that goes out and searches a spreadsheet for
information. If it finds a null value, I need my script to open up
another logging spreadsheet and look into Column B (Column A has
preset Error ID numbers to help identify the problem) and find the
next open row, then input a text description.

I have everythign working great, except for I can't for the life of me
figure out what code I'm supposed to put in to find the next available
row in excel.

'~~~~~~~~~~~Visual of what my Error Spreadsheet should
read~~~~~~~~~~~~~~~
Col A Col B
ID # Issue
ID 2 Null Value
ID 3 Null Value
ID 4
ID 5

'~~~~~~~~~~~My script that needs help - Not all of
script~~~~~~~~~~~~~~~~~~~~~~~~
Set objExcel = CreateObject("Excel.Application")
objExcel.Workbooks.Open ("C:\Excelfilename.xls")
Set objSheet = objExcel.ActiveWorkbook.WorkSheets(1)

IntRow = 9
K = 11
R = 18

if objExcel.Cells(intRow, K).Value <> "" then
objExcel.Cells(intRow, R).Interior.ColorIndex = 4
else
objExcel.Visible = False
objExcel.Workbooks.Open("C:\ErrorLog.xls")
objExcel.Cells(NEXTOPENROW, R) = "Null Value" <==========THIS IS
WHERE I NEED HELP========
End If

I put NEXTOPENROW in the above script to let you know that's where i
need help with, that's not part of my script, I have no idea how to
find the next open row....any help is appriciated!!!!!

My System SpecsSystem Spec
Old 03-31-2009   #2 (permalink)
ekkehard.horner


 
 

Re: Finding the last row of and excel worksheet.

GBPackerBacker schrieb:
Quote:

> Hi all, I have a script that goes out and searches a spreadsheet for
> information. If it finds a null value, I need my script to open up
> another logging spreadsheet and look into Column B (Column A has
> preset Error ID numbers to help identify the problem) and find the
> next open row, then input a text description.
>
> I have everythign working great, except for I can't for the life of me
> figure out what code I'm supposed to put in to find the next available
> row in excel.
[...]
Try something like:

Const xlLastCell = 11
... = <WorkSheet>.UsedRange.SpecialCells( xlLastCell )
My System SpecsSystem Spec
Old 03-31-2009   #3 (permalink)
Pegasus [MVP]


 
 

Re: Finding the last row of and excel worksheet.


"GBPackerBacker" <gbpackerbacker@xxxxxx> wrote in message
news:2699deec-49bc-44c3-a02b-c30f0a99029c@xxxxxx
Quote:

> Hi all, I have a script that goes out and searches a spreadsheet for
> information. If it finds a null value, I need my script to open up
> another logging spreadsheet and look into Column B (Column A has
> preset Error ID numbers to help identify the problem) and find the
> next open row, then input a text description.
>
> I have everythign working great, except for I can't for the life of me
> figure out what code I'm supposed to put in to find the next available
> row in excel.
>
> '~~~~~~~~~~~Visual of what my Error Spreadsheet should
> read~~~~~~~~~~~~~~~
> Col A Col B
> ID # Issue
> ID 2 Null Value
> ID 3 Null Value
> ID 4
> ID 5
>
> '~~~~~~~~~~~My script that needs help - Not all of
> script~~~~~~~~~~~~~~~~~~~~~~~~
> Set objExcel = CreateObject("Excel.Application")
> objExcel.Workbooks.Open ("C:\Excelfilename.xls")
> Set objSheet = objExcel.ActiveWorkbook.WorkSheets(1)
>
> IntRow = 9
> K = 11
> R = 18
>
> if objExcel.Cells(intRow, K).Value <> "" then
> objExcel.Cells(intRow, R).Interior.ColorIndex = 4
> else
> objExcel.Visible = False
> objExcel.Workbooks.Open("C:\ErrorLog.xls")
> objExcel.Cells(NEXTOPENROW, R) = "Null Value" <==========THIS IS
> WHERE I NEED HELP========
> End If
>
> I put NEXTOPENROW in the above script to let you know that's where i
> need help with, that's not part of my script, I have no idea how to
> find the next open row....any help is appriciated!!!!!
I faced the same problem several months ago. With a fair bit of googling I
found this solution:
Function LastRow
Const xlCellTypeLastCell = 11
oSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Activate
LastRow = oExcel.ActiveCell.Row
End LastRow


My System SpecsSystem Spec
Old 03-31-2009   #4 (permalink)
GBPackerBacker


 
 

Re: Finding the last row of and excel worksheet.

On Mar 31, 1:56*pm, "ekkehard.horner" <ekkehard.hor...@xxxxxx>
wrote:
Quote:

> GBPackerBacker schrieb:> Hi all, I have a script that goes out and searches a spreadsheet for
Quote:

> > information. If it finds a null value, I need my script to open up
> > another logging spreadsheet and look into Column B (Column A has
> > preset Error ID numbers to help identify the problem) and find the
> > next open row, then input a text description.
>
Quote:

> > I have everythign working great, except for I can't for the life of me
> > figure out what code I'm supposed to put in to find the next available
> > row in excel.
>
> [...]
> Try something like:
>
> * *Const xlLastCell = 11
> * *... = <WorkSheet>.UsedRange.SpecialCells( xlLastCell )

Thanks for the info, I'll see if I can get it to work, in the
meantime, can you explain a little more? What is the significance of
xlLastCell = 11. What does 11 mean? Also, once I find the cell, how
do I set that row number as a variable? For instance, once the cell
is found, I want to input a number into the cell, but also use that
same row to enter in information in a couple different columns....
Example below...


else
objExcel.Visible = False
objExcel.Workbooks.Open("C:\ErrorLog.xls")
varRow = <WorkSheet>.UsedRange.SpecialCells( xlLastCell )
objExcel.Cells(VarRow, R) = "Null Value"
objExcel.Cells(VarRow, S) = "Today's Date"
IdNum = (contentsofpreviouscell) +1
objExcel.Cells(VarRow, A) = IdNum
End If


My System SpecsSystem Spec
Old 03-31-2009   #5 (permalink)
GBPackerBacker


 
 

Re: Finding the last row of and excel worksheet.

Okay Pegasus, I can get your script to work great on a test excel
spreadsheet, but when I plug it into my master script (snippet below)
it errors out with the error: "Activate method of Range class failed"
Code: 800A03EC. Any ideas?

else
objExcel.Visible = False
objExcel.Workbooks.Open("C:\test.xls")
Const xlCellTypeLastCell = 11
objSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Activate
LastRow = objExcel.ActiveCell.Row
FirstOpenRow = LastRow + 1
objExcel.Cells(FirstOpenRow, B) = strComputer
objExcel.Cells(FirstOpenRow, G) = "NULL VALUE for MAC Address, in
row " & intRow & ", Column K"
End If
My System SpecsSystem Spec
Old 03-31-2009   #6 (permalink)
Pegasus [MVP]


 
 

Re: Finding the last row of and excel worksheet.


"GBPackerBacker" <gbpackerbacker@xxxxxx> wrote in message
news:0509e9d6-b01a-4107-82a2-6b1943e7ede8@xxxxxx
Quote:

> Okay Pegasus, I can get your script to work great on a test excel
> spreadsheet, but when I plug it into my master script (snippet below)
> it errors out with the error: "Activate method of Range class failed"
> Code: 800A03EC. Any ideas?
>
> else
> objExcel.Visible = False
> objExcel.Workbooks.Open("C:\test.xls")
> Const xlCellTypeLastCell = 11
> objSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Activate
> LastRow = objExcel.ActiveCell.Row
> FirstOpenRow = LastRow + 1
> objExcel.Cells(FirstOpenRow, B) = strComputer
> objExcel.Cells(FirstOpenRow, G) = "NULL VALUE for MAC Address, in
> row " & intRow & ", Column K"
> End If
Can't tell - you're not showing us your definition of objSheet. It should be
something like
Set objSheet = objWorkbook.ActiveSheet

and objWorkbook could be something like

Set objWorkbook = objExcel.Workbooks.Open("d:\temp.xls")

Try experimenting a little - it's very productive!


My System SpecsSystem Spec
Old 03-31-2009   #7 (permalink)
ekkehard.horner


 
 

Re: Finding the last row of and excel worksheet.

GBPackerBacker schrieb:
Quote:

> On Mar 31, 1:56 pm, "ekkehard.horner" <ekkehard.hor...@xxxxxx>
> wrote:
Quote:

>> GBPackerBacker schrieb:> Hi all, I have a script that goes out and searches a spreadsheet for
Quote:

>>> information. If it finds a null value, I need my script to open up
>>> another logging spreadsheet and look into Column B (Column A has
>>> preset Error ID numbers to help identify the problem) and find the
>>> next open row, then input a text description.
>>> I have everythign working great, except for I can't for the life of me
>>> figure out what code I'm supposed to put in to find the next available
>>> row in excel.
>> [...]
>> Try something like:
>>
>> Const xlLastCell = 11
>> ... = <WorkSheet>.UsedRange.SpecialCells( xlLastCell )
>
>
> Thanks for the info, I'll see if I can get it to work, in the
> meantime, can you explain a little more? What is the significance of
> xlLastCell = 11. What does 11 mean? Also, once I find the cell, how
> do I set that row number as a variable? For instance, once the cell
> is found, I want to input a number into the cell, but also use that
> same row to enter in information in a couple different columns....
> Example below...
[...]

The Const xlLastCell must be defined, because VBScript doesn't know
about this Excel secrets.

This sample code:

Const xlLastCell = 11

Dim oFS : Set oFS = CreateObject( "Scripting.FileSystemObject" )
Dim sFSpec : sFSpec = oFS.GetAbsolutePathName( <your.xls> )

Dim oExcel : Set oExcel = CreateObject( "Excel.Application" )
Dim oWBook : Set oWBook = oExcel.Workbooks.Open( sFSpec )
Dim oSheet : Set oSheet = oWBook.Sheets( <yoursheet> )
Dim oLCell : Set oLCell = oSheet.UsedRange.SpecialCells( xlLastCell )

WScript.Echo oSheet.Cells( oLCell.Row, oLCell.Column )
oSheet.Cells( oLCell.Row + 1, oLCell.Column ) = Now

oWBook.Save
oWBook.Close
oExcel.Quit

appends the current date/time to the sheet.
My System SpecsSystem Spec
Old 03-31-2009   #8 (permalink)
ekkehard.horner


 
 

Re: Finding the last row of and excel worksheet.

Pegasus [MVP] schrieb:
Quote:

> "GBPackerBacker" <gbpackerbacker@xxxxxx> wrote in message
> news:0509e9d6-b01a-4107-82a2-6b1943e7ede8@xxxxxx
Quote:

>> Okay Pegasus, I can get your script to work great on a test excel
>> spreadsheet, but when I plug it into my master script (snippet below)
>> it errors out with the error: "Activate method of Range class failed"
>> Code: 800A03EC. Any ideas?
>>
>> else
>> objExcel.Visible = False
>> objExcel.Workbooks.Open("C:\test.xls")
>> Const xlCellTypeLastCell = 11
>> objSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Activate
>> LastRow = objExcel.ActiveCell.Row
>> FirstOpenRow = LastRow + 1
>> objExcel.Cells(FirstOpenRow, B) = strComputer
>> objExcel.Cells(FirstOpenRow, G) = "NULL VALUE for MAC Address, in
>> row " & intRow & ", Column K"
>> End If
>
> Can't tell - you're not showing us your definition of objSheet. It should be
> something like
> Set objSheet = objWorkbook.ActiveSheet
>
> and objWorkbook could be something like
>
> Set objWorkbook = objExcel.Workbooks.Open("d:\temp.xls")
>
> Try experimenting a little - it's very productive!
>
>
In general, I'd try to avoid Activ* when automating Excel from
VBScript, because it is tied to interactive use.
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Can not open an existing Excel worksheet PowerShell
Error when updating Access table from Excel Worksheet VB Script
Excel Worksheet Tabs Disappeared in vista 64 Microsoft Office
How to determin a worksheet exist in Excel? PowerShell
Excel Worksheet PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46