I need a strategy for combining two sheets

revver

Active Member
Joined
Dec 20, 2007
Messages
257
I have a workbook with twelve sheets named Jan, Feb, Mar ... Nov, Dec.
Each sheet is similar and quite simple. Row 1 contains headings. Column A contains increasing dates beginning at row 2 and extending to row 29, 31 or 32 depending on the month. The next three columns (B, C, D) contain the three readings for the day. That is all that is relevant to my problem really. The rest is max, min averages etc and a graph.

I want to add a thirteenth sheet laid out in a similar way but showing a portion of two consecutive sheets.
I want to enter a date somewhere on it and have it retrieve the three daily readings for that date from the appropriate sheet(s) and so on for 30 more days.

Lets say I enter Feb 17. I want the new sheet to show the daily readings for the month which begins on that date. I realize that I will have to begin with the 'Feb' sheet and then switch to the 'Mar' sheet.

Is the best strategy to use vba to copy data from 'Feb' and 'Mar'
or is there a way to do this with formula.

I hope I have explained this satisfactorily.
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Is the best strategy to use vba to copy data from 'Feb' and 'Mar'
or is there a way to do this with formula.
If I have understood correctly, below are both vba and formula methods. Note that ..
i) I have used dates from 1 Jan 2017 to 31 Dec 2017 in my sample workbook
ii) The formula method involves a number of uses of the volatile function INDIRECT so may cause your sheet to recalculate more often than normal.

Try each method in a new copy of your workbook

Formula Method
On the 13th sheet, set up row 1 as shown.
Formula in A2 is copied down to A32
Formula in B2 is copied across to D2 and down the columns to row 32.

Excel Workbook
ABCD
1Date17/02/2017
217/02/2017193256
318/02/2017497498
419/02/2017651915
520/02/2017238552
621/02/2017504560
722/02/2017166828
823/02/2017809537
924/02/2017116042
1025/02/2017206516
1126/02/2017679245
1227/02/2017738065
1328/02/201718243
141/03/2017181128
152/03/2017406867
163/03/2017621252
174/03/201739540
185/03/2017267248
196/03/2017259957
207/03/2017602471
218/03/2017543777
229/03/2017937629
2310/03/2017747622
2411/03/2017174587
2512/03/2017554616
2613/03/2017933151
2714/03/201793410
2815/03/2017446559
2916/03/2017279259
3017/03/2017367635
3118/03/2017313636
3219/03/2017119770
Sheet13




VBA Method

1. On the 13th sheet, enter "Date" in A1 as for the formula method, but leave B1 empty.
2. Right click the sheet name tab and choose "View Code".
3. Copy and Paste the code below into the main right hand pane that opens at step 1.
4. Close the Visual Basic window & test by entering a date in B1
5. Your workbook will need to be saved as a macro-enabled workbook (*.xlsm).

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  Dim a(1 To 31, 1 To 4) As Variant
  Dim i As Long, j As Long
  Dim StartDate As Date
  
  If Not Intersect(Target, Range("B1")) Is Nothing Then
    Application.EnableEvents = False
    Range("A2:D32").ClearContents
    If IsDate(Range("B1").Text) Then
      StartDate = Range("B1").Value
      For i = 1 To 31
        a(i, 1) = StartDate + i - 1
        With Sheets(Format(a(i, 1), "mmm"))
          For j = 2 To 4
            a(i, j) = .Cells(Day(a(i, 1)) + 1, j).Value
          Next j
        End With
      Next i
      Range("A2:D32").Value = a
    End If
    Application.EnableEvents = True
  End If
End Sub
 
Upvote 0
Thanks very much Peter, G'day.
I wasn't expecting anyone to do my work for me. I was about to roll up my sleeves and unwrap my vba when it occurred to me that a formula technique might work. I figured that avoiding vba would be preferred because of being able to save as .xls and not having to explicitly launch the code. So I took to print for suggestions.

I have implemented your formula method and it works well. I will also try the other method for my education but I'll stick with the first for simplicity.
 
Upvote 0
You are welcome, and using the formula version is fine by me.

Note, though, that the vba method suggested does not require "having to explicitly launch the code" as it is automatically launched when a date is entered in cell B1. The user does need to have macros enabled though.

(BTW, feels like Brisbane weather here - very warm & humid! :))
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,897
Messages
6,175,271
Members
452,628
Latest member
dd2

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