I am trying to import data from another workbook which requires an indirect function so the workbook must be open. The data is transferred during the IF statement, so I want the other workbook to open before the if statement and the workbook to close after the IF statement, both inside of the For loop. Also, when opening the workbook I want it to automatically say yes to the 2 defaults of Update and Continue (however, I have Application.DisplayAlerts = False and that isn't working). And finally, I don't want to save changes when I close the workbook (I always get an error when trying to active/close the workbook Run-time error 424). Thanks for all the help guys!
Sub Importing_Data()
'Defining Variables
Dim i As Long
Dim ii As Long
Dim wb As Workbook
Dim sht1 As Worksheet
Set wb = ThisWorkbook
Set sht1 = wb.Sheets("Comparison")
'Find the Last Column with Data
Dim LastColumn As Integer
With ActiveSheet
LastColumn = .Cells(1, .Columns.Count).End(xlToLeft).Column
End With
EndingColumn = (LastColumn / 2) - 1
AnotherColumn = (LastColumn / 2) - 2
DateColumn = LastColumn + 4
OtherCell = Cells(14, (i * 2) + 2)
DragDownCell = Cells(241, (i * 2) + 2)
'Importing New Data for Current Columns
For i = 1 To EndingColumn
FirstCell = Cells(1, (i * 2) + 1)
SecondCell = (LastColumn - 2)
ThirdCell = FirstCell + 2
FourthCell = ThirdCell + 1
NewWorkbook = Cells(252, 3)
Application.DisplayAlerts = False
Workbooks.Open (NewWorkbook)
wb.Activate
If (FirstCell < SecondCell) Then
Cells(14, FourthCell).Formula = Cells(251, ThirdCell).Formula
Cells(14, (i * 2) + 2).Select
Selection.AutoFill Destination:=Range(Cells(14, (i * 2) + 2), Cells(241, (i * 2) + 2)), Type:=xlFillDefault
Range(Cells(14, (i * 2) + 2), Cells(241, (i * 2) + 2)).Select
End If
NewWorkbook.Activate
ActiveWorkbook.Close savechanges:=False
Next i
End Sub
Bookmarks