Formula function to VBA Function

MarkCBB

Active Member
Joined
Apr 12, 2010
Messages
497
Hi There VBA People,

I am currently using some VBA to insert this formula down 50,000 rows of data, then I copy the formulas and paste values. However I have been told that if i write the formula using proper VBA syntax it will calculate faster? and I wouldn't need to copy the results and paste them each time. Here is a sample


Code:
Sub Test()
    With Selection
        .FormulaR1C1 = "=IFERROR(VLOOKUP(RC[-4],R1C3:R5C4,2,FALSE),"""")"
        .Copy
        .PasteSpecial Paste:=xlPasteValues
    End With
        Application.CutCopyMode = False
End Sub

How would this formula look written correctly in VBA?
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
I don't think there is anything wrong with your formula, but you don't need to copy/paste. Try this

<font face=Courier New><br><SPAN style="color:#00007F">Sub</SPAN> Test()<br>    <SPAN style="color:#00007F">With</SPAN> Selection<br>        .FormulaR1C1 = "=IFERROR(VLOOKUP(RC[-4],R1C3:R5C4,2,FALSE),"""")"<br>        .Value = .Value<br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br></FONT>

Note that you generally do not have to 'select' things in vba to work with them and selecting can slow your code. Is there any particular reason you are working with 'Selection'? Is there some way to identify which cells you want to populate with this code?
 
Upvote 0
Hi Peter,

Isnt there a way for the formula to calculate in VBA and just trasffer the Values? I know you code above does the same thing, I just heard the I can write the formula in VBA.

I have been told before that I should use Select or selection in VBA, But I am so used to it as I started learning VBA by watching what Macros would record.

The only way that I think I can identiy where the formula should go is by, is this what you mean by identifying the cells? Is the a better way to write the below code?

Code:
Sub Test2()
    Dim End1 As Range
    Set End1 = Range("H1048576").End(xlUp).Offset(1, 0)
   With End1
    .FormulaR1C1 = "=""MyFormula"""
   End With
End Sub

I tried using the .Value = value but whe i step though the code, the .value = value removes the value from the cell, example below, what I am doing wrong?

Code:
Sub Test2()
    Dim End1 As Range
    Set End1 = Range("H1048576").End(xlUp).Offset(1, 0)
    With End1
    .FormulaR1C1 = "=""MyFormula"""
    .Value = Value
    End With
End Sub

I would forward to your responce, thank you for your assistance so far.

Kindest Regards,
Mark Blackburn
 
Upvote 0
Mark

You could perhaps use code to do the function but then you would probably need to loop through each cell to get the values for the function, calculate then place the result in the destination cell.

Somehow I don't know if that would be quicker.

Oh, and if you wanted to replace the entire formula with VBA so there are no worksheet functions involved you would need to create a VBA equivalent for
VLOOKUP.
 
Upvote 0
Hi Norie,

Just out of curisity what would the VBA code look like?

Peter, just another question on the Select/Selection topic, what is the correct way to writting this code?

Code:
 Range(Selection, Selection.End(xlDown)).Select
 
Upvote 0
I tried using the .Value = value
You are missing a '.' - the code was
Rich (BB code):
.Value = .Value
I'm not quite sure what you are saying about the location of the formula. However, if the formulas were going in column L, based on the number of rows in column H, then you could use something like this, without selecting anything.
Rich (BB code):
With Range("L2:L" & Range("H" & Rows.Count).End(xlUp).Row)
    .FormulaR1C1 = "=IFERROR(VLOOKUP(RC[-4],R1C3:R5C4,2,FALSE),"""")"
    .Value = .Value
End With
 
Upvote 0
Mark

What would what code look like?

You really should try something like Peter has posted.
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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