Many workbooks consolidation - vba

hello i'm having a problem with the macro bellow on the line 21,
worksheets.add.name = shtname
it seams to me that it's not changing the value of the shtname string once it end the first loop
i´d like to know what's happening hre is the code:
Sub lsConsolidarPlanilhas()
Dim lWorkbook As Workbooks
Dim lWorksheet As Worksheet
Dim lUltimaLinhaAtiva As Long
Dim lControle As Long
Dim lUltimaLinhaAtiva2 As Long
Dim lUltimaLinhaAtiva3 As Long
Dim lUltimaLinhaAtiva4 As Long
Dim shtname As String
Application.DisplayAlerts = False
Application.ScreenUpdating = False
lUltimaLinhaAtiva = Worksheets("Configuração").Cells(Worksheets("Configuração").Rows.Count, 1).End(xlUp).Row
lControle = 2
While lControle <= lUltimaLinhaAtiva
If (Workbooks("Macros.xlsm").Worksheets("Configuração").Range("B" & lControle).Value <> "") Then
shtname = Range("B" & lControle).Text
Worksheets.Add.name = shtname
End If
If (Workbooks("Macros.xlsm").Worksheets("Configuração").Range("B" & lControle).Value <> "") Then
Workbooks.Open Filename:=Worksheets("Configuração").Range("A" & lControle).Value
Set lworkbooks = ActiveWorkbook
If (ActiveWorkbook.Sheets.Count > 1) Then
Workbooks(lworkbooks.name).Worksheets("<LVC>").Activate
lUltimaLinhaAtiva2 = Worksheets("<LVC>").Cells(Worksheets("<LVC>").Rows.Count, 1).End(xlUp).Row
Worksheets("<LVC>").Select
Worksheets("<LVC>").Range("A1:AI18").Select
Selection.Copy
lUltimaLinhaAtiva3 = Workbooks("Macros.xlsm").Worksheets(shtname).Cells(Workbooks("Macros.xlsm").Worksheets(shtname).Rows.Count, 1).End(xlUp).Row + 1
Workbooks("Macros.xlsm").Worksheets(shtname).Activate
Workbooks("Macros.xlsm").Worksheets(shtname).Range("A" & lUltimaLinhaAtiva3).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
For i = 1 To ActiveWorkbook.Sheets.Count
Workbooks(lworkbooks.name).Worksheets(i).Activate
lUltimaLinhaAtiva2 = Worksheets(i).Cells(Worksheets(i).Rows.Count, 1).End(xlUp).Row
Worksheets(i).Select
Worksheets(i).Range("A19:AI" & lUltimaLinhaAtiva2).Select
Selection.Copy
lUltimaLinhaAtiva4 = Workbooks("Macros.xlsm").Worksheets(shtname).Cells(Workbooks("Macros.xlsm").Worksheets(shtname).Rows.Count, 1).End(xlUp).Row + 1
Workbooks("Macros.xlsm").Worksheets(shtname).Activate
Workbooks("Macros.xlsm").Worksheets(shtname).Range("A" & lUltimaLinhaAtiva4).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Next
End If
Workbooks(lworkbooks.name).Worksheets("<LVC>").Activate
lUltimaLinhaAtiva2 = Worksheets("<LVC>").Cells(Worksheets("<LVC>").Rows.Count, 1).End(xlUp).Row
Worksheets("<LVC>").Select
Worksheets("<LVC>").Range("A1:AI" & lUltimaLinhaAtiva2).Select
Selection.Copy
lUltimaLinhaAtiva3 = Workbooks("Macros.xlsm").Worksheets(shtname).Cells(Workbooks("Macros.xlsm").Worksheets(shtname).Rows.Count, 1).End(xlUp).Row + 1
Workbooks("Macros.xlsm").Worksheets(shtname).Activate
Workbooks("Macros.xlsm").Worksheets(shtname).Range("A" & lUltimaLinhaAtiva3).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Else:
Workbooks.Open Filename:=Worksheets("Configuração").Range("A" & lControle).Value
Set lworkbooks = ActiveWorkbook
If (ActiveWorkbook.Sheets.Count > 1) Then
For i = 1 To ActiveWorkbook.Sheets.Count
Workbooks(lworkbooks.name).Worksheets(i).Activate
lUltimaLinhaAtiva2 = Worksheets(i).Cells(Worksheets(i).Rows.Count, 1).End(xlUp).Row
Worksheets(i).Select
Worksheets(i).Range("A19:AI" & lUltimaLinhaAtiva2).Select
Selection.Copy
lUltimaLinhaAtiva4 = Workbooks("Macros.xlsm").Worksheets(shtname).Cells(Workbooks("Macros.xlsm").Worksheets(shtname).Rows.Count, 1).End(xlUp).Row + 1
Workbooks("Macros.xlsm").Worksheets(shtname).Activate
Workbooks("Macros.xlsm").Worksheets(shtname).Range("A" & lUltimaLinhaAtiva4).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Next
End If
Workbooks(lworkbooks.name).Worksheets("<LVC>").Activate
lUltimaLinhaAtiva2 = Worksheets("<LVC>").Cells(Worksheets("<LVC>").Rows.Count, 1).End(xlUp).Row
Worksheets("<LVC>").Select
Worksheets("<LVC>").Range("A19:AI" & lUltimaLinhaAtiva2).Select
Selection.Copy
lUltimaLinhaAtiva3 = Workbooks("Macros.xlsm").Worksheets(shtname).Cells(Workbooks("Macros.xlsm").Worksheets(shtname).Rows.Count, 1).End(xlUp).Row + 1
Workbooks("Macros.xlsm").Worksheets(shtname).Activate
Workbooks("Macros.xlsm").Worksheets(shtname).Range("A" & lUltimaLinhaAtiva3).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End If
Workbooks(lworkbooks.name).Close
lControle = lControle + 1
Wend
Worksheets("Configuração").Select
Worksheets("Configuração").Range("A1").Select
Application.DisplayAlerts = True
Application.ScreenUpdating = True
MsgBox "Planilhas consolidadas!"
End Sub

Thank you #Cor_Blimey that really was the problem, once i've especified the workbook for the shtname it worked perfectly
"unqualified Range() function implicitly refers to the active worksheet which is probably not what was intended. Make it fully qualified by adding Workbooks("Macros.xlsm").Worksheets("Configuração"). in front of it. In general, also, I suggest refactoring the code to eliminate all .Select and .Activate and and instead assigning to temporary worksheet variables so it is transparent and clear what is being acted on (e.g. Set sourceWb = ...Set sourceWs = sourceWb.Worksheets(1)...etc – Cor_Blimey 22 hours ago"

Related

VBA- Copy specific columns to a sheet from a workbook

i need some help how to fix my syntax. Whenever i try to run it there is an error saying "subscript out of range"
I need to copy columns ("B:F"),("J"),(N:Q), (S:V) from Sheet("Filtered Data") to a workbook Sheet("February 2018 Tracker (Raw)")
When i deleted the selected columns ("J"),(N:Q), (S:V) the code is working and copying the data from columns B2:F2.
I know there is something wrong with my syntax but i can't figure it out how to correct it. Please help.
Thanks
Sub L4toMetrics()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Dim MainWorkfile As String
Dim OtherWorkfile As String
MainWorkfile = ActiveWorkbook.Name
lRow = Range("C1048576").End(xlUp).Row
Sheets("February 2018 Tracker (Raw)").Select
Range("B2:Q2" & lRow).ClearContents
Range("C1").Select
Application.AskToUpdateLinks = False
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Workbooks.Open Filename:=Application.GetOpenFilename
OtherWorkfile = ActiveWorkbook.Name
Sheets("Filtered Data").Select
If ActiveWorkbook.ActiveSheet.FilterMode Or _
ActiveWorkbook.ActiveSheet.AutoFilterMode Then _
ActiveWorkbook.ActiveSheet.AutoFilterMode = False
lRw = Range("C1048576").End(xlUp).Row
Range("B2:F2" & lRw).Select
Selection.Copy
Windows(MainWorkfile).Activate
Sheets("February 2018 Tracker (Raw)").Select
lstrw = ActiveSheet.Cells(Rows.Count, "C").End(xlUp).Row + 1
Range("B" & lstrw).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows(OtherWorkfile).Activate
Sheets("Filtered Data").Select
If ActiveWorkbook.ActiveSheet.FilterMode Or _
ActiveWorkbook.ActiveSheet.AutoFilterMode Then _
ActiveWorkbook.ActiveSheet.AutoFilterMode = False
lRw = Range("C1048576").End(xlUp).Row
Range("J2" & lRw).Select
Selection.Copy
Windows(MainWorkfile).Activate
Sheets("February 2018 Tracker (Raw)").Select
lstrw = ActiveSheet.Cells(Rows.Count, "C").End(xlUp).Row + 1
Range("C" & lstrw).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows(OtherWorkfile).Activate
Sheets("Filtered Data").Select
If ActiveWorkbook.ActiveSheet.FilterMode Or _
ActiveWorkbook.ActiveSheet.AutoFilterMode Then _
ActiveWorkbook.ActiveSheet.AutoFilterMode = False
lRw = Range("C1048576").End(xlUp).Row
Range("N2:Q2" & lRw).Select
Selection.Copy
Windows(MainWorkfile).Activate
Sheets("February 2018 Tracker (Raw)").Select
lstrw = ActiveSheet.Cells(Rows.Count, "D").End(xlUp).Row + 1
Range("D" & lstrw).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows(OtherWorkfile).Activate
Sheets("Filtered Data").Select
If ActiveWorkbook.ActiveSheet.FilterMode Or _
ActiveWorkbook.ActiveSheet.AutoFilterMode Then _
ActiveWorkbook.ActiveSheet.AutoFilterMode = False
lRw = Range("C1048576").End(xlUp).Row
Range("S2:O2" & lRw).Select
Selection.Copy
Windows(MainWorkfile).Activate
Sheets("February 2018 Tracker (Raw)").Select
lstrw = ActiveSheet.Cells(Rows.Count, "D").End(xlUp).Row + 1
Range("D" & lstrw).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Application.AskToUpdateLinks = True
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
You are relying too much on the MACRO-Recorder, try the code below to copy>>paste for the first section (columns "B:F").
You be able to implement it for the rest of the columns.
Option Explicit
Sub L4toMetrics()
Dim MainWorkfile As Workbook
Dim OtherWorkfile As Workbook
Dim TrackerSht As Worksheet
Dim FilterSht As Worksheet
Dim lRow As Long, lRw As Long
Application.ScreenUpdating = False
Application.DisplayAlerts = False
' set workbook object
Set MainWorkfile = ActiveWorkbook
' set the worksheet object
Set TrackerSht = MainWorkfile.Sheets("February 2018 Tracker (Raw)")
With TrackerSht
lRow = .Cells(.Rows.Count, "C").End(xlUp).Row ' last row with data in column "C"
.Range("B2:Q2" & lRow).ClearContents
End With
Application.AskToUpdateLinks = False
' set the 2nd workbook object
Set OtherWorkfile = Workbooks.Open(Filename:=Application.GetOpenFilename)
' set the 2nd worksheet object
Set FilterSht = OtherWorkfile.Sheets("Filtered Data")
With FilterSht
If .FilterMode Or .AutoFilterMode Then .AutoFilterMode = False
lRw = .Cells(.Rows.Count, "C").End(xlUp).Row ' last row with data in column "C"
.Range("B2:F" & lRw).Copy ' copy your range
End With
' paste
TrackerSht.Range("B" & lRow).PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
' implement it for the rest of your columns...
End Sub

Macro copies from one workbook into another, Runs with no error but sometimes doesn't work

I am slowly learning VBA in Excel on my own so I'm sure this code can be picked a part. Basically users fill this area with info and click a button that in the background copies the data they populate, opens a new workbook and pastes it in the next open row. There are many users, and for some it works, for others it runs with no error but their info is not pasted in the new. location. Most of the stuff at the end is just reformatting, but I didn't want to take it out in case it could be a part of the problem.
Function IsWorkBookOpen(FileName As String)
Dim ff As Long, ErrNo As Long
On Error Resume Next
ff = FreeFile()
Open FileName For Input Lock Read As #ff
Close ff
ErrNo = Err
On Error GoTo 0
Select Case ErrNo
Case 0: IsWorkBookOpen = False
Case 70: IsWorkBookOpen = True
Case Else: Error ErrNo
End Select
End Function
Sub FF_Temp_Upload()
'
' FF_Temp_Upload Macro
'
Application.ScreenUpdating = False
Dim Workbk As Workbook
Set Workbk = ThisWorkbook
Dim LR As Long
Dim Cell As Long
Dim Ret As String
LR = Range("B" & Rows.Count).End(xlUp).Row
Ret = IsWorkBookOpen("Location of the 2nd workbook/OVS Upload Template.xlsx")
If Ret = True Then
MsgBox "Template is currently being updated elsewhere. Please try again."
Exit Sub
Else
Workbooks.Open FileName:= _
"Location of the 2nd workbook/OVS Upload Template.xlsx"
End If
Workbk.Activate
Range("A2:C" & LR).Select
Selection.Copy
Windows("OVS Upload Template.xlsx").Activate
If Range("A2") = "" Then
Range("A2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Else
Range("A1").Select
Selection.End(xlDown).Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
End If
Workbk.Activate
Range("H2:H" & LR).Select
Application.CutCopyMode = False
Selection.Copy
Windows("OVS Upload Template.xlsx").Activate
If Range("L2") = "" Then
Range("L2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Else
Range("L2").Select
Selection.End(xlDown).Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
End If
ActiveSheet.Range("$A$1:$M$100000").RemoveDuplicates Columns:=1, Header:=xlYes
LR = Range("B" & Rows.Count).End(xlUp).Row
Range("B2:B" & LR) = "=text(left(A2,8),""00000000"")"
Range("B2:B" & LR).Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Range("C2:C" & LR) = "=""DCG""&MID(A2,9,4)"
Range("C2:C" & LR).Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Range("D2:D" & LR).Select
Selection.Formula = "DT"
Range("I2:I" & LR).Select
Selection.Formula = "730"
Range("M2:M" & LR).Select
Selection.Formula = "MAJOH73"
ActiveWorkbook.Save
ActiveWindow.Close
Workbk.Activate
MsgBox "Articles Uploaded"
End Sub
You do not refer to Worksheets anywhere in your code. Thus, for some users, it works and for some it does not.
For those who works - their Excel file was saved with the correct Worksheet selected.
For those who does not work - their Excel file was saved with wrong Worksheet selected. Thus, when it is opened, the ActiveSheet is the wrong one and the code works there.
To fix it (quick and dirty) rewrite your code, refering the worksheet like this:
Worksheets("MyWorksheet").Range("$A$1:$M$100000").RemoveDuplicates Columns:=1
Then try to avoid Selection and ActiveSheet - How to avoid using Select in Excel VBA. At the end, each range or cell should be with refered Worksheet. Like this:
With Worksheets("MyName")
.Range("D2:D" & LR).Formula = "DT"
.Range("I2:I" & LR).Formula = "730"
.Range("M2:M" & LR).Formula = "MAJOH73"
End With

Copying specific ranges from one excel file and distribute them into their appropriate sheets in another workbook

I have a table in the "Source" file, range: AG5:AN5,AG6:AN6...AG16:AN16 and my task is to copy these data and paste them in the appropriate sheets in another "Destination" workbook with 12 sheets in it. Each sheet has the name. Although the range in the source workbook doesn't change, the data contained there changes on daily basis. Therefore these data should be copied not in one fixed cell but should slide down based on last filled-in cell. Copying range in the destination file will be started from "C6" and down. I did record macros and made small corrections but the problem is that when this task is performed workbooks are activated several times and it has a blinking effect. Is it possible to use loop in this example and how can I avoid activation of workbooks during copy-paste operation?
"GL Rates Calculation.xlsm" - Source file
"DPR_ALS_September_2017.xlsx" - Destination file
Here's my code:
Sub GL_DPR_FillIn()
Range("AG5:AN5").Select
Selection.Copy
Windows("DPR_ALS_September_2017.xlsx").Activate
Sheets("Ch24").Select
lastRow = ActiveSheet.Cells(Rows.Count, "C").End(xlUp).Row
ActiveSheet.Range("C" & lastRow + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Windows("GL Rates Calculation.xlsm").Activate
Range("AG6:AN6").Select
Selection.Copy
Windows("DPR_ALS_September_2017.xlsx").Activate
Sheets("Ch30").Select
lastRow = ActiveSheet.Cells(Rows.Count, "C").End(xlUp).Row
ActiveSheet.Range("C" & lastRow + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Windows("GL Rates Calculation.xlsm").Activate
Range("AG7:AN7").Select
Selection.Copy
Windows("DPR_ALS_September_2017.xlsx").Activate
Sheets("Ch54").Select
lastRow = ActiveSheet.Cells(Rows.Count, "C").End(xlUp).Row
ActiveSheet.Range("C" & lastRow + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Windows("GL Rates Calculation.xlsm").Activate
Range("AG8:AN8").Select
Selection.Copy
Windows("DPR_ALS_September_2017.xlsx").Activate
Sheets("Ch56").Select
lastRow = ActiveSheet.Cells(Rows.Count, "C").End(xlUp).Row
ActiveSheet.Range("C" & lastRow + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Windows("GL Rates Calculation.xlsm").Activate
Range("AG9:AN9").Select
Selection.Copy
Windows("DPR_ALS_September_2017.xlsx").Activate
Sheets("Ch60 ").Select
lastRow = ActiveSheet.Cells(Rows.Count, "C").End(xlUp).Row
ActiveSheet.Range("C" & lastRow + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Windows("GL Rates Calculation.xlsm").Activate
Range("AG10:AN10").Select
Selection.Copy
Windows("DPR_ALS_September_2017.xlsx").Activate
Sheets("Ch62").Select
lastRow = ActiveSheet.Cells(Rows.Count, "C").End(xlUp).Row
ActiveSheet.Range("C" & lastRow + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Windows("GL Rates Calculation.xlsm").Activate
Range("AG11:AN11").Select
Selection.Copy
Windows("DPR_ALS_September_2017.xlsx").Activate
Sheets("Ch65").Select
lastRow = ActiveSheet.Cells(Rows.Count, "C").End(xlUp).Row
ActiveSheet.Range("C" & lastRow + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Windows("GL Rates Calculation.xlsm").Activate
Range("AG12:AN12").Select
Selection.Copy
Windows("DPR_ALS_September_2017.xlsx").Activate
Sheets("Ch67").Select
lastRow = ActiveSheet.Cells(Rows.Count, "C").End(xlUp).Row
ActiveSheet.Range("C" & lastRow + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Windows("GL Rates Calculation.xlsm").Activate
Range("AG13:AN13").Select
Selection.Copy
Windows("DPR_ALS_September_2017.xlsx").Activate
Sheets("Ch117").Select
lastRow = ActiveSheet.Cells(Rows.Count, "C").End(xlUp).Row
ActiveSheet.Range("C" & lastRow + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Windows("GL Rates Calculation.xlsm").Activate
Range("AG14:AN14").Select
Selection.Copy
Windows("DPR_ALS_September_2017.xlsx").Activate
Sheets("Ch123").Select
lastRow = ActiveSheet.Cells(Rows.Count, "C").End(xlUp).Row
ActiveSheet.Range("C" & lastRow + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Windows("GL Rates Calculation.xlsm").Activate
Range("AG15:AN15").Select
Selection.Copy
Windows("DPR_ALS_September_2017.xlsx").Activate
Sheets("Ch51").Select
lastRow = ActiveSheet.Cells(Rows.Count, "C").End(xlUp).Row
ActiveSheet.Range("C" & lastRow + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Windows("GL Rates Calculation.xlsm").Activate
Range("AG16:AN16").Select
Selection.Copy
Windows("DPR_ALS_September_2017.xlsx").Activate
Sheets("Ch124").Select
lastRow = ActiveSheet.Cells(Rows.Count, "C").End(xlUp).Row
ActiveSheet.Range("C" & lastRow + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Windows("GL Rates Calculation.xlsm").Activate
End Sub
Give this a try and let me know if you have any problems.
Sub GL_DPR_FillIn()
Dim wbA As Workbook
Dim wbB As Workbook
Dim wsName, lastRow
Dim j
Set wbA = Workbooks("GL Rates Calculation.xlsm") 'This one should already be open
Set wbB = Workbooks.Open("C:\pathname\DPR_ALS_September_2017.xlsx") 'Open the destination workbook
For j = 5 To 16 Step 1
wbA.Worksheets("GL").Range("AG" & j & ":" & "AN" & j).Copy
If j = 5 Then wsName = "Ch24"
If j = 6 Then wsName = "Ch30"
If j = 7 Then wsName = "Ch54"
If j = 8 Then wsName = "Ch56"
If j = 9 Then wsName = "Ch60"
If j = 10 Then wsName = "Ch62"
If j = 11 Then wsName = "Ch65"
If j = 12 Then wsName = "Ch67"
If j = 13 Then wsName = "Ch117"
If j = 14 Then wsName = "Ch123"
If j = 15 Then wsName = "Ch51"
If j = 16 Then wsName = "Ch124"
lastRow = Worksheets(wsName).Cells(Rows.Count, "C").End(xlUp).Row
Worksheets(wsName).Range("C" & lastRow + 1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Next j
'To Close the Destination Workbook, uncomment the following line
'wbB.Close
End Sub

how to shorten my code

I would like someone to look at my code and advice if there is any way to shorten it? Maybe another function that can be used?
The macro copies cells from one worksheet ("macro") to the first empty row in another worksheet ("tracker"). For instance the cell L1 in "macro" needs to be copied to first empty row in column A in "tracker" etc.
Sub tracker_update()
Application.ScreenUpdating = False
Application.Worksheets("macro").Range("D4") = "name"
Application.Worksheets("macro").Range("C10") = "n"
Sheets("macro").Select
Range("L1").Select
Selection.Copy
Sheets("Tracker").Select
lMaxRows = Cells(Rows.Count, "A").End(xlUp).Row
Range("A" & lMaxRows + 1).Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
Sheets("macro").Select
Range("B6").Select
Selection.Copy
Sheets("Tracker").Select
lMaxRows = Cells(Rows.Count, "B").End(xlUp).Row
Range("B" & lMaxRows + 1).Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
Sheets("macro").Select
Range("D4").Select
Selection.Copy
Sheets("Tracker").Select
lMaxRows = Cells(Rows.Count, "C").End(xlUp).Row
Range("C" & lMaxRows + 1).Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
Sheets("macro").Select
Range("B3").Select
Selection.Copy
Sheets("Tracker").Select
lMaxRows = Cells(Rows.Count, "D").End(xlUp).Row
Range("D" & lMaxRows + 1).Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
Sheets("macro").Select
Range("B5").Select
Selection.Copy
Sheets("Tracker").Select
lMaxRows = Cells(Rows.Count, "H").End(xlUp).Row
Range("H" & lMaxRows + 1).Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
Sheets("macro").Select
Range("B7").Select
Selection.Copy
Sheets("Tracker").Select
lMaxRows = Cells(Rows.Count, "I").End(xlUp).Row
Range("I" & lMaxRows + 1).Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
Sheets("macro").Select
Range("B10").Select
Selection.Copy
Sheets("Tracker").Select
lMaxRows = Cells(Rows.Count, "K").End(xlUp).Row
Range("K" & lMaxRows + 1).Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
Sheets("macro").Select
Range("C10").Select
Selection.Copy
Sheets("Tracker").Select
lMaxRows = Cells(Rows.Count, "M").End(xlUp).Row
Range("M" & lMaxRows + 1).Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
Sheets("macro").Select
Range("C10").Select
Selection.Copy
Sheets("Tracker").Select
lMaxRows = Cells(Rows.Count, "L").End(xlUp).Row
Range("L" & lMaxRows + 1).Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
Sheets("macro").Select
Range("L2").Select
Selection.Copy
Sheets("Tracker").Select
lMaxRows = Cells(Rows.Count, "E").End(xlUp).Row
Range("E" & lMaxRows + 1).Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
Sheets("macro").Select
Range("L4").Select
Selection.Copy
Sheets("Tracker").Select
lMaxRows = Cells(Rows.Count, "F").End(xlUp).Row
Range("F" & lMaxRows + 1).Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
Sheets("macro").Select
Range("L5").Select
Selection.Copy
Sheets("Tracker").Select
lMaxRows = Cells(Rows.Count, "G").End(xlUp).Row
Range("G" & lMaxRows + 1).Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
Sheets("macro").Select
Range("A:H").Clear
Columns("A:H").ColumnWidth = 8.43
Rows("1:100").RowHeight = 15
Application.ScreenUpdating = False
End Sub
Please note that I am new in macros and VBA and I used this code because it works prety well, however it takes some time to copy everything.
Regards,
I'd do something like this, allowing you to add new from/to ranges simply by adding them to the arrays:
Sub tracker_update()
Application.ScreenUpdating = False
Dim myLoop As Integer
Dim copyfrom As Variant
Dim pasteto As Variant
Dim sourceSht As Worksheet
Dim targetSht As Worksheet
Dim lMaxRows As Long
Set sourceSht = Sheets("macro")
Set targetSht = Sheets("Tracker")
sourceSht.Range("D4") = "name"
sourceSht.Range("C10") = "n"
copyfrom = Split("L1,B6,D4,B3,B5,B7,B10,C10,C10,L2,L4,L5", ",")
pasteto = Split("A,B,C,D,H,I,K,M,L,E,F,G", ",")
For myLoop = 0 To UBound(copyfrom)
sourceSht.Range(copyfrom(myLoop)).Copy
With targetSht
lMaxRows = .Cells(.Rows.Count, pasteto(myLoop)).End(xlUp).Row
.Range(pasteto(myLoop) & lMaxRows + 1).PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
End With
Next
With sourceSht
.Range("A:H").Clear
.Columns("A:H").ColumnWidth = 8.43
.Rows("1:100").RowHeight = 15
End With
Application.ScreenUpdating = False
End Sub
You can get rid of a lot of your selection statements. For example, try this for your first copy/paste
Sheets("macro").Range("L1").Copy
lMaxRows = Sheets("Tracker").Cells(Rows.Count, "A").End(xlUp).Row
Sheets("Tracker").Range("A" & lMaxRows + 1).PasteSpecial xlPasteValues
You should always declare the worksheet variables that will require less typing and make the code cleaner.
So in your sub routine, declare the sheet variables like below...
Dim sws As Worksheet, dws As Worksheet
Set sws = Sheets("macro")
Set dws = Sheets("Tracker")
Now your first two copy/paste blocks can be shortened as below. Change all other blocks exactly in the same way...
sws.Range("L1").Copy
dws.Range("A" & Rows.Count).End(3)(2).PasteSpecial xlPasteValues
sws.Range("B6").Copy
dws.Range("B" & Rows.Count).End(3)(2).PasteSpecial xlPasteValues
And in the end, don't forget to use the following line to clear the application clipboard.
Application.CutCopyMode = 0
Here are some updates using VBA Best Practices:
Sub tracker_update()
Dim array1(10) As String, array2(10) As String, i As Integer
array1(0) = "L1": array1(1) = "B6": array1(2) = "D4": array1(3) = "B3": array1(4) = "B5": array1(5) = "B7": array1(6) = "B10": array1(7) = "C10": array1(8) = "L2": array1(9) = "L4": array1(10) = "L5"
array2(0) = "A": array2(1) = "B": array2(2) = "C": array2(3) = "D": array2(4) = "H": array2(5) = "I": array2(6) = "K": array2(7) = "M": array2(8) = "L": array2(9) = "E": array2(10) = "F": array2(10) = "G"
'turn off screen updating and popup alerts
Application.ScreenUpdating = False 'turn off screen updating (don't show screen)
Application.DisplayAlerts = False 'turn off popup alerts
Worksheets("macro").Range("D4").Value = "name"
Worksheets("macro").Range("C10").Value = "n"
For i = 0 To UBound(array1)
Sheets("Tracker").Range(array2(i) & findLastRow(array2(i), "Tracker")).Value = Sheets("macro").Range(array1(i)).Value
Next i
'Clean up
With Sheets("macro")
.Range("A:H").Clear
.Columns("A:H").ColumnWidth = 8.43
.Rows("1:100").RowHeight = 15
End With
'turn off screen updating and popup alerts
Application.ScreenUpdating = True 'turn on screen updating (don't show screen)
Application.DisplayAlerts = True 'turn on popup alerts
End Sub
Function findLastRow(ByVal col As String, ByVal sht As String) As Integer
findLastRow = Sheets(sht).Range(col & Sheets(sht).Rows.Count).End(xlUp).Row + 1 'get last row that is empty
End Function

Excel Macro - Repeat a process

I have a recorded macro, for a simple process in Excel. However, I need it to repeat the process for about 80 lines. Here is the code I have for the first 4 lines. Any help on a simple way to do this would be appreciated. Thank you.
Sub Macro2()
'
' Macro2 Macro
'
'
Range("A5").Select
ActiveCell.FormulaR1C1 = "1"
Sheets("EST COST").Select
Range("D6").Select
Selection.Copy
Sheets("IL").Select
Range("I5").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("A5").Select
ActiveCell.FormulaR1C1 = "0"
Range("A6").Select
ActiveCell.FormulaR1C1 = "1"
Sheets("EST COST").Select
Range("D6").Select
Selection.Copy
Sheets("IL").Select
Range("I6").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("A6").Select
ActiveCell.FormulaR1C1 = "0"
Range("A7").Select
ActiveCell.FormulaR1C1 = "1"
Sheets("EST COST").Select
Range("D6").Select
Selection.Copy
Sheets("IL").Select
Range("I7").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("A7").Select
ActiveCell.FormulaR1C1 = "0"
End Sub
You want to use a for...next loop. Some Googling should get you quite far, but here's a flavour of the general idea:
dim startRow as integer
dim endRow as integer
dim myColumn as integer
startRow = 5
endRow = 45
For activeRow = startRow to endRow
[do something]
myColumn = [some column number]
cells(activeRow, myColumn).Value = [something]
Next activeRow
Something like this
Sub test()
Dim wsTarget As Worksheet
Dim wsSource As Worksheet
Set wsTarget = Sheets("EST COST")
Set wsSource = Sheets("IL")
Dim intIndex As Integer
For intIndex = 5 To 85
wsTarget.Range("A" & intIndex).FormulaR1C1 = "1"
wsTarget.Range("D" & intIndex).Copy
With wsSource
.Range("I" & intIndex).PasteSpecial Paste:=xlPasteValues _
, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
.Range("A" & intIndex).FormulaR1C1 = "0"
End With
Next
End Sub
To keep your code as similar as you have it, try this:
Sub test()
Dim rng As Range
Dim i&
For i = 5 To 40
' WHAT SHEET IS YOUR DEFAULT RANGES ON?
Range("A" & i).FormulaR1C1 = "1" ' what sheet is this on? We want to be explicit
Sheets("EST COST").Range("D" & i + 1).Copy
Sheets("IL").Range("I" & i).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("A" & i).FormulaR1C1 = "0"
Next i
End Sub
I'm assuming you want the pasted range to be offset one row (you copy A5, pasted into I6). As I noted though, I'd prefer to know what sheet your ranges to be copied are on, so we can add that worksheet to the ranges (Range("A"& i)... should really be Sheets("mainSheet").Range("A"&i)...)