How to copy and find the last 125 row? - excel

I have a task where I need to get the last 125 data from an excel workbook copied to another workbook. And I want the user to select from a file browser the excel file where the data has been stored. The data will always in the the range of C17:C2051, F17:F2051 and goes on...
At last I want to put two formula above these ranges.
There are the formulas:
=AVARAGE(INDEX(C17:C2051;MATCH(MAX(C17:C2051);C17:C2051;1)):INDEX(C17:C2051;MAX(1;MATCH(MAX(C17:C2051);C17:C2051;1)-124)))
=STDEV(INDEX(C17:C2051;MATCH(MAX(C17:C2051);C17:C2051;1)):INDEX(C17:C2051;MAX(1;MATCH(MAX(C17:C2051);C17:C2051;1)-124)))
I wrote some code but right now it's actually doing nothing.
Sub Get_Data_From_File()
Dim FileToOpen As String
Dim File As Workbook
FileToOpen = Application.GetOpenFilename("Excel files (*.xlsx), *.xlsx")
Dim LastRow As Long
Dim Last8Rows As Range
LastRow = File.Range("D" & File.Rows.Count).End(xlUp).Row
Set Last8Rows = File.Range("C" & LastRow - 7)
Last8Rows.Copy
End Sub

This should get you started:
Sub Get_Data_From_File()
Const START_ROW As Long = 17
Const NUM_ROWS As Long = 125
Dim FileToOpen As String
Dim wb As Workbook, ws As Worksheet, wsDest As Worksheet
Dim LastRow As Long, FirstRow As Long
Dim LastRows As Range
FileToOpen = Application.GetOpenFilename("Excel files (*.xlsx), *.xlsx", _
Title:="Select file to import from")
If FileToOpen = False Then Exit Sub 'no file selected
Set wsDest = ActiveSheet 'pasting here; or specfy some other sheet...
Set wb = Workbooks.Open(FileToOpen, ReadOnly:=True)
Set ws = wb.Worksheets("data") 'or whatever sheet you need
LastRow = ws.Cells(ws.Rows.Count, "D").End(xlUp).Row 'find last row
If LastRow < START_ROW Then LastRow = START_ROW
FirstRow = IIf(LastRow - NUM_ROWS >= START_ROW, LastRow - NUM_ROWS, START_ROW) 'find first row
'copy ranges
ws.Range("C" & FirstRow & ":C" & LastRow).Copy wsDest.Cells(START_ROW, "C")
ws.Range("F" & FirstRow & ":F" & LastRow).Copy wsDest.Cells(START_ROW, "F")
'Add the formulas (note you need the US-format when using .Formula
' or you can use your local format with .FormulaLocal
wb.Close False 'no save
End Sub

After all these days it's working finally. I modified some lines in the code and it's doing the job. Thanks again #TimWilliams!
Here's my solution:
Sub Get_Data_From_File()
Const START_ROW As Long = 17
Const NUM_ROWS As Long = 124
Dim FileToOpen As String
Dim wb As Workbook, ws As Worksheet, wsDest As Worksheet
Dim LastRow As Long, FirstRow As Long
Dim LastRows As Range
FileToOpen = Application.GetOpenFilename("Excel files (*.xlsx), *.xlsx", _
Title:="Select file to import from") 'no file selected
Set wsDest = ActiveSheet 'pasting here; or specfy some other sheet...
Set wb = Workbooks.Open(FileToOpen, ReadOnly:=True)
Set ws = wb.Worksheets("SMI_650_Lxy") 'or whatever sheet you need
LastRow = ws.Cells(ws.Rows.Count, "F").End(xlUp).Row 'find last row
If LastRow < START_ROW Then LastRow = START_ROW
FirstRow = IIf(LastRow - NUM_ROWS >= START_ROW, LastRow - NUM_ROWS, START_ROW) 'find first row
Debug.Print "FirstRow" & vbTab & FirstRow 'test
Debug.Print "LastRow" & vbTab & LastRow
Debug.Print "START_ROW" & vbTab & START_ROW
'copy ranges
ws.Range("C" & FirstRow & ":C" & LastRow).Copy wsDest.Cells(START_ROW, "C")
ws.Range("F" & FirstRow & ":F" & LastRow).Copy wsDest.Cells(START_ROW, "F")
ws.Range("M" & FirstRow & ":M" & LastRow).Copy wsDest.Cells(START_ROW, "M") 'formula
ws.Range("P" & FirstRow & ":P" & LastRow).Copy wsDest.Cells(START_ROW, "P")
ws.Range("S" & FirstRow & ":S" & LastRow).Copy wsDest.Cells(START_ROW, "S")
ws.Range("V" & FirstRow & ":V" & LastRow).Copy wsDest.Cells(START_ROW, "V")
ws.Range("Y" & FirstRow & ":Y" & LastRow).Copy wsDest.Cells(START_ROW, "Y")
ws.Range("AF" & FirstRow & ":AF" & LastRow).Copy wsDest.Cells(START_ROW, "AF") 'formula
ws.Range("AM" & FirstRow & ":AM" & LastRow).Copy wsDest.Cells(START_ROW, "AM") 'formula
ws.Range("AP" & FirstRow & ":AP" & LastRow).Copy wsDest.Cells(START_ROW, "AP")
ws.Range("AS" & FirstRow & ":AS" & LastRow).Copy wsDest.Cells(START_ROW, "AS")
ws.Range("AV" & FirstRow & ":AV" & LastRow).Copy wsDest.Cells(START_ROW, "AV")
ws.Range("AY" & FirstRow & ":AY" & LastRow).Copy wsDest.Cells(START_ROW, "AY")
ws.Range("BB" & FirstRow & ":BB" & LastRow).Copy wsDest.Cells(START_ROW, "BB")
ws.Range("BE" & FirstRow & ":BE" & LastRow).Copy wsDest.Cells(START_ROW, "BE")
ws.Range("BL" & FirstRow & ":BL" & LastRow).Copy wsDest.Cells(START_ROW, "BL") 'formula
ws.Range("BS" & FirstRow & ":BS" & LastRow).Copy wsDest.Cells(START_ROW, "BS") 'formula
ws.Range("BV" & FirstRow & ":BV" & LastRow).Copy wsDest.Cells(START_ROW, "BV")
ws.Range("BZ" & FirstRow & ":BZ" & LastRow).Copy wsDest.Cells(START_ROW, "BZ")
ws.Range("CD" & FirstRow & ":CD" & LastRow).Copy wsDest.Cells(START_ROW, "CD")
ws.Range("CH" & FirstRow & ":CH" & LastRow).Copy wsDest.Cells(START_ROW, "CH")
ws.Range("CK" & FirstRow & ":CK" & LastRow).Copy wsDest.Cells(START_ROW, "CK")
ws.Range("CN" & FirstRow & ":CN" & LastRow).Copy wsDest.Cells(START_ROW, "CN")
ws.Range("CQ" & FirstRow & ":CQ" & LastRow).Copy wsDest.Cells(START_ROW, "CQ")
ws.Range("CT" & FirstRow & ":CT" & LastRow).Copy wsDest.Cells(START_ROW, "CT")
ws.Range("CW" & FirstRow & ":CW" & LastRow).Copy wsDest.Cells(START_ROW, "CW")
ws.Range("CZ" & FirstRow & ":CZ" & LastRow).Copy wsDest.Cells(START_ROW, "CZ")
ws.Range("DC" & FirstRow & ":DC" & LastRow).Copy wsDest.Cells(START_ROW, "DC")
ws.Range("DF" & FirstRow & ":DF" & LastRow).Copy wsDest.Cells(START_ROW, "DF")
'Add the formulas (note you need the US-format when using .Formula
' or you can use your local format with .FormulaLocal
wb.Close False 'no save
End Sub

Related

I want to copy data from another workbook to my current workbook using vba code

I want to write a code similar to UDF where I hardcode a function and then create parameters for it for the code below. This is for opening an excel file and from external workbook and copy pasting the values from certain columns to the active workbook.
Private Sub Btn_Load_Test_Data_file_Click()
Dim FileLocation As String
Dim LastRow As Long
Dim wb As Workbook
Set wb = ActiveWorkbook
FileLocation = Application.GetOpenFilename
If FileLocation = "False" Then
Beep
Exit Sub
End If
Application.ScreenUpdating = False
Set ImportWorkbook = Workbooks.Open(Filename:=FileLocation)
LastRow = ImportWorkbook.Worksheets("Projects").Range("A7").End(xlDown).row
curr_lrow = wb.Worksheets("Projects").Range("A5").End(xlDown).row
'Copy range to clipboard
ImportWorkbook.Worksheets("Projects").Range("B7", "B" & LastRow).Copy
'PasteSpecial to paste values, formulas, formats, etc.
wb.Worksheets("Projects").Range("A" & LastRow).PasteSpecial Paste:=xlPasteValues
ImportWorkbook.Worksheets("Projects").Range("C7", "C" & LastRow).Copy
wb.Worksheets("Projects").Range("C" & LastRow).PasteSpecial Paste:=xlPasteValues
ImportWorkbook.Worksheets("Projects").Range("D7", "D" & LastRow).Copy
wb.Worksheets("Projects").Range("E" & LastRow).PasteSpecial Paste:=xlPasteValues
ImportWorkbook.Worksheets("Projects").Range("E7", "E" & LastRow).Copy
wb.Worksheets("Projects").Range("F" & LastRow).PasteSpecial Paste:=xlPasteValues
ImportWorkbook.Worksheets("Projects").Range("F7", "F" & LastRow).Copy
wb.Worksheets("Projects").Range("G" & LastRow).PasteSpecial Paste:=xlPasteValues
ImportWorkbook.Worksheets("Projects").Range("G7", "G" & LastRow).Copy
wb.Worksheets("Projects").Range("H" & LastRow).PasteSpecial Paste:=xlPasteValues
ImportWorkbook.Worksheets("Projects").Range("H7", "H" & LastRow).Copy
wb.Worksheets("Projects").Range("I" & LastRow).PasteSpecial Paste:=xlPasteValues
ImportWorkbook.Worksheets("Projects").Range("I7", "I" & LastRow).Copy
wb.Worksheets("Projects").Range("J" & LastRow).PasteSpecial Paste:=xlPasteValues
ImportWorkbook.Worksheets("Projects").Range("J7", "J" & LastRow).Copy
wb.Worksheets("Projects").Range("K" & LastRow).PasteSpecial Paste:=xlPasteValues
ImportWorkbook.Worksheets("Projects").Range("K7", "K" & LastRow).Copy
wb.Worksheets("Projects").Range("L" & LastRow).PasteSpecial Paste:=xlPasteValues
ImportWorkbook.Worksheets("Projects").Range("L7", "L" & LastRow).Copy
wb.Worksheets("Projects").Range("M" & LastRow).PasteSpecial Paste:=xlPasteValues
ImportWorkbook.Worksheets("Projects").Range("M", "M" & LastRow).Copy
wb.Worksheets("Projects").Range("N" & LastRow).PasteSpecial Paste:=xlPasteValues
ImportWorkbook.Worksheets("Projects").Range("N7", "N" & LastRow).Copy
wb.Worksheets("Projects").Range("O" & LastRow).PasteSpecial Paste:=xlPasteValues
ImportWorkbook.Worksheets("Projects").Range("O7", "O" & LastRow).Copy
wb.Worksheets("Projects").Range("P" & LastRow).PasteSpecial Paste:=xlPasteValues
ImportWorkbook.Worksheets("Projects").Range("P7", "P" & LastRow).Copy
wb.Worksheets("Projects").Range("Q" & LastRow).PasteSpecial Paste:=xlPasteValues
ImportWorkbook.Worksheets("Projects").Range("Q7", "Q" & LastRow).Copy
wb.Worksheets("Projects").Range("R" & LastRow).PasteSpecial Paste:=xlPasteValues
ImportWorkbook.Worksheets("Projects").Range("R7", "R" & LastRow).Copy
wb.Worksheets("Projects").Range("BL" & LastRow).PasteSpecial Paste:=xlPasteValues
ImportWorkbook.Worksheets("Projects").Range("S7", "S" & LastRow).Copy
wb.Worksheets("Projects").Range("BM" & LastRow).PasteSpecial Paste:=xlPasteValues
ImportWorkbook.Worksheets("Projects").Range("T7", "T" & LastRow).Copy
wb.Worksheets("Projects").Range("BN" & LastRow).PasteSpecial Paste:=xlPasteValues
ImportWorkbook.Worksheets("Projects").Range("U7", "U" & LastRow).Copy
wb.Worksheets("Projects").Range("BO" & LastRow).PasteSpecial Paste:=xlPasteValues
ImportWorkbook.Worksheets("Projects").Range("V7", "V" & LastRow).Copy
wb.Worksheets("Projects").Range("BP" & LastRow).PasteSpecial Paste:=xlPasteValues
ImportWorkbook.Worksheets("Projects").Range("W7", "W" & LastRow).Copy
wb.Worksheets("Projects").Range("BQ" & LastRow).PasteSpecial Paste:=xlPasteValues
ImportWorkbook.Worksheets("Projects").Range("X7", "X" & LastRow).Copy
wb.Worksheets("Projects").Range("BR" & LastRow).PasteSpecial Paste:=xlPasteValues
ImportWorkbook.Worksheets("Projects").Range("Y7", "Y" & LastRow).Copy
wb.Worksheets("Projects").Range("BS" & LastRow).PasteSpecial Paste:=xlPasteValues
ImportWorkbook.Worksheets("Projects").Range("Z7", "Z" & LastRow).Copy
wb.Worksheets("Projects").Range("BT" & LastRow).PasteSpecial Paste:=xlPasteValues
ImportWorkbook.Worksheets("Projects").Range("AA7", "AA" & LastRow).Copy
wb.Worksheets("Projects").Range("BU" & LastRow).PasteSpecial Paste:=xlPasteValues
ImportWorkbook.Worksheets("Projects").Range("AB7", "AB" & LastRow).Copy
wb.Worksheets("Projects").Range("BV" & LastRow).PasteSpecial Paste:=xlPasteValues
End Sub
Try something like this:
Private Sub Btn_load_data_file_Click()
Dim FileLocation As String
Dim LastRow As Long, wsPaste As Worksheet, curr_lrow As Long
Dim wb As Workbook, ImportWorkbook As Workbook, wsImport As Worksheet
FileLocation = Application.GetOpenFilename
If FileLocation = "False" Then
Beep
Exit Sub
End If
Set wb = ActiveWorkbook
Set wsPaste = wb.Worksheets("Projects")
Application.ScreenUpdating = False
Set ImportWorkbook = Workbooks.Open(Filename:=FileLocation)
Set wsImport = ImportWorkbook.Worksheets("Projects")
LastRow = wsImport.Cells(Rows.Count, "A").End(xlUp).Row + 1 'safer than .End(xlDown)...
curr_lrow = wsPaste.Cells(Rows.Count, "A").End(xlUp).Row + 1
CopyValues wsImport.Range("B7:B" & LastRow), wsPaste.Range("A" & curr_lrow)
CopyValues wsImport.Range("C7:C" & LastRow), wsPaste.Range("C" & curr_lrow)
ImportWorkbook.Close False
End Sub
'Copy values from `rngFrom` to `rngTo`
Sub CopyValues(rngFrom As Range, rngTo As Range)
With rngFrom
rngTo.Cells(1).Resize(.Rows.Count, .Columns.Count).Value = .Value
End With
End Sub

How to get sure that i just copy & paste values and no format?

Good morning,
I tried with ActiveCell.PasteSpecial Paste:=xlPasteValuesbut it doesnt work.
Sub CopyCoverage()
Dim x As Worksheet, y As Worksheet, LastRow
Set x = Sheets("1SalesAnalysis")
Set y = Sheets("Basics")
LastRow = x.Cells.SpecialCells(xlCellTypeLastCell).Row
x.Range("A2:A" & LastRow).Copy y.Cells(Rows.Count, "E").End(xlUp).Offset(1, 0)
x.Range("B2:B" & LastRow).Copy y.Cells(Rows.Count, "F").End(xlUp).Offset(1, 0)
x.Range("C2:C" & LastRow).Copy y.Cells(Rows.Count, "G").End(xlUp).Offset(1, 0)
x.Range("D2:D" & LastRow).Copy y.Cells(Rows.Count, "L").End(xlUp).Offset(1, 0)
x.Range("E2:E" & LastRow).Copy y.Cells(Rows.Count, "M").End(xlUp).Offset(1, 0)
x.Range("F2:F" & LastRow).Copy y.Cells(Rows.Count, "P").End(xlUp).Offset(1, 0)
x.Range("G2:G" & LastRow).Copy y.Cells(Rows.Count, "Q").End(xlUp).Offset(1, 0)
x.Range("H2:H" & LastRow).Copy y.Cells(Rows.Count, "R").End(xlUp).Offset(1, 0)
x.Range("I2:I" & LastRow).Copy y.Cells(Rows.Count, "S").End(xlUp).Offset(1, 0)
x.Range("J2:J" & LastRow).Copy y.Cells(Rows.Count, "T").End(xlUp).Offset(1, 0)
x.Range("K2:K" & LastRow).Copy y.Cells(Rows.Count, "V").End(xlUp).Offset(1, 0)
x.Range("L2:L" & LastRow).Copy y.Cells(Rows.Count, "W").End(xlUp).Offset(1, 0)
x.Range("O2:O" & LastRow).Copy y.Cells(Rows.Count, "EA").End(xlUp).Offset(1, 0)
x.Range("P2:P" & LastRow).Copy y.Cells(Rows.Count, "EI").End(xlUp).Offset(1, 0)
x.Range("Q2:Q" & LastRow).Copy y.Cells(Rows.Count, "EB").End(xlUp).Offset(1, 0)
x.Range("R2:R" & LastRow).Copy y.Cells(Rows.Count, "EJ").End(xlUp).Offset(1, 0)
x.Range("S2:S" & LastRow).Copy y.Cells(Rows.Count, "EC").End(xlUp).Offset(1, 0)
x.Range("T2:T" & LastRow).Copy y.Cells(Rows.Count, "EK").End(xlUp).Offset(1, 0)
ActiveCell.PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
End Sub
Thanks
Best regards
Store the mapping rules in an array so you can reuse the same code for each column.
Option Explicit
Sub CopyCoverage()
Dim wsX As Worksheet, wsY As Worksheet
Dim LastRowX As Long, msg As String
Dim rngX As Range, rngY As Range
Set wsX = Sheets("1SalesAnalysis")
Set wsY = Sheets("Basics")
LastRowX = wsX.Cells.SpecialCells(xlCellTypeLastCell).Row
Dim map, ar, i As Integer
map = Split("A=>E,B=>F,C=>G,D=>L,E=>M,F=>P,G=>Q,H=>R,I=>S,J=>T,K=>V,L=>W," & _
"O=>EA,P=>EI,Q=>EB,R=>EJ,S=>EC,T=>EK", ",")
Application.ScreenUpdating = False
For i = 0 To UBound(map)
ar = Split(map(i), "=>")
msg = msg & vbLf & ar(0) & " to " & ar(1)
Set rngX = wsX.Range(ar(0) & "2:" & ar(0) & LastRowX)
Set rngY = wsY.Cells(Rows.Count, ar(1)).End(xlUp).Offset(1, 0)
rngY.Resize(rngX.Rows.Count).Value2 = rngX.Value2
Next
Application.ScreenUpdating = True
MsgBox "Copied " & msg, vbInformation
End Sub
The best way to avoid formatting not being copied/pasted is by not copying/pasting in the first place: you can simply do:
Destination_Range.Value = Source_Range.Value
Like this, only the value gets copied", but the formatting is not involved.
More information can be found in this reference question about this subject.
Those one-line 'copy-pastes' already finishes the task of copy-paste, so the ActiveCell.PasteSpecial at the bottom part of your code doesn't do anything.
There are several ways to do it but I will stick to the pattern of your code:
Sub CopyCoverage()
Dim x As Worksheet
Dim y As Worksheet
Dim LastRow As Long
Set x = ThisWorkbook.Sheets("Sheet2")
Set y = ThisWorkbook.Sheets("Ans")
LastRow = x.Cells.SpecialCells(xlCellTypeLastCell).Row
Application.ScreenUpdating = False '~turn off the 'animation' to speed up a bit
'The logic will be, copy-paste, copy-paste
x.Range("A2:A" & LastRow).Copy
y.Cells(Rows.Count, "E").End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValues
x.Range("B2:B" & LastRow).Copy
y.Cells(Rows.Count, "F").End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValues
'and so and so forth
'Just continue with this pattern
Application.CutCopyMode = False '~end line
Application.ScreenUpdating = True '~turn on the 'animation' again
End Sub

I was wondering if my code could be simpelfied

I've created some code to let users fill in a excelsheet with 4 tables. by pressing a button it copies the table data to another workbook. it has to find the last row filled and copy the data below that. Some cells won't get filled so i used .Filldown to get the empty cells filled. In order to make the .Filldown work i had to count the rows in the tables.
It all works but I'm new in VBA so i was wondering if my code can be simpelfied. It looks like a lot of code.
Private Sub CommandButton5_Click()
Dim PassWord As Variant
PassWord = InputBox("Wachtwoord?")
'PassWord = "Something"
If PassWord = "Something" Then
Dim nT1 As Integer
Dim nT2 As Integer
Dim nT3 As Integer
Dim nT4 As Integer
If Sheets("Variabelen").Range("H2") = 0 Then
Set Z = ActiveWorkbook.Sheets(1)
Set T1 = ActiveSheet.ListObjects("Tabel1").DataBodyRange
Set T1C = ActiveSheet.ListObjects("Tabel1")
Set T2 = ActiveSheet.ListObjects("Tabel2").DataBodyRange
Set T2C = ActiveSheet.ListObjects("Tabel2")
Set T3 = ActiveSheet.ListObjects("Tabel3").DataBodyRange
Set T3C = ActiveSheet.ListObjects("Tabel3")
Set T4 = ActiveSheet.ListObjects("Tabel4").DataBodyRange
Set T4C = ActiveSheet.ListObjects("Tabel4")
nT1 = T1C.Range.Rows.Count - 1
nT2 = T2C.Range.Rows.Count - 1
nT3 = T3C.Range.Rows.Count - 1
nT4 = T4C.Range.Rows.Count - 1
'Test_ verwijderen als bestand actief wordt
Set Y = Workbooks.Open("\\Somewhere\Test_Masterbestand Afdeling.xlsx")
'Huidige medewerker in opleiding (T1)
lRow = Y.Worksheets("Data").Cells(Y.Worksheets("Data").Rows.Count, 1).End(xlUp).Row
lRow = lRow + 1
T1.Copy
Y.Worksheets("Data").Range("A" & lRow).PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
Y.Sheets("Data").Range("I" & lRow).Value = "Huidige medewerker in opleiding"
Y.Sheets("Data").Range("J" & lRow).Value = Z.Range("C3").Value
Y.Sheets("Data").Range("K" & lRow).Value = Z.Range("C4").Value
Y.Sheets("Data").Range("L" & lRow).Value = Z.Range("C6").Value
Dim LastRowA As Long
Dim LastRowB As Long
Dim LastRowC As Long
Dim LastRowD As Long
Dim LastRowE As Long
LastRowA = ActiveSheet.Range("A:A").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
LastRowB = ActiveSheet.Range("I:I").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
LastRowC = ActiveSheet.Range("J:J").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
LastRowD = ActiveSheet.Range("K:K").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
LastRowE = ActiveSheet.Range("L:L").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
CopyrangeB = "I" & LastRowB & ":" & "I" & LastRowA
CopyrangeC = "J" & LastRowC & ":" & "J" & LastRowA
CopyrangeD = "K" & LastRowD & ":" & "K" & LastRowA
CopyrangeE = "L" & LastRowE & ":" & "L" & LastRowA
If nT1 > 1 Then
ActiveSheet.Range(CopyrangeB).FillDown
ActiveSheet.Range(CopyrangeC).FillDown
ActiveSheet.Range(CopyrangeD).FillDown
ActiveSheet.Range(CopyrangeE).FillDown
End If
'Nieuwe instroom in opleiding (T2)
lRow = Y.Worksheets("Data").Cells(Y.Worksheets("Data").Rows.Count, 1).End(xlUp).Row
lRow = lRow + 1
T2.Copy
Y.Worksheets("Data").Range("A" & lRow).PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
Y.Sheets("Data").Range("I" & lRow).Value = "Nieuwe instroom in opleiding"
Y.Sheets("Data").Range("J" & lRow).Value = Z.Range("C3").Value
Y.Sheets("Data").Range("K" & lRow).Value = Z.Range("C4").Value
Y.Sheets("Data").Range("L" & lRow).Value = Z.Range("C6").Value
Dim LastRowF As Long
Dim LastRowG As Long
Dim LastRowH As Long
Dim LastRowI As Long
Dim LastRowJ As Long
LastRowF = ActiveSheet.Range("A:A").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
LastRowG = ActiveSheet.Range("I:I").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
LastRowH = ActiveSheet.Range("J:J").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
LastRowI = ActiveSheet.Range("K:K").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
LastRowJ = ActiveSheet.Range("L:L").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
CopyrangeG = "I" & LastRowG & ":" & "I" & LastRowF
CopyrangeH = "J" & LastRowH & ":" & "J" & LastRowF
CopyrangeI = "K" & LastRowI & ":" & "K" & LastRowF
CopyrangeJ = "L" & LastRowJ & ":" & "L" & LastRowF
If nT2 > 1 Then
ActiveSheet.Range(CopyrangeG).FillDown
ActiveSheet.Range(CopyrangeH).FillDown
ActiveSheet.Range(CopyrangeI).FillDown
ActiveSheet.Range(CopyrangeJ).FillDown
End If
'Afdelingspecifiek(T3)
lRow = Y.Worksheets("Data").Cells(Y.Worksheets("Data").Rows.Count, 1).End(xlUp).Row
lRow = lRow + 1
T3.Copy
Y.Worksheets("Data").Range("A" & lRow).PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
Y.Sheets("Data").Range("I" & lRow).Value = "Afdelingspecifiek"
Y.Sheets("Data").Range("J" & lRow).Value = Z.Range("C3").Value
Y.Sheets("Data").Range("K" & lRow).Value = Z.Range("C4").Value
Y.Sheets("Data").Range("L" & lRow).Value = Z.Range("C6").Value
Dim LastRowK As Long
Dim LastRowL As Long
Dim LastRowM As Long
Dim LastRowN As Long
Dim LastRowO As Long
LastRowK = ActiveSheet.Range("A:A").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
LastRowL = ActiveSheet.Range("I:I").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
LastRowM = ActiveSheet.Range("J:J").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
LastRowN = ActiveSheet.Range("K:K").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
LastRowO = ActiveSheet.Range("L:L").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
CopyrangeL = "I" & LastRowL & ":" & "I" & LastRowK
CopyrangeM = "J" & LastRowM & ":" & "J" & LastRowK
CopyrangeN = "K" & LastRowN & ":" & "K" & LastRowK
CopyrangeO = "L" & LastRowO & ":" & "L" & LastRowK
If nT3 > 1 Then
ActiveSheet.Range(CopyrangeL).FillDown
ActiveSheet.Range(CopyrangeM).FillDown
ActiveSheet.Range(CopyrangeN).FillDown
ActiveSheet.Range(CopyrangeO).FillDown
End If
'Individueel (T4)
lRow = Y.Worksheets("Data").Cells(Y.Worksheets("Data").Rows.Count, 1).End(xlUp).Row
lRow = lRow + 1
T4.Copy
Y.Worksheets("Data").Range("A" & lRow).PasteSpecial Paste:=xlPasteValues
Y.Sheets("Data").Range("I" & lRow).Value = "Individueel"
Y.Sheets("Data").Range("J" & lRow).Value = Z.Range("C3").Value
Y.Sheets("Data").Range("K" & lRow).Value = Z.Range("C4").Value
Y.Sheets("Data").Range("L" & lRow).Value = Z.Range("C6").Value
Application.CutCopyMode = False
Dim LastRowP As Long
Dim LastRowQ As Long
Dim LastRowR As Long
Dim LastRowS As Long
Dim LastRowT As Long
LastRowP = ActiveSheet.Range("A:A").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
LastRowQ = ActiveSheet.Range("I:I").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
LastRowR = ActiveSheet.Range("J:J").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
LastRowS = ActiveSheet.Range("K:K").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
LastRowT = ActiveSheet.Range("L:L").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
LastRowU = ActiveSheet.Range("N:N").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
CopyrangeQ = "I" & LastRowQ & ":" & "I" & LastRowP
CopyrangeR = "J" & LastRowR & ":" & "J" & LastRowP
CopyrangeS = "K" & LastRowS & ":" & "K" & LastRowP
CopyrangeT = "L" & LastRowT & ":" & "L" & LastRowP
'Formule in Kolom M
CopyrangeU = "N" & LastRowU & ":" & "N" & LastRowP
If nT4 > 1 Then
ActiveSheet.Range(CopyrangeQ).FillDown
ActiveSheet.Range(CopyrangeR).FillDown
ActiveSheet.Range(CopyrangeS).FillDown
ActiveSheet.Range(CopyrangeT).FillDown
ActiveSheet.Range(CopyrangeU).FillDown
End If
' Y.Close (True)
'Quote weghalen bij opleveren
'Sheets("Variabelen").Range("H2").Value = Sheets("Variabelen").Range("H2").Value + 1
Else
MsgBox ("Niet nog een keer Sylvia!!!!")
End If
Else
'Do nothing
End If
End Sub
None

Copy multiple columns (in different order) from a source workbook to a destination workbook and paste it below the last non empty row

I have two different work books named Input.xlsb (Source data) and Lapsed Pipeline.xlsm (destination workbook). I have searched the codes here and found one which helped me partially but the issue with this code is the data of one column gets pasted below the other . Eg:Column D gets pasted correctly in last non empty cell, but data in column gets pasted in the last row after data in column and like wise for every columns, i want all the data from the source data to be pasted after the last non empty row at once. below is code i have reedited for my purpose.
Example:
Sub CopyCoverage()
Dim x As Worksheet, y As Worksheet, LastRow&
Set x = Workbooks("Input.xlsb").Worksheets("Opportunity")
Set y = ThisWorkbook.Worksheets("Lapsed Opps")
LastRow = x.Cells.SpecialCells(xlCellTypeLastCell).Row
x.Range("G2:G" & LastRow).Copy y.Cells(Rows.Count, "D").End(xlUp).Offset(1, 0)
x.Range("I2:I" & LastRow).Copy y.Cells(Rows.Count, "M").End(xlUp).Offset(1, 0)
x.Range("P2:P" & LastRow).Copy y.Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
x.Range("Y2:Y" & LastRow).Copy y.Cells(Rows.Count, "C").End(xlUp).Offset(1, 0)
x.Range("Z2:Z" & LastRow).Copy y.Cells(Rows.Count, "B").End(xlUp).Offset(1, 0)
x.Range("AJ2:AJ" & LastRow).Copy y.Cells(Rows.Count, "G").End(xlUp).Offset(1, 0)
x.Range("AK2:AK" & LastRow).Copy y.Cells(Rows.Count, "H").End(xlUp).Offset(1, 0)
x.Range("AL2:AL" & LastRow).Copy y.Cells(Rows.Count, "I").End(xlUp).Offset(1, 0)
x.Range("AM2:AM" & LastRow).Copy y.Cells(Rows.Count, "J").End(xlUp).Offset(1, 0)
x.Range("EC2:EC" & LastRow).Copy y.Cells(Rows.Count, "F").End(xlUp).Offset(1, 0)
x.Range("EG2:EG" & LastRow).Copy y.Cells(Rows.Count, "E").End(xlUp).Offset(1, 0)
Application.CutCopyMode = False
End Sub
How about the following, if I understood correctly:
Sub CopyCoverage()
Dim x As Worksheet, y As Worksheet, LastRow&, yLastRow&
Set x = Workbooks("Input.xlsb").Worksheets("Opportunity")
Set y = ThisWorkbook.Worksheets("Lapsed Opps")
LastRow = x.Cells.SpecialCells(xlCellTypeLastCell).Row
yLastRow = y.Cells(y.Rows.Count, "A").End(xlUp).Row + 1
x.Range("G2:G" & LastRow).Copy y.Cells(yLastRow, "D").End(xlUp)
x.Range("I2:I" & LastRow).Copy y.Cells(yLastRow, "M").End(xlUp)
x.Range("P2:P" & LastRow).Copy y.Cells(yLastRow, "A").End(xlUp)
x.Range("Y2:Y" & LastRow).Copy y.Cells(yLastRow, "C").End(xlUp)
x.Range("Z2:Z" & LastRow).Copy y.Cells(yLastRow, "B").End(xlUp)
x.Range("AJ2:AJ" & LastRow).Copy y.Cells(yLastRow, "G").End(xlUp)
x.Range("AK2:AK" & LastRow).Copy y.Cells(yLastRow, "H").End(xlUp)
x.Range("AL2:AL" & LastRow).Copy y.Cells(yLastRow, "I").End(xlUp)
x.Range("AM2:AM" & LastRow).Copy y.Cells(yLastRow, "J").End(xlUp)
x.Range("EC2:EC" & LastRow).Copy y.Cells(yLastRow, "F").End(xlUp)
x.Range("EG2:EG" & LastRow).Copy y.Cells(yLastRow, "E").End(xlUp)
Application.CutCopyMode = False
End Sub
Instead of using Rows.Count and End(xlUp), store the bottom row of the UsedRange in a variable before you start copying:
Dim PasteRow AS Long
PasteRow = y.UsedRange.Rows(y.UsedRange.Rows.Count).Row + 1
x.Range("G2:G" & LastRow).Copy y.Cells(PasteRow, 4) 'Do not change PasteRow
x.Range("I2:I" & LastRow).Copy y.Cells(PasteRow, 13) 'et cetera
{EDIT} Longer code, removes the requirement for UsedRange, still accepts that some columns may have blank cells:
'This replaces your Copy block - everything before stays as you wrote it
Dim PasteRow As Long, iCheckCol AS Integer
PasteRow = 0
For iCheckCol = 1 to 10 'Check columns A - J
If y.Cells(y.Rows.Count, iCheckCol).End(xlUp).Row > PasteRow Then
PasteRow = y.Cells(y.Rows.Count, iCheckCol).End(xlUp).Row 'Find lowest bottom of rows
End If
Next iCheckCol
PasteRow = PasteRow+1 'Go down from the Bottom Row
x.Range("G2:G" & LastRow).Copy y.Cells(PasteRow, 4) 'Do not change PasteRow
x.Range("I2:I" & LastRow).Copy y.Cells(PasteRow, 13) 'et cetera
'Add a line for every column that you want to copy

How to match data between columns to do the comparasion

I do not really know how to explain this in a clear manner. Please see attached image
I have a table with 4 different columns, 2 are identical to each other (NAME and QTY). The goal is to compare the differences between the QTY, however, in order to do it. I must:
1. sort the data
2. match the data item by item
This is not a big deal with small table but with 10 thousand rows, it takes me a few days to do it.
Pleas help me, I appreciate.
My logic is:
1. Sorted the first two columns (NAME and QTY)
2. For each value of second two columns (NAME and QTY), check if it match with first two column. If true, the insert the value.
3. For values are not matched, insert to new rows with offset from the rows that are in first two columns but not in second two columns
Is this what you are trying?
Option Explicit
Sub Sample()
Dim ws As Worksheet
Dim lastRow As Long, i As Long, newRow As Long
Dim aCell As Range, SrchRange As Range
Set ws = Sheets("Sheet1")
With ws
.Columns("A:B").Copy .Columns("G:G")
.Columns("G:H").Sort Key1:=.Range("G2"), Order1:=xlAscending, Header:=xlYes, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
lastRow = .Range("G" & Rows.Count).End(xlUp).Row
newRow = lastRow
Set SrchRange = .Range("G2:G" & lastRow)
lastRow = .Range("C" & Rows.Count).End(xlUp).Row
.Range("I1").Value = "NAME": .Range("J1").Value = "QTY"
For i = 2 To lastRow
If Len(Trim(.Range("C" & i).Value)) <> 0 Then
Set aCell = SrchRange.Find(What:=.Range("C" & i).Value, LookIn:=xlFormulas, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not aCell Is Nothing Then
.Range("I" & aCell.Row).Value = .Range("C" & i).Value
.Range("J" & aCell.Row).Value = .Range("D" & i).Value
Else
newRow = newRow + 1
.Range("I" & newRow).Value = .Range("C" & i).Value
.Range("J" & newRow).Value = .Range("D" & i).Value
End If
End If
Next
End With
End Sub
SNAPSHOT
Based on your above requirements, the logic totally changes and hence I am posting it as a different answer.
Also in your "This is Wonderful" snapshot above, there is a slight error. As per logic SAMPLE10 cannot come above SAMPLE11. It has to come after SAMPLE11.
See the below snapshot
And here is the code :)
Option Explicit
Sub sAMPLE()
Dim ws As Worksheet
Dim lastRow As Long, i As Long, newRow As Long, rw As Long
Dim aCell As Range, SrchRange As Range
Set ws = Sheets("Sheet1")
With ws
.Columns("A:B").Copy .Columns("G:G")
.Columns("G:H").Sort key1:=.Range("G2"), Order1:=xlAscending, Header:=xlYes, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
.Columns("H:H").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
lastRow = .Range("G" & Rows.Count).End(xlUp).Row
For i = 2 To lastRow
.Range("H" & i).Value = GetLastNumbers(.Range("G" & i).Value)
If .Range("H" & i).Value <> 0 Then
.Range("G" & i).Value = Left(.Range("G" & i).Value, _
Len(.Range("G" & i).Value) - Len(.Range("H" & i).Value))
End If
Next i
.Columns("G:H").Sort key1:=.Range("H2"), Order1:=xlAscending, Header:=xlYes, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
For i = 2 To lastRow
If .Range("H" & i).Value <> 0 Then _
.Range("G" & i).Value = .Range("G" & i).Value & .Range("H" & i).Value
Next i
.Columns("H:H").Delete
newRow = lastRow
Set SrchRange = .Range("G2:G" & lastRow)
lastRow = .Range("C" & Rows.Count).End(xlUp).Row
.Range("I1").Value = "NAME": .Range("J1").Value = "QTY"
For i = 2 To lastRow
If Len(Trim(.Range("C" & i).Value)) <> 0 Then
Set aCell = SrchRange.Find(What:=.Range("C" & i).Value, LookIn:=xlFormulas, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not aCell Is Nothing Then
.Range("I" & aCell.Row).Value = .Range("C" & i).Value
.Range("J" & aCell.Row).Value = Application.Evaluate("=SUMPRODUCT((C2:C" & lastRow _
& "=" & """" & .Range("C" & i).Value & """" & ")*(D2:D" & lastRow & "))")
Else
newRow = newRow + 1
.Range("I" & newRow).Value = .Range("C" & i).Value
.Range("J" & newRow).Value = .Range("D" & i).Value
End If
End If
Next
lastRow = .Range("G" & Rows.Count).End(xlUp).Row
For i = lastRow To 2 Step -1
If .Range("G" & i).Value = .Range("G" & i - 1).Value Then
.Range("H" & i - 1).Value = .Range("H" & i).Value + .Range("H" & i - 1).Value
If Application.WorksheetFunction.CountA(.Range("I" & i & ":J" & i)) = 0 Then
.Range("G" & i & ":J" & i).Delete Shift:=xlUp
Else
.Range("G" & i & ":H" & i).Delete Shift:=xlUp
End If
End If
Next i
lastRow = .Range("I" & Rows.Count).End(xlUp).Row
newRow = .Range("G" & Rows.Count).End(xlUp).Row
If lastRow <= newRow Then Exit Sub
.Range("I" & newRow & ":J" & lastRow).Sort key1:=.Range("I" & newRow), Order1:=xlAscending, Header:=xlYes, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
For i = lastRow To newRow Step -1
If .Range("I" & i).Value = .Range("I" & i - 1).Value Then
.Range("J" & i - 1).Value = .Range("J" & i).Value + .Range("J" & i - 1).Value
.Range("I" & i & ":J" & i).Delete Shift:=xlUp
End If
Next i
End With
End Sub
Function GetLastNumbers(strVal As String) As Long
Dim j As Long, strTemp As String
For j = Len(strVal) To 1 Step -1
If Not IsNumeric(Mid(strVal, j, 1)) Then Exit For
strTemp = Mid(strVal, j, 1) & strTemp
Next j
GetLastNumbers = Val(Trim(strTemp))
End Function

Resources