Deleting all input in the Name Manager after saving external workbook - vba

I am currently making a macro which creates a catalogue and afterwards saves it in different languages as external files. Whenever I save the files with the VBA script below the file is still large (2MB+), but whenever I open the file and delete all references in the Name Manager (these are copied as well it seems), the file is just 30/40 kb.
Is there a VBA formula that automatically deletes the formulas in the Name Manager (only in the external copy, not in the original file!)?
Sub NIP_Version()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Workbooks("Opbouw catalogus.xlsm").Activate
filenaam = ActiveWorkbook.Path & "\" & "Excel prijslijst" & "\" & Sheets("Catalogus").Range("A1").Text & " " & Sheets("Catalogus").Range("G2").Text
'Quotation blad copy
Sheets("Catalogus").Select
'paste as values
Cells.Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
Dim LastRowNIP As Long
With ActiveSheet
LastRowNIP = .Cells(.Rows.Count, "E").End(xlUp).Row
End With
Set example = Range("A5:G" & LastRowNIP)
example.Value = example.FormulaR1C1
Columns("F").EntireColumn.AutoFit
'Save
Application.ScreenUpdating = True
Sheets("Catalogus").Range("A1").Select
ActiveSheet.Copy
ActiveWorkbook.Sheets("Catalogus").SaveAs Filename:=filenaam, FileFormat:=51
Application.DisplayAlerts = True
ActiveWorkbook.Close SaveChanges:=False

Sub remove_names()
Dim xName As Name
For Each xName In Application.ThisWorkbook.Names
xName.Delete
Next xName
End Sub

Sub NIP_Version()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Workbooks("Opbouw catalogus.xlsm").Activate
filenaam = ActiveWorkbook.Path & "\" & "Excel prijslijst" & "\" & Sheets("Catalogus").Range("A1").Text & " " & Sheets("Catalogus").Range("G2").Text
'Quotation blad Copy
Sheets("Catalogus").Select
'Paste as value
Cells.Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
Dim LastRowNIP As Long
With ActiveSheet
LastRowNIP = .Cells(.Rows.Count, "E").End(xlUp).Row
End With
Set example = Range("A5:G" & LastRowNIP)
example.Value = example.FormulaR1C1
Columns("F").EntireColumn.AutoFit
'Save
Application.ScreenUpdating = True
Sheets("Catalogus").Range("A1").Select
ActiveSheet.Copy
ActiveWorkbook.Sheets("Catalogus").SaveAs Filename:=filenaam, FileFormat:=51
Sub remove_names()
Dim xName As Name
For Each xName In Application.ThisWorkbook.Names
xName.Delete
Next xName
End Sub
Application.DisplayAlerts = True
ActiveWorkbook.Close SaveChanges:=False
Workbooks("Prijslijst maken.xlsm").Activate

Related

VBA Autofill speed issues

I am working on optimising my vba code but until now I haven't succeeded yet. Would there be any possibility that you can look at my code and tell me how I can make it faster?
I have two excel files: 1 of them is the excel template from which the report will be computed and the other 1 is the generator. In the attached code, and I know that it is pretty long, you can find the code that has been written. I am not sure how to optimise this code any further so any help from your side would be helpful.
Thank you,
Jeroen
Sub Prepare_Files()
Dim TabName As String
MacroSheet = "Sheet1"
File_Loc = "File Locations"
strReportDate = Worksheets(MacroSheet).Range("I23").Value
strPrevReportDate = Worksheets(MacroSheet).Range("I26").Value
strInvoiceDate = Worksheets(MacroSheet).Range("I29").Value
TemplateAUHUHeadyOpen = False
EEEEEEEEEJJJ = "A. Oipoip Data - YYYYYY"
EEEEEEEEEUHUH = "B. Oipoip Data - XXXXXXXXXX"
QQQQQQ_Inv = "C. QQQQQQ Data - Inventory"
QQQQQQ_Act = "D. QQQQQQ Data - Active"
Prod_Data = "E. PROD Data"
Report_Detail = "F. Report Detail"
Sales_Summary = "G. Sales Summary"
US_Trial_Plans = "P. US Trial Plans"
US_Wholesale_Plans = "Q. US Wholesale Plans"
CAN_Trial_Plans = "R. CAN Trial Plans"
CAN_Wholesale_Plans = "S. CAN Wholesale Plans"
JJJ_NA_Data_Locn = Worksheets(File_Loc).Range("B2").Value
JJJ_UK_Data_Locn = Worksheets(File_Loc).Range("B3").Value
JJJ_EU_Data_Locn = Worksheets(File_Loc).Range("B4").Value
UHUH_NA_Data_Locn = Worksheets(File_Loc).Range("B5").Value
UHUH_UK_Data_Locn = Worksheets(File_Loc).Range("B6").Value
UHUH_EU_Data_Locn = Worksheets(File_Loc).Range("B7").Value
QQQQQQ_Act_Data_Locn = Worksheets(File_Loc).Range("B8").Value
QQQQQQ_Inv_Data_Locn = Worksheets(File_Loc).Range("B9").Value
Prod_Build_Data_Locn = Worksheets(File_Loc).Range("B10").Value
TemplateFiles_Locn = Worksheets(File_Loc).Range("B11").Value
New_Sales_Report_Locn = Worksheets(File_Loc).Range("B12").Value
ZZZ_Invoice_Data_Locn = Worksheets(File_Loc).Range("B13").Value
EEEEEEEEEFile_Locn = Worksheets(File_Loc).Range("B14").Value
ModelYear1 = Worksheets("Settings").Range("B2").Value
ModelYear2 = Worksheets("Settings").Range("B3").Value
ModelYear3 = Worksheets("Settings").Range("B4").Value
ModelYear4 = Worksheets("Settings").Range("B5").Value
ModelYear5 = Worksheets("Settings").Range("B6").Value
ReportNum = Worksheets(MacroSheet).Range("I18").Value
If ReportNum = 1 Then
All_Reports = False
All_Reports_1st_No = 1
All_Reports_last_No = 1
TabName = EEEEEEEEEJJJ
JJJ_Data_Locn = JJJ_NA_Data_Locn
Else
Exit Sub
End If
For All_Reports_No = All_Reports_1st_No To All_Reports_last_No
If All_Reports_No = 1 Then
MarketName = "North America"
OptOuts_ColNo = OptOuts_ColNo1
VistaCountryname = VistaCountryname1
SettingsColumnNo = SettingsColumnNo1
SheetName_Data_In_Daily_Report = SheetName_Data_In_Daily_Report1
JJJ_Vista_File_Locn = JJJ_NA_Data_Locn
UHUH_Vista_File_Locn = UHUH_NA_Data_Locn
End If
Next All_Reports_No
JJJ_VistaFile = Dir$(JJJ_Vista_File_Locn & "\YYYYYY TSTSTSTS - " & MarketName & " - " & Format(strReportDate, "dd-mm-yy") & ".xlsx")
If Len(JJJ_VistaFile) = 0 Then
MsgBox ("The Data file 'YYYYYY TSTSTSTS - " & MarketName & " - " & Format(strReportDate, "dd-mm-yy") & ".xlsx""' is missing")
Exit Sub
End If
UHUH_VistaFile = Dir$(UHUH_Vista_File_Locn & "\YHYHYHYHY TSTSTSTS - " & MarketName & " - " & Format(strReportDate, "dd-mm-yy") & ".xlsx")
If Len(UHUH_VistaFile) = 0 Then
MsgBox ("The Data file 'YHYHYHYHY TSTSTSTS - " & MarketName & " - " & Format(strReportDate, "dd-mm-yy") & ".xlsx""' is missing")
Exit Sub
End If
OipoipFile = Dir$(EEEEEEEEEFile_Locn & "\ZZZ_reports_" & Format(strReportDate, "yyyy-mm-dd") & ".zip")
If Len(OipoipFile) = 0 Then
MsgBox ("The Data file 'ZZZ_reports_" & Format(strReportDate, "yyyy-mm-dd") & ".zip""' is missing")
Exit Sub
End If
QQQQQQInvFile = Dir$(QQQQQQ_Inv_Data_Locn & "\QQQQQQ Inventory_" & Format(strReportDate, "yyyymmdd") & "*.xls")
If Len(QQQQQQInvFile) = 0 Then
MsgBox ("The QQQQQQ Inventory Data file 'QQQQQQ Inventory_" & Format(strReportDate, "yyyymmdd") & "*.xls""' is missing")
Exit Sub
End If
QQQQQQActFile = Dir$(QQQQQQ_Act_Data_Locn & "\QQQQQQ Activated_" & Format(strReportDate, "yyyymmdd") & "*.xls")
If Len(QQQQQQActFile) = 0 Then
MsgBox ("The QQQQQQ Activated Data file 'QQQQQQ Activated_" & Format(strReportDate, "yyyymmdd") & "*.xls""' is missing")
Exit Sub
End If
ProdBuildFile = Dir$(Prod_Build_Data_Locn & "\Production Build Data IOIOIOIOIOI_PAPAPAPAPAPAPA.xlsx")
If Len(ProdBuildFile) = 0 Then
MsgBox ("The Data file 'Production Build Data IOIOIOIOIOI_PAPAPAPAPAPAPA.xlsx' is missing")
Exit Sub
End If
TemplateFile = Dir$(TemplateFiles_Locn & "\Sales Report V6 Template.xlsx")
If Len(TemplateFile) = 0 Then
MsgBox ("The Template file 'Sales Report V6 Template.xlsx' is missing")
Exit Sub
End If
PrevReportFile = Dir$(New_Sales_Report_Locn & "\Sales Report V6 - " & Format(strPrevReportDate, "dd.mm.yyyy") & ".xlsx")
If Len(PrevReportFile) = 0 Then
MsgBox ("The Previous Report ( 'Sales Report V6 - " & Format(strPrevReportDate, "dd.mm.yyyy") & ".xlsx' ) is not found.")
Exit Sub
End If
ZZZInvoiceFile = Dir$(ZZZ_Invoice_Data_Locn & "\ZZZ Invoice - " & Format(strInvoiceDate, "mm.yyyy") & ".xlsx")
If Len(ZZZInvoiceFile) = 0 Then
MsgBox ("The Previous Report ('ZZZ Invoice - " & Format(strInvoiceDate, "mm.yyyy") & ".xlsx' ) is not found.")
Exit Sub
End If
FolderPath = New_Sales_Report_Locn & "\"
'Copy the YYYYYY Data from the Vista Data file to the Template's EEEEEEEEEJJJ Sheet
If ReportNum = 1 Then
'Now that all the required files are present, Copy the first YYYYYY Vista Data file to the Template
'But first switch off Auto Caluculate in Excel
'Application.EnableEvents = False
Application.Calculation = xlCalculationManual
If All_Reports_No = 1 Then
TabName = TabName1
MarketName = MarketName1
End If
'Set the Template to y and clear any exisitng data from the Built Orders tab
If TemplateAUHUHeadyOpen = False Then
Set wbTemplate = Workbooks.Open(TemplateFiles_Locn & "\" & TemplateFile)
ElseIf TemplateAUHUHeadyOpen = True Then
Workbooks.Item(TemplateFile).Activate
End If
'Open the YYYYYY Vista Data File & copy the data
Set wbJJJVista = Workbooks.Open(JJJ_Vista_File_Locn & "\" & JJJ_VistaFile)
Workbooks.Open(JJJ_Vista_File_Locn & "\" & JJJ_VistaFile).Activate
Worksheets("All Built Orders").Select
Range("A1").Select
Selection.End(xlDown).Select
NoOfRows_Data = ActiveCell.Row
'Apply Filters
ActiveSheet.Range("$A$1:$Y$" & NoOfRows_Data).AutoFilter Field:=1, Criteria1:=Array(""), Operator:=xlFilterValues
ActiveSheet.ShowAllData
ActiveSheet.Range("$A$1:$Y$" & NoOfRows_Data).AutoFilter Field:=15, Criteria1:=Array( _
ModelYear1, _
ModelYear2, _
ModelYear3, _
ModelYear4, _
ModelYear5), Operator:=xlFilterValues
Filtered_Total = Application.WorksheetFunction.Subtotal(103, [A2:A1040000])
Range("A2:Y2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
'Go to the Template File & paste the data into the first sheet
Workbooks.Item(TemplateFile).Activate
Sheets(EEEEEEEEEJJJ).Range("B2").PasteSpecial
Application.CutCopyMode = False
Selection.End(xlDown).Select
NoOfRows_Data = ActiveCell.Row
Range("A1").Select
'Close the Vista Data File, without saIOIOIOIOIOIg
Workbooks.Item(JJJ_VistaFile).Activate
ActiveWorkbook.Close SaveChanges:=False
'********
'Check if the TRTRTRTR Data file exists, in zipped format or the unzipped format
RTRTRT = Dir$(EEEEEEEEEFile_Locn & "\ZZZ_report_" & Format(strReportDate, "yyyy-mm-dd") & "_3.csv")
If Len(RTRTRT) = 0 Then
ZippedRTRTRT = Dir$(EEEEEEEEEFile_Locn & "\ZZZ_reports_" & Format(strReportDate, "yyyy-mm-dd") & ".zip")
If Len(ZippedRTRTRT) = 0 Then
MsgBox ("The Zipped TRTRTRTR Data File ( 'ZZZ_reports_" & Format(strReportDate, "yyyy-mm-dd") & ".zip' ) is not found")
Exit Sub
Else
FolderPath = EEEEEEEEEFile_Locn
zFile = "ZZZ_reports_" & Format(strReportDate, "yyyy-mm-dd") & ".zip"
UnzipFile FolderPath & "\" & zFile, FolderPath
RTRTRT = Dir$(EEEEEEEEEFile_Locn & "\ZZZ_report_" & Format(strReportDate, "yyyy-mm-dd") & "_3.csv")
If Len(RTRTRT) = 0 Then
MsgBox ("The TRTRTRTR Data File ( ZZZ_report_" & Format(strReportDate, "yyyy-mm-dd") & "_3.csv ) is not found in the zipped file")
Exit Sub
Else
'Copy the WCData from the TRTRTRTR Data file to the Template's WData tab
'Only need to do this once for all the reports
Set wbWCData = Workbooks.Open(EEEEEEEEEFile_Locn & "\" & RTRTRT, Format:=4, Delimiter:=",")
Workbooks.Open(EEEEEEEEEFile_Locn & "\" & RTRTRT, Format:=4, Delimiter:=",").Activate
Range("A2:C2").Select
Range(Selection, Selection.End(xlDown)).Select
'Cells.Select
Selection.Copy
Range("A1").Select
With wbTemplate
If TemplateAUHUHeadyOpen = True Then
wbTemplate.Sheets("T. Oipoip PAPAPAPAPAPAPA").Range("A2").PasteSpecial
Else
Workbooks.Item(TemplateFile).Activate
wbTemplate.Sheets("T. Oipoip PAPAPAPAPAPAPA").Range("A2").PasteSpecial
Worksheets("T. Oipoip PAPAPAPAPAPAPA").Select
Range("C:C").Select
Selection.NumberFormat = "0"
End If
Range("A1").Select
Application.CutCopyMode = False
TemplateAUHUHeadyOpen = True
RTRTRT_Populated = True
End With
With wbWCData
Workbooks.Item(RTRTRT).Close
End With
End If
End If
Else
RTRTRT = Dir$(EEEEEEEEEFile_Locn & "\ZZZ_report_" & Format(strReportDate, "yyyy-mm-dd") & "_3.csv")
'Copy the WCData from the TRTRTRTR Data file to the Template's WData tab
'Only need to do this once for all the reports
'Set wbWCData = Workbooks.Open(EEEEEEEEEFile_Locn & "\" & RTRTRT, Format:=4, Delimiter:=",")
'Workbooks.Open(EEEEEEEEEFile_Locn & "\" & RTRTRT, Format:=4, Delimiter:=",").Activate
Sheets("T. Oipoip PAPAPAPAPAPAPA").Select
Range("A1").Select
ConnectionTxt = "TEXT;" & EEEEEEEEEFile_Locn & "\ZZZ_report_" & Format(strReportDate, "yyyy-mm-dd") & "_3.csv"
With ActiveSheet.QueryTables.Add(Connection:=ConnectionTxt, Destination:=Range("$A$1"))
' .CommandType = 0
.Name = RTRTRT
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 850
.TextFileStartRow = 2
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 2)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
Range("A1").Select
Range("A1").Select
Application.CutCopyMode = False
TemplateAUHUHeadyOpen = True
RTRTRT_Populated = True
End If
'********
'Open the YHYHYHYHY Vista Data File & copy the data
Set wbUHUHVista = Workbooks.Open(UHUH_Vista_File_Locn & "\" & UHUH_VistaFile)
'Workbooks.Open(UHUH_Vista_File_Locn & "\" & UHUH_VistaFile).Activate
Worksheets("All Built Orders").Select
Range("A1").Select
Selection.End(xlDown).Select
NoOfRows_Data = ActiveCell.Row
'Apply Filters
ActiveSheet.Range("$A$1:$Y$" & NoOfRows_Data).AutoFilter Field:=1, Criteria1:=Array(""), Operator:=xlFilterValues
ActiveSheet.ShowAllData
ActiveSheet.Range("$A$1:$Y$" & NoOfRows_Data).AutoFilter Field:=15, Criteria1:=Array( _
ModelYear1, _
ModelYear2, _
ModelYear3, _
ModelYear4, _
ModelYear5), Operator:=xlFilterValues
Filtered_Total = Application.WorksheetFunction.Subtotal(103, [A2:A1040000])
'Range("A2:Y2").Select
Range("A2:Y" & NoOfRows_Data).Select
Selection.Copy
'Go to the Template File & paste the data into the second sheet
Workbooks.Item(TemplateFile).Activate
Sheets(EEEEEEEEEUHUH).Range("B2").PasteSpecial
Application.CutCopyMode = False
Worksheets(EEEEEEEEEUHUH).Select
Range("A1").Select
'Selection.End(xlDown).Select
'NoOfRows_Data = ActiveCell.Row
'Close the Vista Data File, without saIOIOIOIOIOIg
'Workbooks.Open(UHUH_Vista_File_Locn & "\" & UHUH_VistaFile).Activate
Workbooks.Item(UHUH_VistaFile).Activate
ActiveWorkbook.Close SaveChanges:=False
'********
'Open the QQQQQQ Inventory Data File & copy the data
Set wbJasInv = Workbooks.Open(QQQQQQ_Inv_Data_Locn & "\" & QQQQQQInvFile)
Worksheets("Sheet0").Select
Range("A2").Select
Selection.End(xlDown).Select
NoOfRows_Data = ActiveCell.Row
Range("A2:B2").Select
Range("A2:B" & NoOfRows_Data).Select
Selection.Copy
'Go to the Template File & paste the data into the third sheet
Workbooks.Item(TemplateFile).Activate
Sheets(QQQQQQ_Inv).Range("B2").PasteSpecial
Application.CutCopyMode = False
Workbooks.Item(QQQQQQInvFile).Activate
Worksheets("Sheet0").Select
Range("M2:N" & NoOfRows_Data).Select
Selection.Copy
'Go to the Template File & paste the data into the third sheet
Workbooks.Item(TemplateFile).Activate
Sheets(QQQQQQ_Inv).Range("D2").PasteSpecial
Application.CutCopyMode = False
Workbooks.Item(QQQQQQInvFile).Activate
Worksheets("Sheet0").Select
Range("D2:E" & NoOfRows_Data).Select
Selection.Copy
'Go to the Template File & paste the data into the third sheet
Workbooks.Item(TemplateFile).Activate
Sheets(QQQQQQ_Inv).Range("F2").PasteSpecial
Application.CutCopyMode = False
Workbooks.Item(QQQQQQInvFile).Activate
Worksheets("Sheet0").Select
Range("H2:H" & NoOfRows_Data).Select
Selection.Copy
'Go to the Template File & paste the data into the third sheet
Workbooks.Item(TemplateFile).Activate
Sheets(QQQQQQ_Inv).Range("H2").PasteSpecial
Application.CutCopyMode = False
Workbooks.Item(QQQQQQInvFile).Activate
Worksheets("Sheet0").Select
Range("J2:K" & NoOfRows_Data).Select
Selection.Copy
'Go to the Template File & paste the data into the third sheet
Workbooks.Item(TemplateFile).Activate
Sheets(QQQQQQ_Inv).Range("I2").PasteSpecial
Application.CutCopyMode = False
Worksheets(QQQQQQ_Inv).Select
Range("A1").Select
'Close the Vista Data File, without saIOIOIOIOIOIg
Workbooks.Open(QQQQQQ_Inv_Data_Locn & "\" & QQQQQQInvFile).Activate
ActiveWorkbook.Close SaveChanges:=False
'Open the QQQQQQ Activated Data File & copy the data
Set wbJasAct = Workbooks.Open(QQQQQQ_Act_Data_Locn & "\" & QQQQQQActFile)
Worksheets("Sheet0").Select
Range("A2").Select
Selection.End(xlDown).Select
NoOfRows_Data = ActiveCell.Row
Range("A2:A" & NoOfRows_Data).Select
Selection.Copy
'Go to the Template File & paste the data into the third sheet
Workbooks.Item(TemplateFile).Activate
Sheets(QQQQQQ_Act).Range("B2").PasteSpecial
Application.CutCopyMode = False
Workbooks.Item(QQQQQQActFile).Activate
Worksheets("Sheet0").Select
Range("O2:O" & NoOfRows_Data).Select
Selection.Copy
'Go to the Template File & paste the data into the third sheet
Workbooks.Item(TemplateFile).Activate
Sheets(QQQQQQ_Act).Range("C2").PasteSpecial
Application.CutCopyMode = False
'Set wbJasAct = Workbooks.Open(QQQQQQ_Act_Data_Locn & "\" & QQQQQQActFile)
Workbooks.Item(QQQQQQActFile).Activate
Worksheets("Sheet0").Select
Range("B2:B" & NoOfRows_Data).Select
Selection.Copy
'Go to the Template File & paste the data into the third sheet
Workbooks.Item(TemplateFile).Activate
Sheets(QQQQQQ_Act).Range("D2").PasteSpecial
Application.CutCopyMode = False
Workbooks.Item(QQQQQQActFile).Activate
Worksheets("Sheet0").Select
Range("M2:N" & NoOfRows_Data).Select
Selection.Copy
'Go to the Template File & paste the data into the third sheet
Workbooks.Item(TemplateFile).Activate
Sheets(QQQQQQ_Act).Range("E2").PasteSpecial
Application.CutCopyMode = False
Workbooks.Item(QQQQQQActFile).Activate
Worksheets("Sheet0").Select
Range("D2:E" & NoOfRows_Data).Select
Selection.Copy
'Go to the Template File & paste the data into the third sheet
Workbooks.Item(TemplateFile).Activate
Sheets(QQQQQQ_Act).Range("G2").PasteSpecial
Application.CutCopyMode = False
Worksheets(QQQQQQ_Inv).Select
Workbooks.Item(QQQQQQActFile).Activate
Worksheets("Sheet0").Select
Range("H2:H" & NoOfRows_Data).Select
Selection.Copy
'Go to the Template File & paste the data into the third sheet
Workbooks.Item(TemplateFile).Activate
Sheets(QQQQQQ_Act).Range("I2").PasteSpecial
Application.CutCopyMode = False
Worksheets(QQQQQQ_Inv).Select
Workbooks.Item(QQQQQQActFile).Activate
Worksheets("Sheet0").Select
Range("J2:K" & NoOfRows_Data).Select
Selection.Copy
'Go to the Template File & paste the data into the third sheet
Workbooks.Item(TemplateFile).Activate
Sheets(QQQQQQ_Act).Range("J2").PasteSpecial
Application.CutCopyMode = False
Worksheets(QQQQQQ_Act).Select
Range("A1").Select
'Close the Vista Data File, without saIOIOIOIOIOIg
Workbooks.Item(QQQQQQActFile).Activate
ActiveWorkbook.Close SaveChanges:=False
'Open the Production Build Data File & copy the data
Set wbJasAct = Workbooks.Open(Prod_Build_Data_Locn & "\" & ProdBuildFile)
Worksheets("PROD_IOIOIOIOIOI_PAPAPAPAPAPAPA").Select
Range("A2:D2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
'Go to the Template File & paste the data into the third sheet
Workbooks.Item(TemplateFile).Activate
Sheets(Prod_Data).Range("C2").PasteSpecial
Application.CutCopyMode = False
Worksheets(Prod_Data).Select
Range("A1").Select
'Close the Production Build Data File, without saIOIOIOIOIOIg
Workbooks.Open(Prod_Build_Data_Locn & "\" & ProdBuildFile).Activate
ActiveWorkbook.Close SaveChanges:=False
'Open the ZZZ Invoice Data File & copy the data set 1
Set wbJasAct = Workbooks.Open(ZZZ_Invoice_Data_Locn & "\" & ZZZInvoiceFile)
'Workbooks.Open(ZZZ_Invoice_Data_Locn & "\" & ZZZInvoiceFile).Activate
Worksheets("US - Other Charges (Trial Fee)").Select
Range("A7:I7").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
'Go to the Template File & paste the data into the data trial summary sheet
Workbooks.Item(TemplateFile).Activate
Sheets(US_Trial_Plans).Range("A2").PasteSpecial
Application.CutCopyMode = False
Worksheets(US_Trial_Plans).Select
Range("A1").Select
'Close the Invoice File, without saIOIOIOIOIOIg
Workbooks.Open(ZZZ_Invoice_Data_Locn & "\" & ZZZInvoiceFile).Activate
ActiveWorkbook.Close SaveChanges:=False
'Open the ZZZ Invoice Data File & copy the data set 2
Set wbJasAct = Workbooks.Open(ZZZ_Invoice_Data_Locn & "\" & ZZZInvoiceFile)
'Workbooks.Open(ZZZ_Invoice_Data_Locn & "\" & ZZZInvoiceFile).Activate
Worksheets("US - January Rate Plan Detail ").Select
Range("A10:H10").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
'Go to the Template File & paste the data into the data wholesale summary sheet
Workbooks.Item(TemplateFile).Activate
Sheets(US_Wholesale_Plans).Range("A2").PasteSpecial
Application.CutCopyMode = False
Worksheets(US_Wholesale_Plans).Select
Range("A1").Select
'Close the Invoice File, without saIOIOIOIOIOIg
Workbooks.Open(ZZZ_Invoice_Data_Locn & "\" & ZZZInvoiceFile).Activate
ActiveWorkbook.Close SaveChanges:=False
'Open the ZZZ Invoice Data File & copy the data set 3
Set wbJasAct = Workbooks.Open(ZZZ_Invoice_Data_Locn & "\" & ZZZInvoiceFile)
Worksheets("CAN Other Charges (Trial Fee) ").Select
Range("A7:I7").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
The rest of the code will be in the comment of this query.
Extend down all the formulas in the template file (these formula's are mostly Index+Match formula's)
Copy the formula's as values to speed up opening the report afterwards
This is a duplication of effort. Depending on how many formulae you have, one thing that could speed this up A LOT would be to use VBA to calculate the values. At the moment, you're using VBA to copy and paste formulae, waiting for the formulae to calculate, copying the formulae, then pasting as values. Just doing the whole calculation in VBA and placing the end result into the spreadsheet should be way quicker. You can use Application.WorksheetFunction to put any function that works in a spreadsheet into your VBA.
I also see you're opening files then closing them without saving changes. Try opening them with ReadOnly:=True. It can make a big speed difference.
Added later:
Depends what you're looking up, but, if you do take my advice and do all the calculation within the VBA, you might well find that Find and Offset are more efficient than MATCH and INDEX. By sheer coincidence, I posted an example of using Find and Offset earlier today: https://stackoverflow.com/a/39410878/2475052
This it the rest of the code...
'Go to the Template File & paste the data into the data trial summary sheet
Workbooks.Item(TemplateFile).Activate
Sheets(CAN_Trial_Plans).Range("A2").PasteSpecial
Application.CutCopyMode = False
Worksheets(CAN_Trial_Plans).Select
Range("A1").Select
'Close the Invoice File, without saIOIOIOIOIOIg
Workbooks.Open(ZZZ_Invoice_Data_Locn & "\" & ZZZInvoiceFile).Activate
ActiveWorkbook.Close SaveChanges:=False
'Open the ZZZ Invoice Data File & copy the data set 4
Set wbJasAct = Workbooks.Open(ZZZ_Invoice_Data_Locn & "\" & ZZZInvoiceFile)
Worksheets("CAN January Rate Plan Detail").Select
Range("A8:N8").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
'Go to the Template File & paste the data into the data wholesale summary sheet
Workbooks.Item(TemplateFile).Activate
Sheets(CAN_Wholesale_Plans).Range("A2").PasteSpecial
Application.CutCopyMode = False
Worksheets(CAN_Wholesale_Plans).Select
Range("A1").Select
'Close the Invoice File, without saIOIOIOIOIOIg
Workbooks.Open(ZZZ_Invoice_Data_Locn & "\" & ZZZInvoiceFile).Activate
ActiveWorkbook.Close SaveChanges:=False
'Extend down all the formulae in the Template file
Workbooks.Item(TemplateFile).Activate
Worksheets(EEEEEEEEEJJJ).Select
Range("B2").Select
Selection.End(xlDown).Select
NoOfRows_Data = ActiveCell.Row
NoOfRows1 = "A2:A" & NoOfRows_Data
Range("A2").Select
Selection.AutoFill Destination:=Range(NoOfRows1)
NoOfRows1 = "AA2:AA" & NoOfRows_Data
Range("AA2:AA2").Select
Selection.AutoFill Destination:=Range(NoOfRows1)
Range("A1").Select
Worksheets(EEEEEEEEEUHUH).Select
Range("B2").Select
Selection.End(xlDown).Select
NoOfRows_Data = ActiveCell.Row
NoOfRows1 = "A2:A" & NoOfRows_Data
Range("A2").Select
Selection.AutoFill Destination:=Range(NoOfRows1)
Range("A1").Select
NoOfRows1 = "AA2:AA" & NoOfRows_Data
Range("AA2:AA2").Select
Selection.AutoFill Destination:=Range(NoOfRows1)
Range("A1").Select
Worksheets(QQQQQQ_Inv).Select
Range("B2").Select
Selection.End(xlDown).Select
NoOfRows_Data = ActiveCell.Row
NoOfRows1 = "A2:A" & NoOfRows_Data
Range("A2").Select
Selection.AutoFill Destination:=Range(NoOfRows1)
Range("L2").Value = Format(strReportDate, "dd-mmm-yyyy")
Range("A1").Select
Worksheets(QQQQQQ_Act).Select
Range("B2").Select
Selection.End(xlDown).Select
NoOfRows_Data = ActiveCell.Row
NoOfRows1 = "A2:A" & NoOfRows_Data
Range("A2").Select
Selection.AutoFill Destination:=Range(NoOfRows1)
Range("W2").Value = Format(strReportDate, "dd-mmm-yyyy")
Range("X2").Value = Format(Now(), "dd-mmm-yyyy")
Range("A1").Select
NoOfRows1 = "L2:P" & NoOfRows_Data
Range("L2:P2").Select
Selection.AutoFill Destination:=Range(NoOfRows1)
Range("A1").Select
NoOfRows1 = "Q2:Q" & NoOfRows_Data
Range("Q2").Select
Selection.AutoFill Destination:=Range(NoOfRows1)
Range("A1").Select
NoOfRows1 = "R2:V" & NoOfRows_Data
Range("R2:V2").Select
Selection.AutoFill Destination:=Range(NoOfRows1)
Range("A1").Select
Worksheets(Prod_Data).Select
Range("C2").Select
Selection.End(xlDown).Select
NoOfRows_Data = ActiveCell.Row
NoOfRows1 = "A2:B" & NoOfRows_Data
Range("A2:B2").Select
Selection.AutoFill Destination:=Range(NoOfRows1)
Range("N2").Value = Format(strReportDate, "dd-mmm-yyyy")
Range("A1").Select
NoOfRows1 = "G2:J" & NoOfRows_Data
Range("G2:J2").Select
Selection.AutoFill Destination:=Range(NoOfRows1)
Range("A1").Select
NoOfRows1 = "K2:K" & NoOfRows_Data
Range("K2:K2").Select
Selection.AutoFill Destination:=Range(NoOfRows1)
Range("A1").Select
NoOfRows1 = "L2:L" & NoOfRows_Data
Range("L2:L2").Select
Selection.AutoFill Destination:=Range(NoOfRows1)
Range("A1").Select
'Report Detail
Worksheets(Report_Detail).Select
Range("A3").Select
NoOfRows1 = "A3:AB" & NoOfRows_Data
Range("A3:AB3").Select
Selection.AutoFill Destination:=Range(NoOfRows1)
NoOfRows1 = "AC3:AC" & NoOfRows_Data
Range("AC3").Select
Selection.AutoFill Destination:=Range(NoOfRows1)
Range("A1").Select
NoOfRows1 = "AE3:AL" & NoOfRows_Data
Range("AE3:AL3").Select
Selection.AutoFill Destination:=Range(NoOfRows1)
Range("A1").Select
'Now switch on the Auto Caluculate in Excel
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
Worksheets(EEEEEEEEEJJJ).Select
Range("B2").Select
Sheets(Sales_Summary).Select
Range("K16").Select
ActiveSheet.PivotTables("PivotTable4").PivotCache.Refresh
Range("K4").Select
ActiveSheet.PivotTables("PivotTable3").PivotCache.Refresh
Range("A4").Select
ActiveSheet.PivotTables("PivotTable2").PivotCache.Refresh
Range("A16").Select
ActiveSheet.PivotTables("PivotTable1").PivotCache.Refresh
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
'Replace all the formulae with actual values to speed up opening the report afterwards
Workbooks.Item(TemplateFile).Activate
Worksheets(EEEEEEEEEJJJ).Select
Range("A2").Select
Selection.End(xlDown).Select
NoOfRows_Data = ActiveCell.Row
Range("A2:A" & NoOfRows_Data).Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
Range("A1").Select
'Workbooks.Item(TemplateFile).Activate
Worksheets(EEEEEEEEEUHUH).Select
Range("A2:A" & NoOfRows_Data).Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
Range("A1").Select
'Workbooks.Item(TemplateFile).Activate
Worksheets(QQQQQQ_Inv).Select
Range("A2:A" & NoOfRows_Data).Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Sheets(QQQQQQ_Inv).Range("A2").PasteSpecial
Application.CutCopyMode = False
Range("A1").Select
'Workbooks.Item(TemplateFile).Activate
Worksheets(QQQQQQ_Act).Select
Range("A2:A" & NoOfRows_Data).Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
Range("A1").Select
'Workbooks.Item(TemplateFile).Activate
Worksheets(QQQQQQ_Act).Select
Range("L2:V" & NoOfRows_Data).Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
Range("A1").Select
'Workbooks.Item(TemplateFile).Activate
Worksheets(Prod_Data).Select
Range("A2:B" & NoOfRows_Data).Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
Range("A1").Select
'Workbooks.Item(TemplateFile).Activate
Worksheets(Prod_Data).Select
Range("G2:L" & NoOfRows_Data).Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
Range("A1").Select
'Workbooks.Item(TemplateFile).Activate
Worksheets(Report_Detail).Select
Range("A3:AL" & NoOfRows_Data).Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
Range("A1").Select
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
'Save the Template As the Activation Report file
ActiveWorkbook.SaveAs Filename:=(New_Sales_Report_Locn & "\Sales Report V6 - " & Format(strReportDate, "dd.mm.yyyy") & ".xlsx")
ActiveWorkbook.Close SaveChanges:=True
End If
MsgBox ("The Daily Report(s) generation is now complete.")
End Sub
Sub UnzipFile(ByVal sZipFile As String, ByVal sDestFolder As String)
Dim objApp As Object
Dim objArchive As Object
Dim objDest As Object
Dim vDestFolder As Variant
Dim vZipFile As Variant
Set objApp = CreateObject("Shell.Application")
vZipFile = sZipFile
vDestFolder = sDestFolder
If Dir$(sDestFolder, vbDirectory) = "" Then MkDir sDestFolder
objApp.Namespace(vDestFolder).CopyHere objApp.Namespace(vZipFile).Items
End Sub
does anyone know how to add this code to make it a readonly spreadsheet open?
'Open the YYYYYY Vista Data File & copy the data
Set wbJJJVista = Workbooks.Open(JJJ_Vista_File_Locn & "\" & JJJ_VistaFile)
Workbooks.Open(JJJ_Vista_File_Locn & "\" & JJJ_VistaFile).Activate
Worksheets("All Built Orders").Select
Range("A1").Select
Selection.End(xlDown).Select
NoOfRows_Data = ActiveCell.Row

Browse file path and should add it to Vlookup for Reference using VBA

Hi I need to be able to browse a file and add it in vlookup formula for reference using vba...here is my code...please help
I am able to pick a file, but the problem is double time opening of FileDialog folder for selection.
Private Sub CommandButton2_Click()
Range("Q2").Select
FilePath = Application.GetOpenFilename("Excel Files (*.xlsx), *.xls")
If FilePath <> False Then
Range("D6").Value = FilePath
ActiveCell.FormulaR1C1 = _
"=VLookup(RC[-13]:R[68]C[-13],'FilePath'!R2C2:R994C6,5,False)"
Range("Q2").Select
Selection.Copy
Range("P2").Select
Selection.End(xlDown).Select
Selection.Offset(0, 1).Select
Range(Selection, Selection.End(xlUp)).Select
ActiveSheet.Paste
Application.CutCopyMode = False
Range("Q:Q").Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("Q1").Select
Selection.Value = "Payment Method"
Range("Q2").Select
Dim AutoRange As Range
Dim i As Long
Set AutoRange = Range("Q:Q")
For i = 1 To 8000
If AutoRange.Cells(i).Value = "0" Then
AutoRange.Cells(i).Value = "Online"
Else
End If
Next i
ActiveWorkbook.Save
End Sub
I think you are forgetting to name the worksheet from where you want yo search your information in your vlookup formula. Let's assume the data is stored in "sheet1" for every workbook that is eligible:
FilePath_final = Left(FilePath, InStrRev(FilePath, "\")) + "[" + Right(FilePath, Len(FilePath) - InStrRev(FilePath, "\")) + "]"
' That is to put the "[" and "]" before and after the workbook name
ActiveCell.FormulaR1C1 = _
"=VLookup(RC[-13]:R[68]C[-13],'" + FilePath_final + "sheet1'!R2C2:R994C6,5,False)"
Try those 2 lines instead of your vlookup line

How to use pastespecial with End(xlUp)

I am currently having an issue getting the data from one sheet to paste special into another sheet, I am trying to consolidate multiple files (same headers, differing number of rows) into one master sheet containing all the rows. At the moment I'm doing that by opening all the files, pulling in the tabs I want, copy and pasting the data, and then deleting the tabs. Yes I am sure there is an easier way, but I'm very new to VBA and am learning on the fly..here's what I have so far:
Sub ConsolidateSheets()
' open each file in folder
Dim Folder As String
Dim Files As String
Folder = "C:\Users\212411103\Documents\Risk Project Tracker\Risk Project Tracker Monthly\Monthly Data"
Files = Dir(Folder & "\*.xls")
Do While Files <> ""
Workbooks.Open Filename:=Folder & "\" & Files
Files = Dir
Loop
' pull in Risk Project Tracker tab from each file to new workbook
Dim wkb As Workbook
Dim sWksName As String
sWksName = "Risk Project Tracker"
For Each wkb In Workbooks
If wkb.Name <> ThisWorkbook.Name Then
wkb.Worksheets(sWksName).Copy _
Before:=ThisWorkbook.Sheets(1)
End If
Next
Set wkb = Nothing
Dim J As Integer
' add new sheet for combined data
On Error Resume Next
Sheets(1).Select
Worksheets.Add
Sheets(1).Name = "New Month"
' paste headers from first two rows into new sheet "New Month"
Sheets(2).Select
Range("A1:AH2").Select
Selection.Copy
Sheets("New Month").Select
Selection.PasteSpecial Paste:=xlPasteColumnWidths, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
ActiveSheet.Paste
Range("A1").Select
' work through sheets
For J = 2 To Sheets.Count ' from sheet 2 to last sheet
Sheets(J).Activate ' make the sheet active
Rows("1:2").Select
Selection.Delete Shift:=xlUp
Range("A1:AH500").Select
Selection.Copy
Sheets("New Month").Select
Selection.PasteSpecial Paste:=xlPasteColumnWidths, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Range("A1").Select
Next
' Delete tabs that are no longer needed i.e. the tabs from the 17 files
' For Each ws in Sheets
' Application.DisplayAlerts=False
' If ws.Name <> "New Month" Then ws.delete
' Next
' Application.DisplayAlerts=True
End Sub
It appears that the primary reason you are specifying the Range .PasteSpecial method is the carry-over of column widths which is done for every tab. Perhaps cycling through A:AH once and setting the column widths should be sufficient.
Sub ConsolidateSheets2()
Dim fldr As String, fn As String, sWksName As String, sNewWksName As String
Dim ws As Worksheet, wkb As Workbook
On Error GoTo bm_Safe_Exit
Application.ScreenUpdating = False
Application.EnableEvents = False
sWksName = "Risk Project Tracker"
fldr = "C:\Users\212411103\Documents\Risk Project Tracker\Risk Project Tracker Monthly\Monthly Data"
fn = Dir(fldr & "\*.xls")
sNewWksName = "New Month"
With ThisWorkbook
Do While fn <> ""
Set wkb = Workbooks.Open(Filename:=fldr & Chr(92) & fn)
If IsObject(wkb.Worksheets(sWksName)) Then
wkb.Worksheets(sWksName).Copy _
Before:=ThisWorkbook.Sheets(1 - CBool(Sheets(1).Name = sNewWksName))
On Error GoTo bm_Need_New_Month_ws
With .Worksheets(sNewWksName)
On Error GoTo bm_Safe_Exit
.Parent.Sheets(2).Range("A3:AH502").Copy _
Destination:=.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
End With
End If
wkb.Close False
fn = Dir
Loop
Application.DisplayAlerts = False
Do While Sheet.Count > 1: Sheets(2).Delete: Loop
End With
GoTo bm_Safe_Exit
bm_Need_New_Month_ws:
If Err.Number = 9 Then
With ThisWorkbook.Worksheets.Add(Before:=ThisWorkbook.Sheets(1))
.Name = sNewWksName
.Move Before:=Sheets(1)
.Parent.Sheets(2).Range("A1:AH2").Copy _
Destination:=.Range("A1")
For c = .Columns("AH:AH").Column To 1 Step -1
.Columns(c).ColumnWidth = _
.Parent.Sheets(2).Columns(c).ColumnWidth
Next c
End With
Resume
End If
bm_Safe_Exit:
Application.ScreenUpdating = True
Application.EnableEvents = True
End Sub

Copy cells between worksheets

I need the macro to open wkbk(B) goto row (??) based value entered in wkbk(A) copy certain colmns and paste back to col (j14) in wkbk (A).
Sub AutofillData()
Dim wkbkSource As Workbook
Dim strPath As String
Dim myRange As Range
Dim i As Integer
Dim c As Range
Application.ScreenUpdating = False
strPath = "\\"
Set wkbkSource = Workbooks.Open(strPath & Range("A13").Value & ".xls?")
Windows("Book1.xlsm").Activate
Set myRange = Range("i14:i25")
For Each c In myRange
i = c.Value
wkbkSource.Activate
Worksheets("Main Data").Select
Range("D" & i & ":O" & i).Select
Selection.Copy
Windows("Book1.xlsm").Activate
Range("J14").Select
Sheets("Data").Cells(Rows.Count, 9).End(xlUp).Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=True, Transpose:=False
Range("J14").Select
Application.CutCopyMode = False
Next
wkbkSource.Close savechanges:=False
Application.ScreenUpdating = True
End Sub
This will do it
Sub AutofillData()
Dim wkbkSource As Workbook
Dim strPath As String
Dim myRange As Range
Dim i As Integer
Dim c As Range
Dim wkbkTarget As Workbook
Application.ScreenUpdating = False
strPath = "C:\temp\"
Set wkbkA = ThisWorkbook
Set wkbkB = Workbooks.Open(strPath & Range("A13").Value & ".xlsx")
Set myRange = wkbkA.Sheets("Sheet2").Range("i14:i25")
offs = 0
For Each c In myRange
i = c.Value
wkbkB.Worksheets("Main Data").Range("D" & i & ":O" & i).Copy
wkbkA.Sheets("Data").Range("J14").Offset(offs, 0).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
SkipBlanks:=True, Transpose:=False
Application.CutCopyMode = False
offs = offs + 1
Next
wkbkB.Close savechanges:=False
Application.ScreenUpdating = True
End Sub

Creating a macro that copies certain cells and places them into columns

I have 26 worksheets every week that contain a "Station #", "Latitude#", and a "Longitude#".
I want to create a Macro that grabs these 3 cells, copies them and places them into 3 columns named "Station #", "Lat", and "Long.
I'm not very good at this so i need some help.
This is what i got so far:
Sub Macro1()
FolderName = "C:\Users\Captain Wypij\Desktop\Traffic\test"
If Right(FolderName, 1) <> Application.PathSeparator Then FolderName = FolderName & Application.PathSeparator
Fname = Dir(FolderName & "*.xls")
'loop through the files
Do While Len(Fname)
With Workbooks.Open(FolderName & Fname)
Range("C8:D8").Select
Selection.Copy
ChDir "C:\Users\Captain Wypij\Desktop\Traffic"
Workbooks.Open Filename:= _
"C:\Users\Captain Wypij\Desktop\Traffic\Test.xls.xlsx"
Range("A2").Select
If ("A2") = "*" Then Range("A3").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Workbooks.Open (FolderName & Fname)
ActiveWindow.SmallScroll Down:=12
Range("C34:D34").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Test.xls.xlsx").Activate
Range("B2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Workbooks.Open (FolderName & Fname)
Range("G34:H34").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Test.xls.xlsx").Activate
Range("C2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Workbooks.Open (FolderName & Fname)
ActiveWindow.Close
Windows("Test.xls.xlsx").Activate
ActiveWorkbook.Save
ActiveWindow.Close
End With
' go to the next file in the folder
Fname = Dir
Loop
End Sub
I cant seem to figure out how to paste the next worksheet i open in the next fields ( such as A3, B3, C3 and so forth.
Please help me!
Try this:
Dim FolderName As String, Fname As Variant
FolderName = "C:\Location\Folder\"
Fname = Dir(FolderName & "*.xlsx")
Dim wb As Workbook, ws As Worksheet, lr as long
Do While Fname <> ""
Set wb = Workbooks.Open(FolderName & Fname)
Set ws = wb.Sheets("SheetName") '~~> Change to suit
With Thisworkbook.Sheets("Sheet1") '~~> Change to suit
lr = .Range("A" & .Rows.Count).End(xlUp).Row + 1
ws.Range("C8:D8").Copy: .Range("A" & lr).PasteSpecial xlPasteValues
ws.Range("C34:D34").Copy: .Range("B" & lr).PasteSpecial xlPasteValues
ws.Range("G34:H34").Copy: .Range("C" & lr).PasteSpecial xlPasteValues
End With
wb.Close False
Set wb = Nothing: Set ws = Nothing
Fname = Dir
Loop
Above code basically opens all .xlsx file in the specified folder and then copies static ranges. This static ranges are the ones you specified in your question (e.g. Range("C8:D8")). So it copies it and paste it as values on the sheet you will specify. It finds the last row on the destination sheet which will put the copied values below it.
Is this what you're trying? HTH.