Fix expected end of statement in VBA

nando88

Board Regular
Joined
Jun 22, 2013
Messages
124
I am trying to use a filter function with data from a seconf workbook. I have the following formula, but I am getting expected end of statement in the first Range and I don't know why:
VBA Code:
sub tenplate()
Dim gen As String, template As String, lr As Long, data As String, name As String, i As Long, wb1 As Workbook, wb2 As Workbook, ws1 As Worksheet, ws2 As Worksheet
gen = ThisWorkbook.name
data = archivo
Set wb1 = Workbooks(gen)
Set wb2 = Workbooks(data)
Set ws1 = Sheets("Trabajo")
Set ws2 = Sheets("Rpt")
For i = 1 To Worksheets.Count
Workbooks(gen).Activate
Worksheets(i).Activate
name = ActiveSheet.name
wb1.ws1.Range("A6").Formula2 = "=filter(wb2.ws2.Range("A6:P65536"),wb2.ws2.Range("C6:C65536")=wb1.ws1.Range("D1"),"""")"
end sub
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
I think because you are mixing objects into your excel formula.

Perhaps this (not tested)
VBA Code:
wb1.ws1.Range("A6").Formula2 = "=filter(" & _
    wb2.ws2.Range("A6:P65536").Address & "," & _
    wb2.ws2.Range("C6:C65536").Address & "=" & wb1.ws1.Range("D1").Address & "," _
    & Chr(34) & Chr(34) & ")"
 
Upvote 0
1) Since you are working with 2 workbooks, I would expect your 2 "Set ws = " lines to specify which workbook the sheet is in.
Rich (BB code):
Set ws1 = Sheets("Trabajo")      ' Which workbook  eg ws1 = wb1.Sheets("Trabajo")
Set ws2 = Sheets("Rpt")            ' Which workbook

2) You can't us the syntax "wb2.ws2" the ws2 is already in a workbook so the wb2 is doubling up, so modifying @rlv01 correction gets you to this:
VBA Code:
ws1.Range("A6").Formula2 = "=filter(" & _
    ws2.Range("A6:P65536").Address & "," & _
    ws2.Range("C6:C65536").Address & "=" & ws1.Range("D1").Address & "," _
    & Chr(34) & Chr(34) & ")"

3) I don't know if you only gave us an extract but
- archivo is not a valid workbook name
- there is no "Next i" at the end
 
Upvote 0
You'll also need to include the workbook and sheet names in the formula. Adding the External:=True parameter to Address should suffice.
 
Upvote 0

Forum statistics

Threads
1,217,448
Messages
6,136,692
Members
450,024
Latest member
Beagle263

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