![]() |
![]() |
![]() |
|||||
![]() |
![]() |
![]() |
![]() |
![]() |
|||
| Welcome
to Tech Support Forum home to more then 136,000 problems solved. Issues
have included: Spyware, Malware, Virus Issues, Windows, Microsoft,
Linux, Networking, Security, Hardware, and Gaming Getting your
problem solved is as easy as: 1. Registering for a free account 2. Asking your question 3. Receiving an answer Registered members: * See fewer ads. * And much more..
|
| Want to know how to post a question? click here | Having problems with spyware and pop-ups? First Steps |
|
|||||||
| Microsoft Office support MS Office support forum |
![]() |
|
|
LinkBack | Thread Tools |
|
|
#1 (permalink) |
|
Registered User
Join Date: Apr 2007
Posts: 60
OS: xp pro
|
Using a Macro in MS Excel to produce a report
Hi all
I have created an excel spreadsheet that has a series of questions, each having a drop down list in the answer column. Yes, No and N/A are the only answers available. Is it possible to set up a button that prints a report of all the questions that have been answered No? If this is possible, how? Thanks |
|
|
|
| Important Information |
|
Join the #1 Tech Support Forum Today - It's Totally Free!
TechSupportForum.com is a leading support website for your computer needs. We offer free, friendly and personalized computer support. Why pay to have your computer fixed when you can do it for free. Join TechSupportforum.com Today - Click Here |
|
|
#2 (permalink) |
|
Moderator/ Rangemaster TSF Academy; Analyst, Security Team; Oor Wullie; TSF Surgeon and Resident Comic
|
Re: Using a Macro in MS Excel to produce a report
Hi
Yes, entirely possible. You would create some code that would check the values in the relevant column, and if all are "No" then print a report. What version of Excel are you using? Is the 'Report' already pre-formatted and ready for printing? What are the relevant ranges? What is the relevant sheet name? What case are the answers - upper case sentence case, lower case? How did you create the drop downs - forms toolbar or Control Toolbox? Lot's of info required...
__________________
Iain - Defender of the Haggis and all things Scottish. I don't help by PM - post in the Forums. ![]() ![]() PC Safety & Security::PC running a bit slow?::Donate::Photographers Corner |
|
|
|
|
|
#3 (permalink) |
|
Registered User
Join Date: Apr 2007
Posts: 60
OS: xp pro
|
Re: Using a Macro in MS Excel to produce a report
Hi
I will clarify what I need then answer your questions. 1. The code should not pick out if ALL are No. The code should see which ones are NO, see the adjacent question then create a statements in the report based around those questions. 2. I am using Excel 2003 3. I have not started the report. 4. I have uploaded a copy of the workbook which should help you understand the ranges, case and sheet names. 5. The dropdown boxes were created using the Data > Validation > List option Once created the report must be enabled for each following column. |
|
|
|
|
|
#4 (permalink) |
|
Moderator/ Rangemaster TSF Academy; Analyst, Security Team; Oor Wullie; TSF Surgeon and Resident Comic
|
Re: Using a Macro in MS Excel to produce a report
Hi
Sorry I didn't get a chance to reply sooner. I've amended your workbook and added a button that will generate a report on a separate sheet. The code looks to see if the answer is no. It then copies that value to column B and copies the corresponding question to column A. Hope this helps get you started.
__________________
Iain - Defender of the Haggis and all things Scottish. I don't help by PM - post in the Forums. ![]() ![]() PC Safety & Security::PC running a bit slow?::Donate::Photographers Corner |
|
|
|
|
|
#5 (permalink) |
|
Registered User
Join Date: Apr 2007
Posts: 60
OS: xp pro
|
Re: Using a Macro in MS Excel to produce a report
many thanks for your help. im probably being a dope but the button does not do anything when I press it and when i double click it I am sent to visual basic.
A further little guidance if you could? |
|
|
|
|
|
#6 (permalink) |
|
Moderator/ Rangemaster TSF Academy; Analyst, Security Team; Oor Wullie; TSF Surgeon and Resident Comic
|
Re: Using a Macro in MS Excel to produce a report
Sounds like you may still be in Design Mode. Look on the Control Toolbox toolbar for a blue triangle - you'll need to click it to turn off Design Mode.
__________________
Iain - Defender of the Haggis and all things Scottish. I don't help by PM - post in the Forums. ![]() ![]() PC Safety & Security::PC running a bit slow?::Donate::Photographers Corner |
|
|
|
|
|
#8 (permalink) |
|
Moderator/ Rangemaster TSF Academy; Analyst, Security Team; Oor Wullie; TSF Surgeon and Resident Comic
|
Re: Using a Macro in MS Excel to produce a report
It's possible, but will take me a bit of time and is slightly more complex. I'll get back to you with something whenever I possibly can.
__________________
Iain - Defender of the Haggis and all things Scottish. I don't help by PM - post in the Forums. ![]() ![]() PC Safety & Security::PC running a bit slow?::Donate::Photographers Corner |
|
|
|
|
|
#9 (permalink) |
|
Registered User
Join Date: Apr 2007
Posts: 60
OS: xp pro
|
Re: Using a Macro in MS Excel to produce a report
Okey dokes, it would be greatly appreciated.
Ive amended the existing code so that each column has its own create report button. Im fairly advanced at HTML and PHP and a little bit of MySQL and JS so can kinda read between the lines of code of Visual Basic and amend as nec. |
|
|
|
|
|
#11 (permalink) |
|
Moderator/ Rangemaster TSF Academy; Analyst, Security Team; Oor Wullie; TSF Surgeon and Resident Comic
|
Re: Using a Macro in MS Excel to produce a report
Hi
Apologies - haven't forgotten - real life has rather intruded a bit. I've just got a code glitch to iron out. Should have that sorted in a while.
__________________
Iain - Defender of the Haggis and all things Scottish. I don't help by PM - post in the Forums. ![]() ![]() PC Safety & Security::PC running a bit slow?::Donate::Photographers Corner |
|
|
|
|
|
#12 (permalink) |
|
Moderator/ Rangemaster TSF Academy; Analyst, Security Team; Oor Wullie; TSF Surgeon and Resident Comic
|
Re: Using a Macro in MS Excel to produce a report
Hi again
Had an Excel glitch but got around it. I appreciate you've already made some code changes, but this is based on what we had. There are some things to note - there is no error checking, you'll have to manually save the Word doc, there are no headers etc included in the Word doc. If you need any of these things let me know. Code:
Private Sub CommandButton1_Click()
Dim myRng As Range
Dim c As Range
Dim repRng As Range
Dim wdApp As Word.Application
Set myRng = Range("C6:C35") 'the range on the question sheet that contains Yes/No etc
For Each c In myRng
If c.Value = "No" Then 'if the answer is No
With Sheets("Report")
.Range("B65536").End(xlUp).Offset(1, 0).Value = c.Value 'write the answer to column B
.Range("A65536").End(xlUp).Offset(1, 0).Value = c.Offset(0, -1).Value 'write the question to column A
End With
End If
Next c
Sheets("Report").Columns("A:B").AutoFit 'adjust the cell widths
Worksheets("Report").Range("A2").Select
ActiveCell.CurrentRegion.Copy 'copy the report
Set wdApp = New Word.Application
'create a new instance of Word and add a blank document
With wdApp
.Documents.Add
.Visible = True
End With
'paste the data
wdApp.Selection.Paste
Application.CutCopyMode = False
Set wdApp = Nothing
End Sub
__________________
Iain - Defender of the Haggis and all things Scottish. I don't help by PM - post in the Forums. ![]() ![]() PC Safety & Security::PC running a bit slow?::Donate::Photographers Corner |
|
|
|
|
|
#13 (permalink) |
|
Registered User
Join Date: Apr 2007
Posts: 60
OS: xp pro
|
Re: Using a Macro in MS Excel to produce a report
Hi
Many thanks, will have a blast at it now. I know im gonna be pushing my luck, but is there anyway you can reference the report to a word template, instead of a new document? |
|
|
|
|
|
#15 (permalink) | |
|
Moderator/ Rangemaster TSF Academy; Analyst, Security Team; Oor Wullie; TSF Surgeon and Resident Comic
|
Re: Using a Macro in MS Excel to produce a report
Quote:
![]() Ah - you need to have a reference in Excel to Word's Object Model. Open Excel and press Alt+F11 to open the VB Editor. On the menu bar click on Tools > References and in the dialog box look for Microsoft Word 10.0 Object Library. Check the box and click OK. That should be OK now.
__________________
Iain - Defender of the Haggis and all things Scottish. I don't help by PM - post in the Forums. ![]() ![]() PC Safety & Security::PC running a bit slow?::Donate::Photographers Corner |
|
|
|
|
|
|
#18 (permalink) | |
|
Moderator/ Rangemaster TSF Academy; Analyst, Security Team; Oor Wullie; TSF Surgeon and Resident Comic
|
Re: Using a Macro in MS Excel to produce a report
Quote:
![]() Leave this with me and I'll work on it over the weekend. I'll also try the template part, but I think I know how to do that.
__________________
Iain - Defender of the Haggis and all things Scottish. I don't help by PM - post in the Forums. ![]() ![]() PC Safety & Security::PC running a bit slow?::Donate::Photographers Corner |
|
|
|
|
|
|
#19 (permalink) |
|
Moderator/ Rangemaster TSF Academy; Analyst, Security Team; Oor Wullie; TSF Surgeon and Resident Comic
|
Re: Using a Macro in MS Excel to produce a report
Hi
Having now realised where I'd made the code blooper , see if this works for you.Code:
Private Sub CommandButton1_Click()
Dim myRng As Range
Dim c As Range
Dim repRng As Range
Dim wdApp As Word.Application
Dim wdDoc As Word.Document
Set myRng = Range("C6:C35") 'the range on the question sheet that contains Yes/No etc
For Each c In myRng
If c.Value = "No" Then 'if the answer is No
With Sheets("Report")
.Range("B65536").End(xlUp).Offset(1, 0).Value = c.Value 'write the answer to column B
.Range("A65536").End(xlUp).Offset(1, 0).Value = c.Offset(0, -1).Value 'write the question to column A
End With
End If
Next c
Sheets("Report").Columns("A:B").AutoFit 'adjust the cell widths
With Worksheets("Report")
.Range("A1", .Range("B65536").End(xlUp)).Copy
End With
Set wdApp = New Word.Application
'create a new instance of Word
wdApp.Visible = True
Set wdDoc = wdApp.Documents.Add(newtemplate:=True) 'add a new template
wdDoc.Activate
'paste the data
wdDoc.Application.Selection.Paste
Application.CutCopyMode = False
Set wdApp = Nothing
End Sub
__________________
Iain - Defender of the Haggis and all things Scottish. I don't help by PM - post in the Forums. ![]() ![]() PC Safety & Security::PC running a bit slow?::Donate::Photographers Corner |
|
|
|
|
|
#20 (permalink) |
|
Registered User
Join Date: Apr 2007
Posts: 60
OS: xp pro
|
Re: Using a Macro in MS Excel to produce a report
Hi
Many thanks for the work on the code. This is working as expected. I didnt quite fully explain myself when I said 'New Template'. I meant New, as in I havent created it yet, not new as in create a new template everytime someone runs a report. Do you know how to reference an existing template and insert the NO answers into the document? If so, can the template be referenced to a file server, or does it have to be local on the machine? |
|
|
|
![]() |
| Thread Tools | |
|
|