Choosing wich sheets to pick while consolidating data using VBA

Doomglazer

New Member
Joined
Mar 17, 2022
Messages
11
Office Version
  1. 365
Platform
  1. Windows
Hello

I have an Excell with a new sheet with data for every working day. The sheets are labeled 0101,0201,0301,.... and this goes on till the end of the year.
I am using the following VBA code to consolidate all the data into a Master sheet. Now im looking for a way where i can say that i only want the data from 0104 to 3004 for example to be on the "Master" sheet

This is the code i use but it captures all the days. But i would like to provide a range where it will then run and provide me the data.

Sub Merge_Sheets()

Dim startRow, startCol, lastRow, lastCol As Long
Dim headers As Range

'Set Master sheet for consolidation
Set mtr = Worksheets("Master")

Set wb = ThisWorkbook
'Get Headers
Set headers = Application.InputBox("Select the Headers", Type:=8)

'Copy Headers into master
headers.Copy mtr.Range("A1")
startRow = headers.Row + 1
startCol = headers.Column

Debug.Print startRow, startCol
'loop through all sheets
For Each ws In wb.Worksheets
'except the master sheet from looping
If ws.Name <> "Master" Then
ws.Activate
lastRow = Cells(Rows.Count, startCol).End(xlUp).Row
lastCol = Cells(startRow, Columns.Count).End(xlToLeft).Column
'get data from each worksheet and copy it into Master sheet
Range(Cells(startRow, startCol), Cells(lastRow, lastCol)).Copy _
mtr.Range("A" & mtr.Cells(Rows.Count, 1).End(xlUp).Row + 1)
End If
Next ws

Worksheets("Master").Activate

End Sub

Thank you in advance
 
Try this:
VBA Code:
Sub Copy_Sheets_To_Master()
'Modified  7/5/2022  5:32:39 AM  EDT
Application.ScreenUpdating = False
On Error GoTo M
Dim i As Long
Dim n As Long
n = Sheets.Count
Dim Lastrow As Long
Dim Lastrowa As Long
Dim Startsheet As String
Dim Lastsheet As String
Dim ans As Long
Dim anss As Long

Startsheet = InputBox("Enter First Sheet Name")
Lastsheet = InputBox("Enter Last Sheet Name")
ans = Sheets(Startsheet).Index
anss = Sheets(Lastsheet).Index

    For i = 2 To n
        Select Case Sheets(i).Index
            Case ans To anss
                Lastrowa = Sheets("Master").Cells(Rows.Count, "A").End(xlUp).Row + 1
                Lastrow = Sheets(i).Cells(Rows.Count, "A").End(xlUp).Row
                Sheets(i).Rows(2).Resize(Lastrow).Copy Sheets("Master").Rows(Lastrowa)
        End Select
    Next
Application.ScreenUpdating = True
Exit Sub
M:
MsgBox "We had some type Problem"
Application.ScreenUpdating = True

End Sub
Thank you for your time it does exactly what it needs to do.
 
Upvote 0

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
In my script above I just need you to enter the exact sheet name to start with
And the exact sheet name to stop with.
I do not

If its a date or a value it does not matter to me.
But if it's 04/07/2022 or Sally it does not matter if that is the sheet name.
Thank you for your time it does exactly what it needs to do.
Glad I was able to help you.
Come back here to Mr. Excel next time you need additional assistance.
Half the reason I help on this forum is to rack my brain to come up with solutions.
And the other half or more is to help Users. Take care
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,327
Members
452,635
Latest member
laura12345

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