Variable Referencing problem

jbesant

New Member
Joined
Jun 8, 2010
Messages
23
Hi everyone:

I am using the vba code below to input a formula into excel cells. However, I want to make the "A:A" and the "D3" in the formula relative to the variable c2 (which is an integer). Does anyone know how to do this?

Cells(3, 3).Offset(0, c2) = "=IF(COUNTIF(" & "A:A" & "," & "D3" & " )=0,""Not Found"",""OK"")"

Thanks so much for your help!

Jamie
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Try:
Code:
Cells(3, 3).Offset(0, c2) = "=IF(COUNTIF(" & "A:A" & "," & "D" & c2 & " )=0,""Not Found"",""OK"")"

assuming c2 is an integer equal to 10, the formula should insert into the cell as
=IF(COUNTIF(A:A,D10)=0,"Not Found","OK")

if its equal to 34, the formula should insert into the cell as
=IF(COUNTIF(A:A,D34)=0,"Not Found","OK")

You may want to add some form of error checking to make sure that the value of C2 is a valid interger of at least 1.
 
Upvote 0
A:A wouldn't change (it is a whole column reference) but what would you expect D3 to be depending on c2?
 
Upvote 0
Canadapip - thanks for noticing that "D3" could be changed so simply. I will create another variable (x=c2+3), and concantate from there.

I still need to change the column reference according to c2. Reason for this is that I'm using a loop and each time it runs, I need to column reference to change by c2 to find the appropriate column.
 
Upvote 0
Sorry Peter - just read your reply... thanks for the interest in my problem. Do you have any ideas how to change the column reference?
 
Upvote 0
If c2 cannot exceed 25 then perhaps

Code:
Cells(3, 3).Offset(0, c2) = "=IF(COUNTIF(" & Chr(34) & Chr(1 + c2) & Chr(34) & "," & "D" & c2 & " )=0,""Not Found"",""OK"")"
 
Upvote 0
I haven't tested the code but it compiles OK.

Any indication of what is wrong?
 
Upvote 0

Forum statistics

Threads
1,223,244
Messages
6,170,976
Members
452,372
Latest member
Natalie18

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