How to insert formula in VBA?

Kishan

Well-known Member
Joined
Mar 15, 2011
Messages
1,648
Office Version
  1. 2010
Platform
  1. Windows
Hello to all,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
<o:p></o:p>
I have a formula shown below in cell Q3 and copied it down to cell Q59051 and it inserts with control + shift + enter keys. <o:p></o:p>
Code:
=MAX(FREQUENCY(IF($B3:$O3=1,COLUMN($B3:$O3)),IF($B3:$O3<>1,COLUMN($B3:$O3))))
<o:p></o:p>

<o:p></o:p>
Is it possible to use this formula placing it in the VBA and run the code from cell Q3 to cell Q59051 and giving the result only in the cells? <o:p></o:p>
<o:p></o:p>
Thanks in advance,<o:p></o:p>
Kishan<o:p></o:p>
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Hello to all,

I have a formula shown below in cell Q3 and copied it down to cell Q59051 and it inserts with control + shift + enter keys.
Code:

=MAX(FREQUENCY(IF($B3:$O3=1,COLUMN($B3:$O3)),IF($B3:$O3<>1,COLUMN($B3:$O3))))</pre>


Is it possible to use this formula placing it in the VBA and run the code from cell Q3 to cell Q59051 and giving the result only in the cells?

Thanks in advance,
Kishan

Is this what you wanted, to copy and paste the values after running the formula?


Code:
sub NameHere()

    range("q3:q59051").select
    selection.formula = "=MAX(FREQUENCY(IF($B3:$O3=1,COLUMN($B3:$O3)),IF($B3:$O3<>1,COLUMN($B3:$O3))))"
    selection.copy
    selection.pastespecial paste:=xlPasteValues

end sub
 
Upvote 0
One way:

Code:
Sub k()
    With Range("Q3:Q59050")
        .Cells(1).FormulaArray = "=MAX(FREQUENCY(IF($B3:$O3=1, COLUMN($B3:$O3)), IF($B3:$O3<>1, COLUMN($B3:$O3))))"
        .Cells(1).Copy .Offset(1)
        .Value = .Value
    End With
End Sub
 
Upvote 0
Bjotgen, your code produces this error: #¡VALUE! Anyway, thank you for your collaboration. <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
<o:p></o:p>
Shg, your 5 line code has reduced the workbook size nearly 6.7MB. Now the workbook opens and closes quickly. <o:p></o:p>
<o:p></o:p>
Really, I appreciate your help for giving me the code and saving my time while working with long data’s.<o:p></o:p>
<o:p></o:p>
Regards,<o:p></o:p>
Kishan
 
Upvote 0
You're welcome, glad it worked for you.
 
Upvote 0
Shg, I have come up with a new problem. <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
<o:p></o:p>
The code below counts the frequency of ‘1’ which is working perfectly.<o:p></o:p>
Code:
=MAX(FREQUENCY(IF($B3:$O3=1,COLUMN($B3:$O3)),IF($B3:$O3<>1,COLUMN($B3:$O3))))
<o:p></o:p>

<o:p></o:p>
But when I change the value ‘1’ to ‘X’ for counting the frequency of ‘X’ and try to run the code it does not work. It also highlights in red.<o:p></o:p>
Code:
=MAX(FREQUENCY(IF($B3:$O3="X",COLUMN($B3:$O3)),IF($B3:$O3<>"X",COLUMN($B3:$O3))))
<o:p></o:p>

<o:p></o:p>
Please, will you look in this situation how the code should be written in a correct way. Or is there another code to count the frequency of X?<o:p></o:p>
<o:p></o:p>
Kishan.
 
Upvote 0
Shg, you are great!<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
<o:p></o:p>
Thank you once again for your quick solution.<o:p></o:p>
<o:p></o:p>
Regards,<o:p></o:p>
Kishan.<o:p></o:p>
 
Upvote 0

Forum statistics

Threads
1,224,616
Messages
6,179,911
Members
452,949
Latest member
beartooth91

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