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
Related
I already got how can i copy specific column from another workbook but now i also need to filter a specific column. I have tried this code but i encounter an error "Subscript out of range".
I need to filter Column C that contains "Mary" and copy its corresponding data.
This is the sample of my code, I know there is something wrong with my syntax especially in using auto filter for COLUMN C and copying different column and paste it to another workbook. Please help me to make it right. Thanks
Sub RAWtransfertoTRUST()
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("Trust Activities Raw")
With TrackerSht
lRow = .Cells(.Rows.Count, "B").End(xlUp).Row
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("Raw Data")
With FilterSht
.AutoFilterMode = False
.Range("B2:F").AutoFilter Field:=3, Criteria1:="Mary"
lRw = .Cells(.Rows.Count, "B").End(xlUp).Row
End With
' paste
TrackerSht.Range("B" & lRow).PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
With FilterSht
If .FilterMode Or .AutoFilterMode Then .AutoFilterMode = False
lRw = .Cells(.Rows.Count, "C").End(xlUp).Row
.Range("J1:J" & lRw).Copy ' copy your range
End With
' paste
TrackerSht.Range("G" & lRow).PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
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("N1:Q" & lRw).Copy ' copy your range
End With
' paste
TrackerSht.Range("H" & lRow).PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
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("T1:W" & lRw).Copy ' copy your range
End With
' paste
TrackerSht.Range("L" & lRow).PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
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("Y1:Z" & lRw).Copy ' copy your range
End With
' paste
TrackerSht.Range("P" & lRow).PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
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("AB1:AC" & lRw).Copy ' copy your range
End With
' paste
TrackerSht.Range("R" & lRow).PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
End Sub
So, a few issues here.
In this code block:
With FilterSht
.AutoFilterMode = False
.Range("B2:F").AutoFilter Field:=3, Criteria1:="Mary"
lRw = .Cells(.Rows.Count, "B").End(xlUp).Row
End With
You are missing a number in the range B2:F. If you want to filter the entire column, then both you should exclude the number "2" from B2. I assume that you were wanting to use the lRw that is actually on the next line, so this would need to go above your range line, then you would need to include that with your B2:F by adding & lRw.
That line should now look like:
.Range("B2:F" & lRw).AutoFilter Field:=2, Criteria1:="Mary"
Also, keep in mind that this is not including row 2 in your autofilter. I assume you were wanting to filter row 2, so you would need to change it to B1: if this was the case.
Next issue is your copy / paste method. You are not pasting anything, because you never copied it. In the same With block, you can add this line: .AutoFilter.Range.Copy
Here's your final result:
Sub RAWtransfertoTRUST()
Dim MainWorkfile As Workbook, OtherWorkfile As Workbook
Dim TrackerSht As Worksheet, FilterSht As Worksheet
Dim lRow As Long, lRw As Long
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Set MainWorkfile = ActiveWorkbook
Set TrackerSht = MainWorkfile.Sheets("Trust Activities Raw")
With TrackerSht
lRow = .Cells(.Rows.Count, "B").End(xlUp).Row
End With
Application.AskToUpdateLinks = False
Set OtherWorkfile = Workbooks.Open(Filename:=Application.GetOpenFilename)
Set FilterSht = OtherWorkfile.Sheets("Raw Data")
With FilterSht
.AutoFilterMode = False
lRw = .Cells(.Rows.Count, "B").End(xlUp).Row
.Range("B1:F" & lRw).AutoFilter Field:=3, Criteria1:="Mary"
.AutoFilter.Range.Copy
End With
' paste
TrackerSht.Range("B" & lRow).PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
With FilterSht
If .FilterMode Or .AutoFilterMode Then .AutoFilterMode = False
lRw = .Cells(.Rows.Count, "C").End(xlUp).Row
.Range("J1:J" & lRw).Copy ' copy your range
End With
' paste
TrackerSht.Range("G" & lRow).PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
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("N1:Q" & lRw).Copy ' copy your range
End With
' paste
TrackerSht.Range("H" & lRow).PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
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("T1:W" & lRw).Copy ' copy your range
End With
' paste
TrackerSht.Range("L" & lRow).PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
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("Y1:Z" & lRw).Copy ' copy your range
End With
' paste
TrackerSht.Range("P" & lRow).PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
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("AB1:AC" & lRw).Copy ' copy your range
End With
' paste
TrackerSht.Range("R" & lRow).PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
End Sub
Oh, and I slightly cleaned up your code formatting :D
Thanks for all your help, i already resolved my issue. I just filter all the columns then delete the columns that i don't need. This is my sample code.
Sub RAWtransfertoTRUST()
Dim MainWorkfile As Workbook, OtherWorkfile As Workbook
Dim TrackerSht As Worksheet, FilterSht As Worksheet
Dim lRow As Long, lRw As Long
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Set MainWorkfile = ActiveWorkbook
Set TrackerSht = MainWorkfile.Sheets("Trust Activities Raw")
With TrackerSht
lRow = .Cells(.Rows.Count, "C").End(xlUp).Row
End With
Application.AskToUpdateLinks = False
Set OtherWorkfile = Workbooks.Open(Filename:=Application.GetOpenFilename)
Set FilterSht = OtherWorkfile.Sheets("Raw Data")
With FilterSht
.AutoFilterMode = False
lRw = .Cells(.Rows.Count, "C").End(xlUp).Row
.Range("B1:W" & lRw).AutoFilter Field:=2, Criteria1:="Mary"
.AutoFilter.Range.Copy
End With
TrackerSht.Range("B" & lRow).PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
With TrackerSht
.Range("G:I,K:M,R:S,X:AD").DELETE Shift:=xlToLeft
.Range("E:E").Copy
.Range("G:O").PasteSpecial Paste:=xlPasteFormats
.Range("G2", "G1000").NumberFormat = "dd/mm/yyyy"
.Range("M2", "M1000").Interior.ColorIndex = 41
.Range("J2", "J1000").Interior.ColorIndex = 6
End With
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 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
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"
My program works by calling a number of macros as such:
Sub Start()
Call ClearAll
Call Sales_Download
Call Copy_Sales
Call Receipt_Download
Call Copy_Receipt
Call Copy1
Call Sales_Summary
Call Copy2
Call Receipt_Summary
End Sub
My program breaks at the copy2, which is essentially an exact replica of copy1 wich works fine. When copy2 is ran by itself it works perfectly, but when I attempt to run the entire program it debugs. The bolded line is where the debug happens.
Sub Copy2()
' Copies all data from Receipt Download tab for each location, and saves in a seperate folder
Dim i As Long
Dim lngLastRow As Long, lngPasteRow As Long
'Find the last row to search through
lngLastRow = Sheets("Receipt_Download").Range("J65535").End(xlUp).Row
'Initialize the Paste Row
lngPasteRow = 2
Dim rng As Range
Dim c As Range
Dim endrow
Dim strName As String
Dim ws As Worksheet
Dim j As Long
endrow = Sheets("names").Range("A65000").End(xlUp).Row
Set rng = Sheets("names").Range("A2:A" & endrow)
j = 1
FBO = strName
For Each c In rng
For i = 2 To lngLastRow
strName = c.Value
If Sheets("Receipt_Download").Range("J" & i).Value = strName Then
Sheets("Receipt_Download").Select
Range("A" & i & ":IV" & i).Copy
Sheets("Summary").Select
Range("A" & lngPasteRow & ":IV" & lngPasteRow).Select
ActiveSheet.Paste
lngPasteRow = lngPasteRow + 1
End If
Next i
j = j + 1
Sheets("Receipt_Download").Select
Rows("1:1").Select
Selection.Copy
Sheets("Summary").Select
Rows("1:1").Select
ActiveSheet.Paste
Columns("D:E").Select
Selection.NumberFormat = "m/d/yyyy"
Sheets("Summary").Select
Range("B25000").Select
ActiveCell.FormulaR1C1 = "Grand Total"
Range("B25000").Select
Selection.Font.Bold = True
Columns("G:G").Select
Selection.Insert Shift:=xlToRight
Range("G1").Select
ActiveCell.FormulaR1C1 = "=IF(RC[-2]=0,""0"",RC[-1])"
Range("G1").Select
Selection.AutoFill Destination:=Range("G1:G24950")
Range("G25000").Select
ActiveCell.FormulaR1C1 = "=SUM(R[-24950]C:R[-1]C)"
Range("G25000").Select
Selection.Copy
Range("F25000").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Columns("G:G").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
Sheets("Summary").Select
Range("F25000").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Names").Select
With Columns("B")
.Find(what:="", after:=.Cells(1, 1), LookIn:=xlValues).Activate
End With
ActiveSheet.Paste
Sheets("Summary").Select
Range("b1:b30000").Select
For Each Cell In Selection
If Cell.Value = "" Then
Cell.ClearContents
End If
Next Cell
Range("b1:b30000").Select
Selection.SpecialCells(xlCellTypeBlanks).EntireRow.Delete
Sheets("Summary").Select
Range("D2").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Names").Select
***With Columns("C")
.Find(what:="", after:=.Cells(1, 1), LookIn:=xlValues).Activate***
End With
ActiveSheet.Paste
Sheets("Summary").Select
Range("A1:Z5000").Select
Selection.Copy
Workbooks.Add
ActiveSheet.Paste
Range("A1").Select
Application.CutCopyMode = False
Selection.Copy
Application.CutCopyMode = False
File = "C:\Documents and Settings\user\Desktop\New FBO\" & strName & "\" & strName & " Receipts.xls"
ActiveWorkbook.SaveAs Filename:=File, _
FileFormat:=xlNormal, Password:="", WriteResPassword:="", _
ReadOnlyRecommended:=False, CreateBackup:=False
ActiveWorkbook.Close
IngPasteRow = IngPasteRow + 1
Sheets("Summary").Select
Selection.ClearContents
Next c
End Sub
I would really appreciate any help, I am certainly no VBA master and this has been quite troublesome.
Replace this part of your code
Sheets("Summary").Select
Range("D2").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Names").Select
With Columns("C")
.Find(what:="", after:=.Cells(1, 1), LookIn:=xlValues).Activate
End With
ActiveSheet.Paste
with
Dim lRow As Long
With Sheets("Names")
lRow = .Range("C" & .Rows.Count).End(xlUp).Row + 1
Sheets("Summary").Range("D2").Copy .Range("C" & lRow)
End With
Now try it.
Also few tips
Avoid .Select and .Activate They are a major cause of errors
Indent and appropriately comment your code. Your code is very difficult to read. If you don't indent/comment your code, you will realize that you will not recognize your OWN code if you visit it say after a week :)
In support of Siddharth's answer above, I have take a portion of your code (up to where your break happens) and have indented and avoided the .Select and .Activate that he mentions. Hopefully this gives you a good start on how to make your code more readable for debugging and understanding.
For Each c In rng
For i = 2 To lngLastRow
strName = c.Value
If Sheets("Receipt_Download").Range("J" & i).Value = strName Then
Sheets("Receipt_Download").Range("A" & i & ":IV" & i).Copy _
Destination:=Sheets("Summary").Range("A" & lngPasteRow & ":IV" & lngPasteRow)
lngPasteRow = lngPasteRow + 1
End If
Next i
j = j + 1
Sheets("Receipt_Download").Rows("1:1").Copy Destination:=Sheets("Summary").Rows("1:1")
With Sheets("Summary")
.Columns("D:E").NumberFormat = "m/d/yyyy"
With .Range("B25000")
.Formula = "Grand Total"
.Font.Bold = True
End With
.Columns("G:G").Insert Shift:=xlToRight
With Range("G1")
.FormulaR1C1 = "=IF(RC[-2]=0,""0"",RC[-1])"
.AutoFill Destination:=Range("G1:G24950")
End With
With ("G25000")
.FormulaR1C1 = "=SUM(R[-24950]C:R[-1]C)"
.Copy
End With
.Range("F25000").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
.Columns("G:G").Delete Shift:=xlToLeft
.Range("F25000").Copy Destination:=Sheets("Names").Columns("B").Find(what:="", after:=Sheets("Names").Cells(1, 1), LookIn:=xlValues)
End With