syntax question with VBA

bdee1

Board Regular
Joined
Feb 17, 2003
Messages
105
i have a loop that determines the first blank row in a worksheet - it is as follows:

Code:
x = 2
  
  Sheets("eRehab_Download").Select
  Range("A1").Select
  
  Do While Not IsEmpty(Range("A" & x))
    x = x + 1
  Loop

and what i want to do is paste a formula 11 rows after the last row contacting data so i am using the following:

Code:
Range("B" & x + 11).Select
  ActiveCell.Formula = "=COUNTA(B4:BX)"

problem is in the line where i say COUNT(B4:BX)

obviously i want X to represent the number of the last row containing data but it is coming out as the letter X in the formula.

what do i need to change to get the to get the formula to have the value of the variable X instead of the letter X?
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
This should do everything you want without an unnecessary loop:
Code:
    Dim myLastRow As Long
    myLastRow = Range("A65536").End(xlUp).Row
    Cells(myLastRow + 11, 2).Formula = "=COUNTA(B4:B" & myLastRow & ")"
 
Upvote 0
i have seen the method you are suggesting (to avoid the loop) but unfortunately it does not work for me because the first 3 rows of the worksheet i am working with are the header and it includes a blank row. so your method always returns the 2nd row... i needed something to start with row 4 and find the next blank row...

but with regard to the notation for setting the formula -
i tried Cells(myLastRow + 11, 2).Formula = "=COUNTA(B4:B" & myLastRow & ")" and it worked! thanks!

before i tried Activecell.Formula = "=COUNTA(B4:B" & X & ")" and it gave me a compile error but for whatever reason your notation worked.

thanks again!
 
Upvote 0
You still don't need a loop. Try:
Code:
    Dim myLastRow As Long 
    myLastRow = Range("A4").End(xlDown).Row 
    Cells(myLastRow + 11, 2).Formula = "=COUNTA(B4:B" & myLastRow & ")"

If you can have row 4 as the first blank row, simply change
myLastRow = Range("A4").End(xlDown).Row
to
myLastRow = Range("A3").End(xlDown).Row
 
Upvote 0
of course - i dont know why i didnt see that before - thank you for the tip

you guys (the mr excel community) are the best!
 
Upvote 0

Forum statistics

Threads
1,214,520
Messages
6,120,016
Members
448,936
Latest member
almerpogi

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top