98.7% Of all customers recommend us, we're so confident about our results we publish all reviews and stats
View Live Stats View ReviewsForum home » Delegate support and help forum » Microsoft Excel VBA Training and help » Consolidate using VBA
Consolidate using VBA
Resolved · Urgent Priority · Version 2007
Sam has attended:
Excel VBA Intro Intermediate course
Consolidate using VBA
I'm trying to use the consolidate function in a macro. I have recorded a macro and have tried to edit it but my issue is that in the array I don't want to hardcode the name of the workbook., instead I want it to refer to a named range.
The code I recoded is below:
Selection.Consolidate Source:=(Array _
"filepath[filename].ResultsC!R12C6:R4361C60"), _
Function:=xlSum, TopRows:=False, LeftColumn:=True, _CreateLinks:=False
But I want to replace the filepath and filename with something that can change.
The code I've written is below:
Dim strWb As String
strWb = CStr(ThisWorkbook.Sheets("Control").Range("rngWorkbook").Value)
Workbooks(strWb).Activate
Range("BJ12").Select
Selection.Consolidate _
Sources:=Sheets("ResultsC").Cells("R12C6:R4361C60"), _
Function:=xlSum, TopRows:=False, LeftColumn:=True, _CreateLinks:=False
Where the name of a workbook has to be entered into a separate workbook on a sheet called Control and in a named range rngWookbook.
If I I run the code above or use "Array" after "Sources:=" it comes up with error 1004 "applicaition-defined or object-defined error".
I hope you can help!
RE: Consolidate using VBA
Hi Sam
Thanks for getting in touch. This looks really close to the finished result.
Have you tried stepping through (F8) - does it trip on the Workbooks(strWb).Activate line (i.e. does it find the right workbook)?
The '1004' error usually refers to trying to do something that is illegal in Excel e.g. trying to go beyond the boundaries of the worksheet, or reference a worksheet that does not exist.
Where does "ResultsC" come into it?
Kind regards
Gary Fenn
Microsoft Office Specialist Trainer
Tel: 0207 987 3777
Best STL - https://www.stl-training.co.uk
98%+ recommend us
London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector
RE: Consolidate using VBA
Hi Gary,
Thanks for your response.
It trips up on the first line of the consolitate code (it doesn't like how I've referred to the sheet I think). ResultsC is the sheet with the table of data which I'd like consolidating.
Any thoughts on why the consolidate code isn't quite right?
Thanks,
Sam
RE: Consolidate using VBA
Hi Sam
Thanks for your reply. Try the following:
Selection.Consolidate _
Sources:=Array("ResultsC!R12C6:R4361C60"), _
Function:=xlSum, TopRows:=False, LeftColumn:=True, _CreateLinks:=False
To add another range to the consolidate, add a comma after the second speech mark, before the close parenthesis.
Kind regards
Gary Fenn
Microsoft Office Specialist Trainer
Tel: 0207 987 3777
Best STL - https://www.stl-training.co.uk
98%+ recommend us
London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector
Training information:
Welcome. Please choose your application (eg. Excel) and then post your question. Our Microsoft Qualified trainers will then respond within 24 hours (working days). Frequently Asked Questions
Any suggestions, questions or comments? Please post in the Improve the forum thread. |
Excel tip:Importing Numbers in Excel 2010Occasionally, when importing data into Excel you find that the some of the imported values are treated as text. |