+ Reply to Thread
Results 1 to 15 of 15
  1. #1
    Registered User
    Join Date
    03-10-2010
    Location
    Birmingham, England
    MS-Off Ver
    Excel 2003
    Posts
    11

    Talking Macro that reads cell values

    Hi guys,

    Right the deal is I have 87 work sheets, each work sheet with a set of questions and answers exported from a questionnaire. I need to write a macro that can scan all 87 and return that 45 of them contain the answer "single" for question number 2, assuming that there are 45 out of the 87 worksheets that contain the answer "single" for question number two.

    Then I need to go one step further and write one that can scan all 87 and say how many contain the answer "single" for question 2, plus contain the answer "cigarettes" for question number 7. These are hypothetical questions, I'm not actually trying to find out how many of my respondents are single and smoke cigarettes lol. But anyway, you guys seem to know what you're doing, if you want me to upload an example xls containing 10 records with full sets of answers (there are 20 questions) just let me know.

    Any help however small is appreciated, thank you

    ~ Tom
    Last edited by tomhoneyfield; 03-12-2010 at 06:38 AM. Reason: solved

  2. #2
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    18,225

    Re: Help with macro that reads cell values

    Much simpler to write a macro that IMPORTS all 87 sheets into a single sheet that can then serve as a database for the reporting you actually DO want to create later.

    So, yes, upload a couple of sample files with a sample of what you might want the merged data to layout. I have many "Consolidation" macros that would take you to that point.

    Click GO ADVANCED and use the paperclip icon to post up your workbook.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

  3. #3
    Registered User
    Join Date
    03-10-2010
    Location
    Birmingham, England
    MS-Off Ver
    Excel 2003
    Posts
    11

    Smile Re: Help with macro that reads cell values

    Hey Jerry,

    Thanks for your reply, I have attached a sample xls, containing the first 10 records taken from the actual questionnaire. Sorry about the mess, you see I have had to copy and paste it from html document, which only displays each full set of answers individually. I have however made sure that every question and every answer lies in the same cell in each of the 10 worksheets. If you say a consolidation macro is the way to go I'm all ears,

    thanks again, reps for answering

    ~ Tom
    Attached Files Attached Files

  4. #4
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    18,225

    Re: Help with macro that reads cell values

    Are all 87 worksheets in the same workbook, like these 10 are? I'd hate write a macro that consolidates sheets in workbook into a database, when you need sheets from OTHER workbooks consolidated.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

  5. #5
    Registered User
    Join Date
    03-10-2010
    Location
    Birmingham, England
    MS-Off Ver
    Excel 2003
    Posts
    11

    Talking Re: Help with macro that reads cell values

    Quote Originally Posted by JBeaucaire View Post
    Are all 87 worksheets in the same workbook, like these 10 are? I'd hate write a macro that consolidates sheets in workbook into a database, when you need sheets from OTHER workbooks consolidated.
    Yeah they are, or more to the point, they will be once I've actually copy and pasted them in lol, that's just the first ten. Once I get an idea of what you're doing I'm hoping I can understand it and configure it as well. I did actually do visual basic in college, I'm just a bit rusty and need someone to stir up the memories. I'm going to be working on building back up my knowledge over the next few days anyway, and the survey is still open so I hope to have at least 200 worksheets in the end.

  6. #6
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    18,225

    Re: Macro that reads cell values

    Something like this...a new sheet called Database is added.

    Each sheet would be parsed and the answers inserted into a separate column for each Questionnaire.

    Once the data is collated in this manner, you have ALL the answers for any one question in a single row, or in concurrent rows for a few of them. With that, it's much simpler to construct COUNTIF() formulas against the answers in one question, or SUMPRODUCT() formulas to look for combinations of answers.
    Attached Files Attached Files
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

  7. #7
    Registered User
    Join Date
    03-10-2010
    Location
    Birmingham, England
    MS-Off Ver
    Excel 2003
    Posts
    11

    Re: Macro that reads cell values

    Thanks Jerry, yeah that looks good. Are you saying there is a way to automatically get the sheet Database to extract each set of answers from the other worksheets and display them as you have sampled there so all answers to any one question are in one row?

  8. #8
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    18,225

    Re: Macro that reads cell values

    We could do it with formulas by changing the Row1 headers to match the sheet names you use, but using 100s and 100s of INDIRECT() formulas may have noticeable performance issue.

    We could create a macro to run through the all the sheets and copy the answers into the format shown. As long as we know the exact cells the answers are in on each sheet, we could do that pretty simply, but it would be hardcoded.

    Hopefully the code would be understandable enough you could adjust it if you decide to change your questions, answers, number of answers or location of answers.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

  9. #9
    Registered User
    Join Date
    03-10-2010
    Location
    Birmingham, England
    MS-Off Ver
    Excel 2003
    Posts
    11

    Re: Macro that reads cell values

    That should be ok because I am making sure that each answer is in the same subsequent cell on each sheet. There are two questions that can contain more than one answer; question 1, which has its answers in a range of cells B3 - B7 which contain a possible 5 answers and question 13, which is range of cells B42 - 51 with a possible 10 answers, if that makes sense.

  10. #10
    Registered User
    Join Date
    03-10-2010
    Location
    Birmingham, England
    MS-Off Ver
    Excel 2003
    Posts
    11

    Re: Macro that reads cell values

    Yeah I've been looking at code all day anyway my brain is spinning, for the most technical bits just add a little comment to say what the code is doing and I should be ok

  11. #11
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    18,225

    Re: Macro that reads cell values

    Here's the code. It will offer to "clear" the existing database. If you say yes, all is cleared and all sheets are collated anew. If you say no, it will append new sheets only to the existing database...any sheets already in will be skipped.

    So, you can add more sheets and rerun the macro anytime to

    There's a button in the top left.
    Code:
    Option Explicit
    
    Sub CollateDateFromSheets()
    'JBeaucaire   3/11/2010
    'Assemble all sheets into an organized database
    Dim ws As Worksheet, wsSumm As Worksheet
    Dim Col As Long, dCheck As Long
    
    Application.ScreenUpdating = False
    'Sheet to collect data into
        Set wsSumm = Sheets("Database")
    
    'Clear existing data if desired
        If MsgBox("Clear existing summary data?" & vbLf & _
            "(No will append all new sheets to existing data)", _
                vbYesNo + vbQuestion) = vbYes Then _
                    wsSumm.Range("A1", Range("A1").SpecialCells(xlCellTypeLastCell)).Offset(0, 1).ClearContents
    
    'column to enter next set of data
        Col = wsSumm.Cells(1, Columns.Count).End(xlToLeft).Column + 1
        
        For Each ws In Worksheets
            If ws.Name <> wsSumm.Name Then   'make sure it's not the summary sheet
                On Error Resume Next
                dCheck = Application.WorksheetFunction.Match(ws.Name, Range("1:1"), 0)
                On Error GoTo 0
                If dCheck = 0 Then           'import if sheet is new
                    With wsSumm
                        .Columns(Col).ColumnWidth = 14
                        .Cells(1, Col) = ws.Name        'add title at top
                        ws.Range("B3:B7,B9,B12,B15,B18,B21,B24,B27,B30,B33,B36,B39,B42:B50,B53,B56,B59,B62,B65,B68,B71").Copy .Cells(2, Col)
                        Col = Col + 1
                    End With
                Else
                    dCheck = 0
                End If
            End If
        Next ws
    
    wsSumm.Range("A1", Range("A1").SpecialCells(xlCellTypeLastCell)).Offset(0, 1).Font.Size = 8
    Application.ScreenUpdating = True
    End Sub
    Attached Files Attached Files
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

  12. #12
    Registered User
    Join Date
    03-10-2010
    Location
    Birmingham, England
    MS-Off Ver
    Excel 2003
    Posts
    11

    Re: Macro that reads cell values

    JERRY. You rock! This is ace man, this will help to no end. The fact that I can add more sheets at a later date is infinitely useful, now I have the code as well I should be able to make adjustments if anything changes which in all honesty it shouldn't. The only task I have left now is to get familiar with the COUNTIF() and SUMPRODUCT() formulas for analysis purposes, but you've done so much for me already we can call it solved if you wish, and I shall endeavour to realise this task myself. THANK YOU SO MUCH!!! I want to rep more but it won't let me!

  13. #13
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    18,225

    Re: Macro that reads cell values

    Hah...no worries. Glad to help.

    I expect you'll add a Report sheet... and in that case you'll want to make sure the macro ignores that sheet, too. Like so:
    Code:
        For Each ws In Worksheets
            If ws.Name <> wsSumm.Name And ws.Name <> "Report" Then   'make sure it's not the summary sheet
    ...just add that in.

    ========
    If that takes care of your need, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

  14. #14
    Registered User
    Join Date
    03-10-2010
    Location
    Birmingham, England
    MS-Off Ver
    Excel 2003
    Posts
    11

    Re: Macro that reads cell values

    Awesome, ok, i've added that code in. How do I say that this is now SOLVED in the subject field? Is that someting that we're supposed to do once a question is answered? Also, I'm managing the COUNTIF() functions ok so I'm pretty much all there. I don't know what I would have done if you hadn't helped! Take it easy man

  15. #15
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    18,225

    Re: Macro that reads cell values

    Look above, for instructions on SOLVED tags, post #13
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.2.0