Copy from multi sheets to one sheets

KlausW

Active Member
Joined
Sep 9, 2020
Messages
458
Office Version
  1. 2016
Platform
  1. Windows
Hi everyone.

I have a little challenge, I want a VBA code that can copy all data from column A2 to J2 and down after, only data. From 12 different sheets and past the data into a single sheet " Bestilling ", starting in cell K9 to R9 and down after. When running VBA code, it should not delete anything in sheets " Bestilling ".

Someone who can help

All help will be appreciated.

Best regards Klaus W

item ordering

VBA Code:
Sub Rektangelafrundedehjørner4_Klik()

'Procedure to Consolidate all sheets in a workbook

On Error GoTo IfError

'1. Variables declaration

Dim Sht As Worksheet, DstSht As Worksheet

Dim LstRow As Long, LstCol As Long, DstRow As Long

Dim i As Integer, EnRange As String

Dim SrcRng As Range



'2. Disable Screen Updating - stop screen flickering

' And Disable Events to avoid inturupted dialogs / popups

With Application

.ScreenUpdating = False

.EnableEvents = False

End With



' '3. Delete the Consolidate_Data WorkSheet if it exists

' Application.DisplayAlerts = False

' On Error Resume Next

' ActiveWorkbook.Sheets("Consolidate_Data").Delete

' Application.DisplayAlerts = True

'

' '4. Add a new WorkSheet and name as 'Consolidate_Data'

' With ActiveWorkbook

' Set DstSht = .Sheets.Add(After:=.Sheets(.Sheets.Count))

' DstSht.Name = "Consolidate_Data"

' End With



' XXX Add back set statement

Set DstSht = ActiveWorkbook.Sheets("Consolidate_Data")



'5. Loop through each WorkSheet in the workbook and copy the data to the 'Consolidate_Data' WorkSheet

DstRow = 9 ' XXX Klaus wanted the first copy at row 9



For Each Sht In ActiveWorkbook.Worksheets

If Sht.Name <> DstSht.Name Then

'5.2: Find Input data range

LstRow = fn_LastRow(Sht)

LstCol = fn_LastColumn(Sht)

EnRange = Sht.Cells(LstRow, LstCol).Address

Set SrcRng = Sht.Range("a2:" & EnRange)

'5.3: Check whether there are enough rows in the 'Consolidate_Data' Worksheet

If DstRow + SrcRng.Rows.Count > DstSht.Rows.Count Then

MsgBox "There are not enough rows to place the data in the Consolidate_Data worksheet."

GoTo IfError



End If



'5.4: Copy data to the 'consolidated_data' WorkSheet

SrcRng.Copy Destination:=DstSht.Range("k" & DstRow + 1)



'5.1: Find the last row on the 'Consolidate_Data' sheet for the next copy

'Moved to end of loop not required for 1st pass

DstRow = fn_LastRow(DstSht)

End If



Next



'DstSht.Range("A1") = "You can place the headeing in the first row"



IfError:



'6. Enable Screen Updating and Events

With Application

.ScreenUpdating = True

.EnableEvents = True

End With



End Sub



'In this example we are finding the last column of specified Sheet

Function fn_LastColumn(ByVal Sht As Worksheet)



'Dim lastCol As Long

Dim lCol As Long

lCol = Sht.Cells.SpecialCells(xlLastCell).Column

lCol = Sht.Cells.SpecialCells(xlLastCell).Column

Do While Application.CountA(Sht.Columns(lCol)) = 0 And lCol <> 1

lCol = lCol - 1

Loop

fn_LastColumn = lCol



End Function



'In this example we are finding the last Row of specified Sheet

'In this example we are finding the last Row of specified Sheet

Function fn_LastRow(ByVal Sht As Worksheet)



'Dim lastRow As Long

Dim lRow As Long



lRow = Sht.Cells.SpecialCells(xlLastCell).Row

lRow = Sht.Cells.SpecialCells(xlLastCell).Row

Do While Application.CountA(Sht.Rows(lRow)) = 0 And lRow <> 1

lRow = lRow - 1

Loop

fn_LastRow = lRow



End Function
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Please advise on whether Alan's suggestion resolves your issue. If not then please advise, what the code you are using is or is not doing and provide examples.
 
Upvote 0
Hi so I am back, after trying with the PQ, but I cannot get it to work with PQ.
I want a VBA code that can copy all data from column A2 to J2 and down after, only data, not blank cells. From 12 different sheets and past the data into a single sheet "Bestilling", starting in cell K9 to R9 and down after. When running VBA code, it should not delete anything in sheets "Bestilling".
I have an example in this link. item ordering 06-11-2021


I use and run this VBA code when I push the yellow bottom “Opsamling” in sheets "Bestilling".
it copies as described above but puts it a little over it all in sheets "Bestilling".
Orange bottom” Slet” is to erase all in K9 to R9 and down.
Every text is in Danish.

All help will be appreciated

Best regards
Klaus W
VBA Code:
Sub Rektangelafrundedehjørner4_Klik()

'Procedure to Consolidate all sheets in a workbook

On Error GoTo IfError

'1. Variables declaration

Dim Sht As Worksheet, DstSht As Worksheet

Dim LstRow As Long, LstCol As Long, DstRow As Long

Dim i As Integer, EnRange As String

Dim SrcRng As Range

'2. Disable Screen Updating - stop screen flickering

' And Disable Events to avoid inturupted dialogs / popups

With Application

.ScreenUpdating = False

.EnableEvents = False

End With

' '3. Delete the Consolidate_Data WorkSheet if it exists

' Application.DisplayAlerts = False

' On Error Resume Next

' ActiveWorkbook.Sheets("Consolidate_Data").Delete

' Application.DisplayAlerts = True

' '4. Add a new WorkSheet and name as 'Consolidate_Data'

' With ActiveWorkbook

' Set DstSht = .Sheets.Add(After:=.Sheets(.Sheets.Count))

' DstSht.Name = "Consolidate_Data"

' End With

' XXX Add back set statement

Set DstSht = ActiveWorkbook.Sheets("Bestilling")

'5. Loop through each WorkSheet in the workbook and copy the data to the 'Consolidate_Data' WorkSheet

DstRow = 9 ' XXX Klaus wanted the first copy at row 9

For Each Sht In ActiveWorkbook.Worksheets

If Sht.Name <> DstSht.Name Then

'5.2: Find Input data range

LstRow = fn_LastRow(Sht)

LstCol = fn_LastColumn(Sht)

EnRange = Sht.Cells(LstRow, LstCol).Address

Set SrcRng = Sht.Range("a2:" & EnRange)

'5.3: Check whether there are enough rows in the 'Consolidate_Data' Worksheet

If DstRow + SrcRng.Rows.Count > DstSht.Rows.Count Then

MsgBox "There are not enough rows to place the data in the Consolidate_Data worksheet."

GoTo IfError

End If

'5.4: Copy data to the 'consolidated_data' WorkSheet

SrcRng.Copy Destination:=DstSht.Range("k" & DstRow + 1)

'5.1: Find the last row on the 'Consolidate_Data' sheet for the next copy

'Moved to end of loop not required for 1st pass

DstRow = fn_LastRow(DstSht)

End If

Next

'DstSht.Range("A1") = "You can place the headeing in the first row"

IfError:

'6. Enable Screen Updating and Events

With Application

.ScreenUpdating = True

.EnableEvents = True

End With

End Sub


'In this example we are finding the last column of specified Sheet

Function fn_LastColumn(ByVal Sht As Worksheet)

'Dim lastCol As Long

Dim lCol As Long

lCol = Sht.Cells.SpecialCells(xlLastCell).Column

lCol = Sht.Cells.SpecialCells(xlLastCell).Column

Do While Application.CountA(Sht.Columns(lCol)) = 0 And lCol <> 1

lCol = lCol - 1

Loop

fn_LastColumn = lCol

End Function



'In this example we are finding the last Row of specified Sheet

'In this example we are finding the last Row of specified Sheet

Function fn_LastRow(ByVal Sht As Worksheet)

'Dim lastRow As Long

Dim lRow As Long

lRow = Sht.Cells.SpecialCells(xlLastCell).Row

lRow = Sht.Cells.SpecialCells(xlLastCell).Row

Do While Application.CountA(Sht.Rows(lRow)) = 0 And lRow <> 1

lRow = lRow - 1

Loop

fn_LastRow = lRow

End Function
 
Upvote 0
This part of your post is a little vague:
From 12 different sheets

What 12 sheets?
Now if I try to read your entire script that does not work I may be able to figure it out. But just put that information into your post would help.
 
Upvote 0
And you said:
copy all data from column A2 to J2
And you said:
and past the data into a single sheet "Bestilling", starting in cell K9 to R9

So we copy data from 10 columns of data
But try to paste them into 8 columns
How can we do that?
 
Upvote 0
Hi so I am back, after trying with the PQ, but I cannot get it to work with PQ.
I want a VBA code that can copy all data from column A2 to J2 and down after, only data, not blank cells. From 12 different sheets and past the data into a single sheet "Bestilling", starting in cell K9 to R9 and down after. When running VBA code, it should not delete anything in sheets "Bestilling".
I have an example in this link. item ordering 06-11-2021


I use and run this VBA code when I push the yellow bottom “Opsamling” in sheets "Bestilling".
it copies as described above but puts it a little over it all in sheets "Bestilling".
Orange bottom” Slet” is to erase all in K9 to R9 and down.
Every text is in Danish.

All help will be appreciated

Best regards
Klaus W
VBA Code:
Sub Rektangelafrundedehjørner4_Klik()

'Procedure to Consolidate all sheets in a workbook

On Error GoTo IfError

'1. Variables declaration

Dim Sht As Worksheet, DstSht As Worksheet

Dim LstRow As Long, LstCol As Long, DstRow As Long

Dim i As Integer, EnRange As String

Dim SrcRng As Range

'2. Disable Screen Updating - stop screen flickering

' And Disable Events to avoid inturupted dialogs / popups

With Application

.ScreenUpdating = False

.EnableEvents = False

End With

' '3. Delete the Consolidate_Data WorkSheet if it exists

' Application.DisplayAlerts = False

' On Error Resume Next

' ActiveWorkbook.Sheets("Consolidate_Data").Delete

' Application.DisplayAlerts = True

' '4. Add a new WorkSheet and name as 'Consolidate_Data'

' With ActiveWorkbook

' Set DstSht = .Sheets.Add(After:=.Sheets(.Sheets.Count))

' DstSht.Name = "Consolidate_Data"

' End With

' XXX Add back set statement

Set DstSht = ActiveWorkbook.Sheets("Bestilling")

'5. Loop through each WorkSheet in the workbook and copy the data to the 'Consolidate_Data' WorkSheet

DstRow = 9 ' XXX Klaus wanted the first copy at row 9

For Each Sht In ActiveWorkbook.Worksheets

If Sht.Name <> DstSht.Name Then

'5.2: Find Input data range

LstRow = fn_LastRow(Sht)

LstCol = fn_LastColumn(Sht)

EnRange = Sht.Cells(LstRow, LstCol).Address

Set SrcRng = Sht.Range("a2:" & EnRange)

'5.3: Check whether there are enough rows in the 'Consolidate_Data' Worksheet

If DstRow + SrcRng.Rows.Count > DstSht.Rows.Count Then

MsgBox "There are not enough rows to place the data in the Consolidate_Data worksheet."

GoTo IfError

End If

'5.4: Copy data to the 'consolidated_data' WorkSheet

SrcRng.Copy Destination:=DstSht.Range("k" & DstRow + 1)

'5.1: Find the last row on the 'Consolidate_Data' sheet for the next copy

'Moved to end of loop not required for 1st pass

DstRow = fn_LastRow(DstSht)

End If

Next

'DstSht.Range("A1") = "You can place the headeing in the first row"

IfError:

'6. Enable Screen Updating and Events

With Application

.ScreenUpdating = True

.EnableEvents = True

End With

End Sub


'In this example we are finding the last column of specified Sheet

Function fn_LastColumn(ByVal Sht As Worksheet)

'Dim lastCol As Long

Dim lCol As Long

lCol = Sht.Cells.SpecialCells(xlLastCell).Column

lCol = Sht.Cells.SpecialCells(xlLastCell).Column

Do While Application.CountA(Sht.Columns(lCol)) = 0 And lCol <> 1

lCol = lCol - 1

Loop

fn_LastColumn = lCol

End Function



'In this example we are finding the last Row of specified Sheet

'In this example we are finding the last Row of specified Sheet

Function fn_LastRow(ByVal Sht As Worksheet)

'Dim lastRow As Long

Dim lRow As Long

lRow = Sht.Cells.SpecialCells(xlLastCell).Row

lRow = Sht.Cells.SpecialCells(xlLastCell).Row

Do While Application.CountA(Sht.Rows(lRow)) = 0 And lRow <> 1

lRow = lRow - 1

Loop

fn_LastRow = lRow

End Function
As noted in #7, you'r columns do not fit correctly. The sheets you want to copy from, are different, when it comes to Columns. In some Columns, you have "price" in Column I, and other sheets, it's in column J. So I think you need to correct this first.
 
Upvote 0
And you said:
copy all data from column A2 to J2
And you said:
and past the data into a single sheet "Bestilling", starting in cell K9 to R9

So we copy data from 10 columns of data
But try to paste them into 8 columns
How can we do that?
And you said:
copy all data from column A2 to J2
And you said:
and past the data into a single sheet "Bestilling", starting in cell K9 to R9

So we copy data from 10 columns of data
But try to paste them into 8 columns
How can we do that?
Now I see, I have corrected the error. So it is copy all data from column A2 to I2. and past the data into a single sheet "Bestilling", starting in cell K9 to S9. So it is 9 columns of data to copy, and pasta them to 9 columns, this is the correct solution, without deleting anything in sheet "Bestilling". KW
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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