Data combination - VBA requires modification

Status
Not open for further replies.

Aberdham

Board Regular
Joined
Mar 8, 2018
Messages
163
Office Version
  1. 365
Platform
  1. Windows
Hi Excel masters,

I have tested the below code on my workbook:

Option Explicit
Public Sub CombineDataFromAllSheets()

Dim wksSrc As Worksheet, wksDst As Worksheet
Dim rngSrc As Range, rngDst As Range
Dim lngLastCol As Long, lngSrcLastRow As Long, lngDstLastRow As Long

'Notes: "Src" is short for "Source", "Dst" is short for "Destination"

'Set references up-front
Set wksDst = ThisWorkbook.Worksheets("overview machine 1")
lngDstLastRow = LastOccupiedRowNum(wksDst)
lngLastCol = LastOccupiedColNum(wksDst)

'Set the initial destination range
Set rngDst = wksDst.Cells(lngDstLastRow + 1, 1)

'Loop through all sheets
For Each wksSrc In ThisWorkbook.Worksheets

'Make sure we skip the "overview machine 1" destination sheet!
If wksSrc.Name <> "Import" Then

'Identify the last occupied row on this sheet
lngSrcLastRow = LastOccupiedRowNum(wksSrc)

'Store the source data then copy it to the destination range
With wksSrc
Set rngSrc = .Range(.Cells(2, 1), .Cells(lngSrcLastRow, lngLastCol))
rngSrc.Copy Destination:=rngDst
End With

'Redefine the destination range now that new data has been added
lngDstLastRow = LastOccupiedRowNum(wksDst)
Set rngDst = wksDst.Cells(lngDstLastRow + 1, 1)

End If

Next wksSrc

End Sub

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'INPUT : Sheet, the worksheet we'll search to find the last row
'OUTPUT : Long, the last occupied row
'SPECIAL CASE: if Sheet is empty, return 1
Public Function LastOccupiedRowNum(Sheet As Worksheet) As Long
Dim lng As Long
If Application.WorksheetFunction.CountA(Sheet.Cells) <> 0 Then
With Sheet
lng = .Cells.Find(What:="*", _
After:=.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
End With
Else
lng = 1
End If
LastOccupiedRowNum = lng
End Function

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'INPUT : Sheet, the worksheet we'll search to find the last column
'OUTPUT : Long, the last occupied column
'SPECIAL CASE: if Sheet is empty, return 1
Public Function LastOccupiedColNum(Sheet As Worksheet) As Long
Dim lng As Long
If Application.WorksheetFunction.CountA(Sheet.Cells) <> 0 Then
With Sheet
lng = .Cells.Find(What:="*", _
After:=.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Column
End With
Else
lng = 1
End If
LastOccupiedColNum = lng
End Function
And here are the output data:

there are altogether 120 of machine, I will demonstrate below the 3 output sheets of the respective machine:

sheet 1 (historical cost) would look like:
Invoice typeInvoice NumberSupplier/DebitorDescriptionInvoice DateFX rateUSD AmountEUR AmountChange in InventoryMachineryCategory
AK8554214ADADeposit SN 84445101/12/20171,1885$361.067,54303.801,05 €-303.801,05 €1
AK8554215ADAfinal payment_ESN 84846201/01/20181,1993$358.718,75299.106,77 €-299.106,77 €1
AK8554216APOMfinal payment_ESN 84846202/02/20181,2492$ 2.600,602.081,82 €-2.081,82 €1
A85426589APOMinspection02/02/20181,2492$ 3.461,332.770,84 €-2.770,84 €1
A85426589UIJopmen02/02/20181,2492$ 18.988,9415.200,88 €-15.200,88 €1

<tbody>
</tbody>



Sheet 2 (sales)

Type of invoiceInvoice NumberSupplier/DebitorDescriptionInvoice DateFX rateUSD AmountEUR AmountChange in InventoryMachineryCategory
RAR00214522ADAAR0025145211/04/20181,2384$ 15.222,00€12.291,67-12.291,67 €1

<tbody>
</tbody>



sheet 3( purchase)

Type of invoiceInvoice NumberSupplier/DebitorDescriptionInvoice DateFX rateUSD AmountEUR AmountChange in InventoryMachineryCategory
P58485AADAP00152311/04/20181,2384$ 15.222,00€12.291,6712.291,67 €1
P584885AARAP00152401/04/20181,2321$ 1.600,00€1.298,601.298,60 €1
P584882AAEAP00152501/04/20181,2321$ 500,00€405,81405,81 €1
P48595AESAP00152601/04/20181,2321$ 18.455,00€14.978,4914.978,49 €1
P485953AHJAP00152701/04/20181,2321$ 16.746,00€13.591,4313.591,43 €1
P1007019UIJAP00152801/04/20181,2321$ 6.200,00€5.050,515.050,51 €1
P1007020JIMKAP00152901/04/20181,2321$ 35.000,00€28.434,4828.434,48 €1
P8958952KYTAP00153001/04/20181,2321$2.000.000,00€1.617.992,071.617.992,07 €1

<tbody>
</tbody>



I would like to have them transfer to a new sheet named overview Machine 1 (drop down list or pivot table)

And when I run the code, only column A were able to combine, all the other columns are left blank.

Could you please help me with this code and to get the job done?

Thank you in advance.
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Status
Not open for further replies.

Forum statistics

Threads
1,214,653
Messages
6,120,751
Members
448,989
Latest member
mariah3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top