Error 1004 with line of macro code that writes CountIf formulas to cells in sheet

johnnyL

Well-known Member
Joined
Nov 7, 2011
Messages
4,546
Office Version
  1. 2007
Platform
  1. Windows
Need some assistance please with the following line of code:

VBA Code:
WS.Range(Cells(LastRowOfNamesColumnC + BottomFormulasRowCounter, BottomFormulasColumnCounter)).Formula = "=CountIf(" & WS.Range(Cells(StartingRowOfNames, BottomFormulasColumnCounter) & ", " & Cells(LastRowOfNamesColumnC, BottomFormulasColumnCounter)) & ", ""*X*"")"

That line gives error 1004, Method 'Range' of object '_Worksheet' failed.

The formula is meant to count the 'X's in each column

I have chosen the 'Cells' route because the formula is to be written across the row, so the column has to be incremented.

I'm pretty sure I probably messed something up in the formula part with the quotations and the '&'s, but I just can't seem to figure it out using the 'Cells' way.

Any help is appreciated.
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Try this:

Rich (BB code):
  ws.Range(ws.Cells(LastRowOfNamesColumnC + BottomFormulasRowCounter, _
    BottomFormulasColumnCounter).Address).Formula = _
    "=CountIf(" & ws.Range(ws.Cells(StartingRowOfNames, BottomFormulasColumnCounter), _
      ws.Cells(LastRowOfNamesColumnC, BottomFormulasColumnCounter)).Address & ",""*X*"")"

Tip: It would help if the variable names are shorter.
 
Upvote 0
Thank you @DanteAmor for the response!

I actually found an easier way to accomplish what I was trying to achieve. I used the 'resize' option to handle the looping which allowed me to abandon the 'Cells' method also. :)

VBA Code:
WS.Range("D" & LastRowOfNamesColumnC + 1).Resize(1, 31).Formula = "=CountIf(D" & StartingRowOfNames & ":D" & LastRowOfNamesColumnC & ", ""*X*"")"

Again, Thank you so much for looking into my problem.
 
Upvote 0

Forum statistics

Threads
1,213,554
Messages
6,114,280
Members
448,562
Latest member
Flashbond

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