#1   Report Post  
Pank
 
Posts: n/a
Default Help with VBA Code

I have the following as part of some VBA:-

lLastRow = wks.Range("A1").End(xlDown).Row
wks.Cells(lLastRow + 1, 1).Formula = "TOTAL VALUE"
wks.Cells(lLastRow + 1, 2).Formula = "=COUNTA(B2:B&1LastRow)"

The first line ascertains the last row that has a value.

The second line sets a formula in the last row + 1 (i.e. sets value to TOTAL
VALUE).

The third line is intended to use the COUNTA function to count all rows from
B2 to the last row.

When I run this I get, Run Time error 1004, Application defined or object
defined error.

Basically, all I the last line to do is effectively do something like
COUNTA(B2:B383). Please note that 383 will be a different value in different
sheets.


Any help offered would be most appreciated.

Pank

  #2   Report Post  
Juan Pablo González
 
Posts: n/a
Default

You need to have the variable outside of the string:

wks.Cells(lLastRow + 1, 2).Formula = "=COUNTA(B2:B&" & lLastRow & ")"


--
Regards,

Juan Pablo González
Excel MVP

"Pank" wrote in message
...
I have the following as part of some VBA:-

lLastRow = wks.Range("A1").End(xlDown).Row
wks.Cells(lLastRow + 1, 1).Formula = "TOTAL VALUE"
wks.Cells(lLastRow + 1, 2).Formula = "=COUNTA(B2:B&1LastRow)"

The first line ascertains the last row that has a value.

The second line sets a formula in the last row + 1 (i.e. sets value to
TOTAL
VALUE).

The third line is intended to use the COUNTA function to count all rows
from
B2 to the last row.

When I run this I get, Run Time error 1004, Application defined or object
defined error.

Basically, all I the last line to do is effectively do something like
COUNTA(B2:B383). Please note that 383 will be a different value in
different
sheets.


Any help offered would be most appreciated.

Pank



  #3   Report Post  
JE McGimpsey
 
Posts: n/a
Default

one way:

With wks
lLastRow = .Range("A1").End(xlDown).Row
.Cells(lLastRow + 1, 1).Value = "TOTAL VALUE"
.Cells(lLastRow + 1, 2).Formula = "=COUNTA(B2:B" & lLastRow & ")"
End With

In article ,
"Pank" wrote:

I have the following as part of some VBA:-

lLastRow = wks.Range("A1").End(xlDown).Row
wks.Cells(lLastRow + 1, 1).Formula = "TOTAL VALUE"
wks.Cells(lLastRow + 1, 2).Formula = "=COUNTA(B2:B&1LastRow)"

The first line ascertains the last row that has a value.

The second line sets a formula in the last row + 1 (i.e. sets value to TOTAL
VALUE).

The third line is intended to use the COUNTA function to count all rows from
B2 to the last row.

When I run this I get, Run Time error 1004, Application defined or object
defined error.

Basically, all I the last line to do is effectively do something like
COUNTA(B2:B383). Please note that 383 will be a different value in different
sheets.


Any help offered would be most appreciated.

Pank

  #4   Report Post  
Pank
 
Posts: n/a
Default

Juan, JE,

Worked a treat as expected.

Many thanks for the quick responses.

Regards

Pank

"JE McGimpsey" wrote:

one way:

With wks
lLastRow = .Range("A1").End(xlDown).Row
.Cells(lLastRow + 1, 1).Value = "TOTAL VALUE"
.Cells(lLastRow + 1, 2).Formula = "=COUNTA(B2:B" & lLastRow & ")"
End With

In article ,
"Pank" wrote:

I have the following as part of some VBA:-

lLastRow = wks.Range("A1").End(xlDown).Row
wks.Cells(lLastRow + 1, 1).Formula = "TOTAL VALUE"
wks.Cells(lLastRow + 1, 2).Formula = "=COUNTA(B2:B&1LastRow)"

The first line ascertains the last row that has a value.

The second line sets a formula in the last row + 1 (i.e. sets value to TOTAL
VALUE).

The third line is intended to use the COUNTA function to count all rows from
B2 to the last row.

When I run this I get, Run Time error 1004, Application defined or object
defined error.

Basically, all I the last line to do is effectively do something like
COUNTA(B2:B383). Please note that 383 will be a different value in different
sheets.


Any help offered would be most appreciated.

Pank


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
Change case...help please Terry Excel Worksheet Functions 14 October 2nd 05 12:29 PM
Macro for changing text to Proper Case JPriest Excel Worksheet Functions 3 August 8th 05 09:31 PM
Using other workbooks.. DavidMunday Excel Worksheet Functions 2 July 1st 05 07:35 AM
Make Change Case in Excel a format rather than formula Kevin Excel Worksheet Functions 1 March 18th 05 08:53 PM
Opening a file with code without a set file name jenkinspat Excel Discussion (Misc queries) 1 March 4th 05 10:50 AM


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