Inserting a Formula to a Range using VBA

AndyJM87

New Member
Joined
Aug 31, 2018
Messages
28
Hello,

I am struggling to insert a VLOOKUP formula using VBA which references another sheet in a workbook.

I have a User Form which loads the file path and file name then stores them as a global variable.

strL & strF are global variables defined as a string.

Below is the Form code to insert the file into a textbox:
VBA Code:
Private Sub cmdStdBrowse_Click()
On Error Resume Next

Dim f           As Object
Dim strFile     As String
Dim strFolder   As String
Dim varItem     As Variant

    Set f = Application.FileDialog(3)
        f.AllowMultiSelect = False
            If f.Show Then
                For Each varItem In f.SelectedItems
                    strFile = Dir(varItem)
                    strFolder = Left(varItem, Len(varItem) - Len(strFile))
                    txtStdProd.Text = strFolder & strFile
                    
                    strL = strFolder
                    strF = strFile
                Next
        End If
    Set f = Nothing

End Sub

Below what I am trying to insert into a range:
VBA Code:
Private Sub cmdInsert_Click()
On Error Resume Next

Set wb = ActiveWorkbook

    'Call wbStart
    
    'MsgBox (strL & strF)
    
    With wb.Sheets("REPORT")
        .Range("F3").Formula = "=IFERROR(VLOOKUP($A3,' & strL & [  & strF & ]Component List for Equipment'!$D$15:$P$150,13,FALSE),"")"
    End With
    
    Unload Me
    
End Sub

Can someone please help. Thank you :)
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
I have found that the easiest way to debug writing formula with vBA is to remove the equals sign from the vba e.g.:
VBA Code:
.Range("F3").Formula = "IFERROR(VLOOKUP($A3,' & strL & [  & strF & ]Component List for Equipment'!$D$15:$P$150,13,FALSE),"")"
this then just writes a string to the cell. Then select the cell and put the = back in , then excel will show you where the error is.
Teach a man to fish , etc..
 
Upvote 0
@AndyJM87
I like @offthelip's method to check the vba generated formula.

You have your 2 variables embedded inside the quotation marks, this means they are read as text and not replaced by their values.
You need to break the string up and put it back together with the translation of the variables.
You also need to double up on the quote marks for the empty string return value
eg
VBA Code:
.Range("F3").Formula = "=IFERROR(VLOOKUP($A3,'" & strL & "[" & strF & "]Component List for Equipment'!$D$15:$P$150,13,FALSE),"""")"

I assume you have the 2 variables Dimmed at a Public / Global level and that you have tested it with the now commented out MsgBox (strL & strF)
 
Upvote 0

Forum statistics

Threads
1,215,064
Messages
6,122,936
Members
449,094
Latest member
teemeren

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