1004 application error, PLEASE HELP!!

johnandginalyn

New Member
Joined
Jun 14, 2010
Messages
6
Hi,
I have a macro that works in the spreadsheet it was written in but when I try to run it in an identical spreadsheet outside of where the macro is stored then I get the 1004 error.
I understand that this is a reference issue with the sheets but I can get rid of an error in the formula line.

Code:
Sub LookingUp()
Dim wks As Worksheet
Dim col As Integer
Dim colA As Integer
Dim colB As Integer
Dim rowA As Integer
Dim row As Integer
Dim ref As Range
Dim ts As String
rowA = 1
ActiveWorkbook.Sheets(1).Select
Cells.Find(What:="Asset Id", After:=ActiveCell, LookIn:=xlFormulas, _
      LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
      MatchCase:=False, SearchFormat:=False).Activate
colB = ActiveCell.Column
colA = colB + 1
For Each wks In ActiveWorkbook.Worksheets
    
    If Not wks.Name = "List_of_Assets" Then
        wks.Select
        Cells.Find(What:="Asset Id", After:=ActiveCell, LookIn:=xlFormulas, _
            LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
            MatchCase:=False, SearchFormat:=False).Activate
        Selection.End(xlDown).Select
        row = ActiveCell.row
        col = ActiveCell.Column
        
        ActiveWorkbook.Sheets(1).Select
        Cells(1, colA).Select
        ActiveCell.EntireColumn.Select
        Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
        
        Cells(1, colB).Select
        Selection.End(xlDown).Select
        rowA = ActiveCell.row
        
        Cells(2, colA).Select
        [COLOR=red]ActiveCell.Formula = "=VLOOKUP(" & Cells(2, colB).Address(False, False) & "," & wks.Name & "!" & Cells(2, col).Address & ":" & Cells(row, col).Address & ",1,0)"[/COLOR]
        Selection.AutoFill Destination:=Range(ActiveCell, Cells(rowA, ActiveCell.Column))
        
        colA = colA + 1
End If
Next wks
End Sub

I'm getting the error on the line in red.
I really hope someone can shine a light on this.

Please help,
John
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
John

If a worksheet name has spaces (or various other characters) you need to enclose it in single quotes for a formula.
Rich (BB code):
ActiveCell.Formula = "=VLOOKUP(" & Cells(2, colB).Address(False, False) & "," & "'"&  wks.Name & "'!" & Cells(2, col).Address & ":" & Cells(row, col).Address & ",1,0)"
That should fix the formula but I'm not 100% sure that's the only problem in the code.

You seem to be missing quite a few worksheet references and all that selecting/activating makes it quite hard to follow.


PS you don't need to specify a sheet name if all the references in the lookup are on the same sheet as the one the formula is going in.
 
Upvote 0
John

If a worksheet name has spaces (or various other characters) you need to enclose it in single quotes for a formula.

I have a similar problem with inserting a formula via vba.
I have tried to put in single quotes but to no avail. Am I totally off here?

Rich (BB code):
    Range("A2").Activate
    ActiveCell.Formula = "=IF(LEFT(C2,8)="Keyword",LEFT(C5,5),A1)"
 
Upvote 0
You aren't using a worksheet name.

The problem is the "s around Keyword.

Just double them up.
Rich (BB code):
Range("A2").Formula   = "=IF(LEFT(C2,8)=""Keyword"",LEFT(C5,5),A1)"
 
Upvote 0

Forum statistics

Threads
1,224,602
Messages
6,179,844
Members
452,948
Latest member
UsmanAli786

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