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
Related
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
I've tried to combine different VBA codes into only one to avoid multiple macros. It works (I get the desired output) but I'd like to know if the code will always work (e.g. if the sheets are not well defined) and if I can simply it (reduce the number of code rows). Here is it :
Sub test2()
Dim sht As Worksheet, cell As Range, areaToTrim As Range, LastRow As Long, lstrow As Long, lrow As Long, sht1 As Worksheet, sht2 As Worksheet
LastRow = Cells(Rows.Count, "B").End(xlUp).Row
Set sht = ThisWorkbook.Worksheets("JDE_Greece")
sht.Activate
Range("A1").EntireColumn.Insert
Sheets("JDE_Greece").Cells(1, 1) = "KEY"
Range("I1").EntireColumn.Insert
sht.Cells(1, 9) = "Quantity JDE (aggregated)"
Range("J1").EntireColumn.Insert
sht.Cells(1, 10) = "Item Code CDL (decomposed)"
Range("K1").EntireColumn.Insert
sht.Cells(1, 11) = "Item Code CDL"
Range("L1").EntireColumn.Insert
sht.Cells(1, 12) = "Quantity CDL (decomposed)"
Range("M1").EntireColumn.Insert
sht.Cells(1, 13) = "Quantity CDL"
Range("N1").EntireColumn.Insert
sht.Cells(1, 14) = "Overwrite (abs in vol)"
Range("O1").EntireColumn.Insert
sht.Cells(1, 15) = "Overwrite (abs in %)"
Range("P1").EntireColumn.Insert
sht.Cells(1, 16) = "Hit/Miss"
Set areaToTrim = Sheets("JDE_Greece").Range("G2:G" & LastRow)
For Each cell In areaToTrim
cell.Value = Trim(cell.Value)
Next cell
Columns("G:G").Select
Selection.NumberFormat = "#"
Range("J2:J" & LastRow).Value = "=IF(ISNA(VLOOKUP(G2,'mapping codes'!A:B,2,0)),G2,VLOOKUP(G2,'mapping codes'!A:B,2,0))"
Range("J2:J" & LastRow).Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Selection.NumberFormat = "#"
Range("K2:K" & LastRow).Formula = "=IF(ISNA(VLOOKUP(""*""&J2&""*"",CDL_Greece!D:D,1,0)),J2,VLOOKUP(""*""&J2&""*"",CDL_Greece!D:D,1,0))"
Range("K2:K" & LastRow).Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Selection.NumberFormat = "#"
Range("A2").Formula = "=K2&B2"
Range("A2").Copy Range("A3:A" & LastRow)
lstrow = Cells(Rows.Count, "A").End(xlUp).Row
Range("I2:I" & lstrow).Value = "=SUMIFS(H:H,G:G,G2,A:A,A2)"
Range("I2:I" & lstrow).Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
ActiveSheet.Range("A2:M" & LastRow).RemoveDuplicates Columns:=1, Header:=xlNo
lrow = Cells(Rows.Count, "A").End(xlUp).Row
Range("L2:L" & lrow).Value = "=VLOOKUP(A2,CDL_Greece!A:I,9,0)"
Range("L2:L" & lrow).Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Range("M2:M" & lrow).Value = "=IF(ISNA(L2),VLOOKUP(VLOOKUP(""*""&G2&""*"",CDL_Greece!D:D,1,0)&B2,CDL_Greece!A:I,9,0),L2)"
Range("M2:M" & lrow).Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Range("N2:N" & lrow).Value = "=ABS(M2-I2)"
Range("O2:O" & lrow).Value = "=ABS(I2-M2)/M2"
Range("O2:O" & lrow).Select
Selection.NumberFormat = "0.00%"
Range("P2:P" & lrow).Value = "=IF(M2=I2,1,0)"
Rows("1:1").Select
Selection.AutoFilter
Range("A1").Interior.Color = RGB(0, 255, 51)
Range("B1:H1").Interior.Color = RGB(255, 153, 102)
Range("I1:M1").Interior.ColorIndex = 37
Range("N1:P1").Interior.ColorIndex = 6
Set sht = ThisWorkbook.Worksheets("JDE_Greece")
Set sht1 = ThisWorkbook.Worksheets("Mismatches")
With sht
lrow = .Cells(.Rows.Count, "A").End(xlUp).Row
.Range("M1").AutoFilter Field:=13, Criteria1:="#N/A"
.Range("A1:P" & lrow).SpecialCells(xlCellTypeVisible).Copy
End With
sht1.Range("A1").PasteSpecial Paste:=xlPasteValuesAndNumberFormats
Application.CutCopyMode = False
Set sht2 = ThisWorkbook.Worksheets.Add
With sht2
.Name = "Summary DRP"
.Move After:=ThisWorkbook.Worksheets("Instructions")
End With
With sht
.Range("M1").AutoFilter Field:=13, Criteria1:="<>#N/A"
.Range("A1:P" & lrow).SpecialCells(xlCellTypeVisible).Copy
End With
sht2.Range("A1").PasteSpecial Paste:=xlPasteValuesAndNumberFormats
End Sub
Sorry in advance if it's a bit long (I've even removed my personal comments..).
Thanks a lot for your expertise :)
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
This is my first macro, and I need some help. I keep changing my variables in sheet 1, and run another macro in sheet 2 to get my results. So this is a sensitivity test and I'm writing the following macro to run an already existing marco. Some of the rows it generates seem to be correct, but some of them are not. I can't figure out what went wrong. Any tips are appreciated.
Sub SensitivityTest()
For i = 8 To 11
Range("G" & i + 1).Select
Selection.Copy
Range("D10").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("D15").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Sheet2").Select
Call AnotherMacro
Range("Q76").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Range("H" & i + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Sheet2").Select
Range("AD76").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Range("I" & i + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Sheet2").Select
Range("Q20").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Range("J" & i + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Sheet2").Select
Range("AD20").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Range("K" & i + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Sheet2").Select
Range("Q27").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Range("L" & i + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Sheet2").Select
Range("AD27").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Range("M" & i + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Sheet2").Select
Range("Q28").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Range("N" & i + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Sheet2").Select
Range("AD28").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Range("O" & i + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Sheet2").Select
Range("V76").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Range("Q" & i + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Sheet2").Select
Range("AI76").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Range("R" & i + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Sheet2").Select
Range("V20").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Range("S" & i + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Sheet2").Select
Range("AI20").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Range("T" & i + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Sheet2").Select
Range("V27").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Range("U" & i + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Sheet2").Select
Range("AI27").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Range("V" & i + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Sheet2").Select
Range("V28").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Range("W" & i + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Sheet2").Select
Range("AI28").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Range("X" & i + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Next i
End Sub
To follow up on #bruceWayne's comment:
Current copy/paste operation:
Sheets("Sheet2").Select
Range("AD76").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Range("I" & i + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Without selecting/activating:
Sheets("Sheet2").Range("AD76").Copy
Sheets("Sheet1").Range("I" & i + 1).PasteSpecial Paste:=xlPasteValues
Because I was bored while eating my lunch, I decided to rewrite the code to see how much it would reduce to after getting rid of all the .Select, Selection. bits (plus a few other bits of tidying). I came up with this:
Sub SensitivityTest()
With Sheets("Sheet1")
For i = 8 To 11
.Range("D10").Value = .Range("G" & i + 1).Value
.Range("D15").Value = .Range("G" & i + 1).Value
'This next line shouldn't be required if "AnotherMacro" was suitably changed
'to fully qualify all ranges, etc, being referred to
Sheets("Sheet2").Select
Call AnotherMacro
'Because the original code was pasting values, I have changed the
'code to just set the destination cell's Value equal to the
'source cell's Value. This avoids using the clipboard, which
'often leads to problems if the user is doing something else
'while a macro is running.
.Range("H" & i + 1).Value = Sheets("Sheet2").Range("Q76").Value
.Range("I" & i + 1).Value = Sheets("Sheet2").Range("AD76").Value
.Range("J" & i + 1).Value = Sheets("Sheet2").Range("Q20").Value
.Range("K" & i + 1).Value = Sheets("Sheet2").Range("AD20").Value
.Range("L" & i + 1).Value = Sheets("Sheet2").Range("Q27").Value
.Range("M" & i + 1).Value = Sheets("Sheet2").Range("AD27").Value
.Range("N" & i + 1).Value = Sheets("Sheet2").Range("Q28").Value
.Range("O" & i + 1).Value = Sheets("Sheet2").Range("AD28").Value
.Range("Q" & i + 1).Value = Sheets("Sheet2").Range("V76").Value
.Range("R" & i + 1).Value = Sheets("Sheet2").Range("AI76").Value
.Range("S" & i + 1).Value = Sheets("Sheet2").Range("V20").Value
.Range("T" & i + 1).Value = Sheets("Sheet2").Range("AI20").Value
.Range("U" & i + 1).Value = Sheets("Sheet2").Range("V27").Value
.Range("V" & i + 1).Value = Sheets("Sheet2").Range("AI27").Value
.Range("W" & i + 1).Value = Sheets("Sheet2").Range("V28").Value
.Range("X" & i + 1).Value = Sheets("Sheet2").Range("AI28").Value
Next i
'Include a final select of Sheet1, just to get around the effect of
'doing the Select of Sheet2 during the macro. This wouldn't be
'needed if AnotherMacro was similarly tidied up to not require
'Sheet2 to be Selected before running.
.Select
End With
End Sub
I find this much easier to read, and therefore it would be a lot easier to maintain and debug when necessary.
P.S. All the i + 1 statements could be changed to just i if the loop was changed from For i = 8 To 11 to be For i = 9 To 12.
P.P.S. My guess as to why your code sometimes worked and sometimes didn't is that your code was dependent on Sheet1 being the active sheet when you invoked the macro. If Sheet2 was active, it would almost certainly not do what you wanted it to do.
Thank you all for the help! When I ran the following codes on Friday, it got stuck at the last few rows, and the same results kept repeating itself. But when I let it ran after work and not doing other things on the computer, it worked !
Sub SensitivityTest()
With Sheets("Sheet1")
For i = 9 To 40
.Range("D10").value = .Range("G" & i).value
.Range("D15").value = .Range("G" & i).value
Call AnotherMacro
.Range("H" & i).value = Sheets("Sheet2").Range("Q76").value
.Range("I" & i).value = Sheets("Sheet2").Range("AD76").value
.Range("J" & i).value = Sheets("Sheet2").Range("Q20").value
.Range("K" & i).value = Sheets("Sheet2").Range("AD20").value
.Range("L" & i).value = Sheets("Sheet2").Range("Q23").value
.Range("M" & i).value = Sheets("Sheet2").Range("AD23").value
.Range("N" & i).value = Sheets("Sheet2").Range("Q28").value
.Range("O" & i).value = Sheets("Sheet2").Range("AD28").value
.Range("Q" & i).value = Sheets("Sheet2").Range("V76").value
.Range("R" & i).value = Sheets("Sheet2").Range("AI76").value
.Range("S" & i).value = Sheets("Sheet2").Range("V20").value
.Range("T" & i).value = Sheets("Sheet2").Range("AI20").value
.Range("U" & i).value = Sheets("Sheet2").Range("V23").value
.Range("V" & i).value = Sheets("Sheet2").Range("AI23").value
.Range("W" & i).value = Sheets("Sheet2").Range("V28").value
.Range("X" & i).value = Sheets("Sheet2").Range("AI28").value
Next i
End With
End Sub
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"