0

Please help, I am in need of it desperately and any help is appreciated.

I am creating a summary sheet, which will bring in 5 columns from all the sheets.

The sheets may have extra columns but I am only interested in bringing in specific variables to the summary sheet from each sheet. Also, the sheet will have a column named milestone, which is of major interest, so I am only interested in data for the 5 columns for the points where the milestone is not blank.

Also, each sheet may have multiple data points, independent of number is other sheets.

The summary sheet should include all points (all milestones and relevant data for each point from sheets) and be auto-populated as each sheet is filled out by each employee.

So far, I have this but it does not seem to be working, I am very new to VBA, thanks a ton:

Sub MakeSummary()

'J stands for rows in summary2 sheet
'I stands for sheet number

Sheets("SUMMARY2").Select
'Range("A1:D60").Value = ""
J = 4
For I = 4 To Sheets.Count
    A = Sheets(I).Name
        If (Sheets(A).Range("A1").Value = "") Then GoTo 10
        x = 3
            For Each Worksheet In ThisWorkbook.Sheets

                Do Until Cells(x, 1).Value <> ""
                    Range("A" + Format(J)).FormulaR1C1 = "='" + A + "'!R4C1"
                Loop

                ''Do While Cells(x, 1).Value <> ""
                    ''Range("B" + Format(J)).FormulaR1C1 = "='" + A + "'!R4C2"
                ''Loop

                Do Until Cells(x, 1).Value <> ""
                    Range("B" + Format(J)).FormulaR1C1 = "='" + A + "'!R4C3"
                Loop

                Do Until Cells(x, 1).Value <> ""
                    Range("C" + Format(J)).FormulaR1C1 = "='" + A + "'!R4C4"
                Loop
            Next Worksheet
    J = J + 1
10:
    Next I

End Sub
Community
  • 1
  • 1
  • Which five columns do you want to extract from each `Sheet` and write to the summary? Though you're off to a good start, I'm confused by the `For Each Worksheet In ThisWorkbook.Sheets` loop, as you're also looping through sheets with the `I` variable – Dan Wagner May 20 '14 at 03:15
  • Hi there, thank you so much for your response, like I said I am very bad with VBA and this is an urgent request from my boss so I compiled whatvever I could from online sources, I am just needed to pull "department name, milestone due, the date, the month and activity" there are more tabs in subsheets – user3654793 May 20 '14 at 03:28
  • For each worksheet in this workbook, i thought I needed a macro to autopopulate for entries from each subshet, as right now only first one from each sheet shows up – user3654793 May 20 '14 at 03:28
  • So you are filling in data on the other sheets? From the problem description above, it seemed like you only needed to compile data from these sheets, not fill-in additional information – Dan Wagner May 20 '14 at 03:31

1 Answers1

0

EDIT: Adjusted the design a bit (removed the sector column from each source sheet as that info is available from Sheet.Name), try this out:

Option Explicit
Sub CombineDataSheets()

Dim Summary As Worksheet, Sheet As Worksheet
Dim MonthCol As Long, LastSummaryRow As Long, _
    LastRow As Long, Index As Long
Dim Source As Range, Target As Range

'set references up-front
Set Summary = ThisWorkbook.Worksheets("SUMMARY2")
MonthCol = 1
LastSummaryRow = FindLastRow(Summary)

'loop through sheets and write info back to summary
For Each Sheet In ThisWorkbook.Worksheets
    'write out all data except sector
    If Sheet.Name <> "SUMMARY2" And Sheet.Name <> "Summary" _
        And Sheet.Name <> "Milestone Types" Then
        With Sheet
            If .Cells(4, MonthCol) <> "" Then
                LastRow = .Cells(.Rows.Count, MonthCol).End(xlUp).Row
            Else
                LastRow = 4
            End If
            Set Source = .Range(.Cells(4, MonthCol), .Cells(LastRow, 7))
        End With
        With Summary
            Set Target = .Range(.Cells(LastSummaryRow + 1, MonthCol + 1), _
                .Cells(LastSummaryRow + 1 + LastRow, 7))
            Source.Copy Target
            'write out sector data
            Index = FindLastRow(Summary)
            While .Cells(Index, 1) = ""
                .Cells(Index, 1) = Sheet.Name
                Index = Index - 1
            Wend
        End With
        LastSummaryRow = FindLastRow(Summary)
    End If
Next Sheet

End Sub

'handy function to identify the last row in a worksheet
Public Function FindLastRow(flrSheet As Worksheet) As Long
    If Application.WorksheetFunction.CountA(flrSheet.Cells) <> 0 Then
        FindLastRow = flrSheet.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    Else
        FindLastRow = 1
    End If
End Function

Original Response: Here are some jumping off points:

Declare your variables and use Option Explicit to catch typos.

Option Explicit '<~ will save your bacon
Sub MakeSummary()

'declare variables, here's a start:
Dim J As Long, I As Long, x As Long
Dim Sheet As Worksheet
'...
'start doing stuff

Avoid .Select when you can. Some serious knowledge dropped here, study up: How to avoid using Select in Excel VBA macros

'...
Dim Summary As Worksheet
Set Summary = ThisWorkbook.Worksheets("SUMMARY2")
'...
'you can now operate on the Summary sheet by referencing it directly.
'for example:
Summary.Cells(x, 5) = "Some Text"

Look for repetition, as it's often an opportunity to refactor. You've got three Do...While loops that are all doing the same thing here, so you can combine your logic:

'...
Do Unit Cells(x, 1).Value <> ""
   Range("A" + Format(J)).FormulaR1C1 = "='" + A + "'!R4C1"
   Range("B" + Format(J)).FormulaR1C1 = "='" + A + "'!R4C3"
   Range("C" + Format(J)).FormulaR1C1 = "='" + A + "'!R4C4"
Loop
'...

Steer clear of GoTo unless it's ABSOLUTELY necessary. A tiny mistake in a GoTo will eventually lead you to pull out your hair and is also a major code smell.

Community
  • 1
  • 1
Dan Wagner
  • 2,693
  • 2
  • 13
  • 18
  • I am confused, sorry and thank you for your time, I have been working with VBA for only last 30 hours or so. My question is how do I ensure that all the entries from each sheet will show up collectively on my summary sheet? making summary cells equal to something will work if there was one entry per cell per sheet...? – user3654793 May 20 '14 at 03:40
  • Thank you so much, here it is, summary 2 is the destination summary file, i have hidden some sheets which may have confused you. – user3654793 May 20 '14 at 03:49
  • I am not sure how to add my file here, I dont see an option, yes the subsheets will be populated by various divisions periodically – user3654793 May 20 '14 at 03:50
  • You'll need to use a file-hosting service, like dropbox – Dan Wagner May 20 '14 at 03:53
  • Good start, will resume this tomorrow as now it's time to turn in for the night – Dan Wagner May 20 '14 at 04:49
  • This is the first thought I woke up with basically an organized idea of what I need: – user3654793 May 20 '14 at 10:27
  • No link showed up there... I put a little time into it, check out this "cool_stuff2.xlsm" book: https://dl.dropboxusercontent.com/u/55764002/cool_stuff2.xlsm – Dan Wagner May 20 '14 at 10:31
  • I woke up with basically an organized idea of what I need: In each subsheet, the macro should look at column D (Milestone Type), if the cell is not blank, then the macro should copy the desired fields from the row to the next empty sheet into the master sheet. This process would ensure all entries from each subsheet. The reason why I say to look for column D is that individuals populating the subsheets may have multiple acitivities listed without a milestone, the master/summary only needs immportant events update from each subsheet i.e. the milestone, the corresponding date, month and sector. – user3654793 May 20 '14 at 10:36
  • Thanks, I see you arranged the summary sheet fields as they appear across all subsheets, I guesss I could do this and then hide the columns my manager doesn't wish to see, however as I explained, in each subsheet, individual may have lets say 50 activities for that month but only two milestones so we are only intersted in information for those two rows, i hope that makes sense? – user3654793 May 20 '14 at 10:39
  • Also, I am not sure how to go about referencing and creating codes myself for this issue and therfore I just stuck with answers I got from other sites (this is in response to your suggested to list summary as my Dim) I am unsure how I could rewrite, but does the issue make more sense now? – user3654793 May 20 '14 at 10:42
  • Oh I see what you did there, ok let me try running it again, also a big thank you for your time – user3654793 May 20 '14 at 10:44
  • Sounds good. At this point, however, the problem statement has changed many times, and I've gone so far as to take your design, modify it and build out the aggregation. My code is commented, which ought to help you walk through it and follow along... If you have more questions, it's probably time to put them in a new post and mark this one as solved: http://i.stack.imgur.com/uqJeW.png – Dan Wagner May 20 '14 at 10:46
  • Just a quick question, when we write a command for each sheet, doesn't it run indefinitely, would that take more time to run than if we had excluded it to the sheets which were named? (Apologies if you alrady took care of this, I am very new to VBA). Also, some of the sheets I had in my origiginal document, named milestone type, was a source sheet from which I was going to crate drop down lists for my subsheets months and milestone type so each subsheet has predetermined two fields they can choose form – user3654793 May 20 '14 at 10:47
  • This line: `If Sheet.Name <> "SUMMARY2" And Sheet.Name <> "Summary" And Sheet.Name <> "Milestone Types" Then` ignores the sheets you mention, `<>` means does not equal – Dan Wagner May 20 '14 at 10:51
  • You have definitely solved more than this post, I cannot tell you how much I appreciate your help, I'll run all of this together and see how it works and see if need be to create another post. I thank you once again for all your time, I will mark this post as solved, MANY THANKS. Just last question, so will the current code bring up only entries from milestone type or for all activities? Should I make another post regarding that? Thank you kindly – user3654793 May 20 '14 at 11:04
  • The code above brings over everything, not just certain milestone types. Before you post another question, you need to make sure you crisply define how those milestone types will be defined -- from there, it ought to be relatively easy to use the `.Autofilter` functionality of Excel on the `SUMMARY2` sheet to eliminate all of the non-critical milestone types – Dan Wagner May 20 '14 at 11:06
  • Thank you Dan, I will be clear in my next post, many many thanks. – user3654793 May 20 '14 at 11:10
  • I have started a new post, if you have some time, and don't mind looking at it, it be much appreciated. – user3654793 May 20 '14 at 13:34
  • Hi there, the code seems to malfunction, each time i refresh the macro to bring the most updated entries from each sheet, all previous ones repeatedly appear, any idea how I can limit each entry to once in the summary sheet? If I repeatedly erase everything from summary sheet and then enter the shortcut for macro, it works fine but if I dont intentionally clear the master sheet, the entries repeat, maybe there can be a code to clear master sheet before each update? – user3654793 May 20 '14 at 13:50
  • This is a common issue that folks need to address with VBA, I would recommend reading up on the `Range.RemoveDuplicates` method as described here on MSDN: http://msdn.microsoft.com/en-us/library/office/ff193823(v=office.15).aspx – Dan Wagner May 20 '14 at 13:58
  • Thank you Dan, each sector may have same milestones, so using the following command, would it delete milestone that are alike? I am only wanting to avoid repeated rows from different sectors, should I replace column with rows????: ThisWorkbook.Worksheets("SUMMARY2").RemoveDuplicates Columns:=Array(1,2), Header:=xlYes – user3654793 May 20 '14 at 14:07