paste a formula in a cell with vba

rjplante

Well-known Member
Joined
Oct 31, 2008
Messages
569
Office Version
  1. 365
Platform
  1. Windows
I have a table that has the number of columns defined when it is opened for the first time. Column A is always a reference column and the number of columns is entered into cell CF1 and starts in column B. If the user enters 8 columns, there will be nine columns to the table. Everything on the table works great and the data is entered as needed. Some rows will have a single cell with data in it, and others will have multiple cells containing data. I want to sort out the single entry rows and then move them to another sheet. I have added a cell on the outside of the table, skipping a column and pasting a formula in the cell. The problem I am having is that when the formula is in the cell is using absolute references (=COUNTIF($B$5:$I$5,$A$5))as opposed to general references (=COUNTIF(B5:I5,A5)). I want to be able to fill down to the last row with the formula so I cannot have any absolute references in the formula (at least not the row references). My VBA code to place the formula is listed below. How can I fix it to remove the absolute references?

VBA Code:
Dim MoveCells As Long

Cells(5, Range("CF1") + 3).Select

MoveCells = Range("CF1").Value


'   Add formula to outside of table
ActiveCell.Formula = "=COUNTIF(" & ActiveCell.Offset(0, -(MoveCells + 1)).Address & ":" & ActiveCell.Offset(0, -2).Address & "," & ActiveCell.Offset(0, -(MoveCells + 2)).Address & ")"

Thanks for the help
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Try this:

VBA Code:
ActiveCell.Formula = "=COUNTIF(" & ActiveCell.Offset(0, -(MoveCells + 1)).address(0, 0) & ":" & ActiveCell.Offset(0, -2).address(0, 0) & "," & ActiveCell.Offset(0, -(MoveCells + 2)).address(0, 0) & ")"
 
Upvote 0
Try this:

VBA Code:
ActiveCell.Formula = "=COUNTIF(" & ActiveCell.Offset(0, -(MoveCells + 1)).address(0, 0) & ":" & ActiveCell.Offset(0, -2).address(0, 0) & "," & ActiveCell.Offset(0, -(MoveCells + 2)).address(0, 0) & ")"
Dante, you are an Excel wizard!
 
Upvote 0
I'm glad to help you. I appreciate your kind comments.
 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,272
Members
449,075
Latest member
staticfluids

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