VBA Loop to go through all sheets. Then Copy a range and Paste it to Main sheet

isanka88

New Member
Joined
Dec 12, 2020
Messages
13
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Hi Good People.

I have an excel workbook with 5 different work sheets. First work sheet is the Master sheet and others are customer data sheets . I want a VBA loop to go through all 4 customer data sheets >> then copy a specific range in each individual sheets >> paste it to master sheet.

However it does not simple as it sounds because data stored in very un structured way. I have attached my workbook below and I have mentioned my expected outcome on it. Kindly check and help me to find a work around for this. Thank you very much.

 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Where does the date come from?
 
Upvote 0
Where does the date come from?
Hey mumps, Thank you very much for coming back. Dates are fixed. Date cells are not changing. I manually enter dates. Master sheet format is fixed
 
Upvote 0
The location (row number) of the dates will depend on the number of rows of data for each customer. If all the dates are entered manually, you would have to check the number of rows of data for each customer. The same is true for the size of the customer labels in column A. Are the headers "Product Name" and "Price" and "Customer" labels in column A also entered manually? Please clarify in detail.
 
Last edited:
Upvote 0
Hi Mumps, Let me share with you. The number of rows of data for each customer are fixed. For an example Customer1 has only 7 rows. Customer2 has only 3 rows. Those are not going to change. Also in the Master sheet, column A is manually entered. Dates, Product names and price heading are also manually entered and fixed locations. I just need those customer's selected fixed range to be copy and pasted in to the master sheet. I have further elaborated my expectation in the below sheet. Kindly have a look.

 
Upvote 0
The dates on the Master Sheet are in merged cells. You should avoid merging cells because they almost always cause problems for macros. Do a little research into "CenterAcrossSelection". This has the same effect as merging without actually merging any cells. Please unmerge the cells in columns C and D containing the dates and then try this macro:
VBA Code:
Sub CopyData()
    Application.ScreenUpdating = False
    Dim ws As Worksheet, desWS As Worksheet, fnd As Range, LastRow As Long, lRow As Long, i As Long: i = 1
    Set desWS = Sheets("Master Sheet")
    LastRow = desWS.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    For Each ws In Sheets
        lRow = Range("C1:C" & LastRow).SpecialCells(xlCellTypeConstants).Areas(i).Cells(1).Row + 2
        If ws.Name <> "Master Sheet" Then
            ws.Range("B2", ws.Range("C" & Rows.Count).End(xlUp)).Copy desWS.Range("C" & lRow)
            i = i + 1
        End If
    Next ws
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
Hi Mumps, You are amazing. This works 100% for my problem. Because of emerald person like you, I was able to replace the manual task which I have been doing for almost 2 years. It is almost out of verbal definitions to say thank you. Love you?
 
Upvote 0
You are very welcome. :)
 
Upvote 0

Forum statistics

Threads
1,214,972
Messages
6,122,530
Members
449,088
Latest member
RandomExceller01

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