Hi.
I have been attempting to perform a vlookup between 2 sheets and have just encountered the following error message:
Run-Time Error '-2147221080 (800401a8)':
Automation Error
What I am attempting to do is reference an eternal workbook where the information is trimmed and copied and pasted as values to match the destination cells and manually this works. When I try to hard code this using VBA, the error message pops up at the point at which the vlookup code is executed (so the trimming of the cells and copying across works fine).
A cut version of my code is below:
Option Explicit
Sub PORTMON()
Dim DT As Date
'INPUT BOX TAKES DATE FROM USER
DT = Application.InputBox("Enter a Date as MMM YYYY")
DT = Format(DT, "MMM YYYY")
Dim PATH As String
Dim WKBOOKATT2 As Workbook
Dim FILENAME2 As String
PATH = "C:\Checks\"
FILENAME6 = "ACC_" & Left(Format(DT, "MMM YYYY"), 3) & "_" & Right(Format(DT, "MMM YYYY"), 4)
Set WKBOOKATT2 = Workbooks.Open(PATH & "ACC_" & Left(Format(DT, "MMM YYYY"), 3) & "_" & Right(Format(DT, "MMM YYYY"), 4) & ".XLS")
Dim APATT1 As Variant
Workbooks.Open (SASPATH & "ACC_" & Left(Format(DT, "MMM YYYY"), 3) & "_" & Right(Format(DT, "MMM YYYY"), 4) & ".XLS")
Range("F2").Select
ActiveCell.FormulaR1C1 = "=TRIM(RC[-5])"
Range("F2").Select
Selection.AutoFill Destination:=Range("F2:F14")
Range("F2:F14").Select
Range("B2:B14").Select
Selection.Copy
Range("G2").Select
ActiveSheet.Paste
Range("F2:F14").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows( _
"Acc Main.xls"). _
Activate
Sheets("Stability").Select
Range("D20").Select
'error occurs here
APATT1 = Application.VLOOKUP(Range("A20").VALUE, WKBOOKATT2.Sheets(FILENAME2).Range("F2:G15"), 2, False)
Range("D20").VALUE = APATT1
Has anyone come across anything like this before?
Please let me know if you require any further details from me.
Thanks
I have been attempting to perform a vlookup between 2 sheets and have just encountered the following error message:
Run-Time Error '-2147221080 (800401a8)':
Automation Error
What I am attempting to do is reference an eternal workbook where the information is trimmed and copied and pasted as values to match the destination cells and manually this works. When I try to hard code this using VBA, the error message pops up at the point at which the vlookup code is executed (so the trimming of the cells and copying across works fine).
A cut version of my code is below:
Option Explicit
Sub PORTMON()
Dim DT As Date
'INPUT BOX TAKES DATE FROM USER
DT = Application.InputBox("Enter a Date as MMM YYYY")
DT = Format(DT, "MMM YYYY")
Dim PATH As String
Dim WKBOOKATT2 As Workbook
Dim FILENAME2 As String
PATH = "C:\Checks\"
FILENAME6 = "ACC_" & Left(Format(DT, "MMM YYYY"), 3) & "_" & Right(Format(DT, "MMM YYYY"), 4)
Set WKBOOKATT2 = Workbooks.Open(PATH & "ACC_" & Left(Format(DT, "MMM YYYY"), 3) & "_" & Right(Format(DT, "MMM YYYY"), 4) & ".XLS")
Dim APATT1 As Variant
Workbooks.Open (SASPATH & "ACC_" & Left(Format(DT, "MMM YYYY"), 3) & "_" & Right(Format(DT, "MMM YYYY"), 4) & ".XLS")
Range("F2").Select
ActiveCell.FormulaR1C1 = "=TRIM(RC[-5])"
Range("F2").Select
Selection.AutoFill Destination:=Range("F2:F14")
Range("F2:F14").Select
Range("B2:B14").Select
Selection.Copy
Range("G2").Select
ActiveSheet.Paste
Range("F2:F14").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows( _
"Acc Main.xls"). _
Activate
Sheets("Stability").Select
Range("D20").Select
'error occurs here
APATT1 = Application.VLOOKUP(Range("A20").VALUE, WKBOOKATT2.Sheets(FILENAME2).Range("F2:G15"), 2, False)
Range("D20").VALUE = APATT1
Has anyone come across anything like this before?
Please let me know if you require any further details from me.
Thanks