Solved

Excel VBA array from dynamic cell list

Posted on 2014-10-24
1
1,056 Views
Last Modified: 2014-10-24
We writing a script that will print multiple Excel sheets to a single PDF.  We are dynamically creating the list of sheets to print using one of the sheets and checking cells in the list.  The corresponding values of cells which have been checked are copied to a new list of cells starting at AA1.  We are using list starting at AA1:AA? to create an array that tells the VBA code which sheets to print.

Our challenge is the list is a different length every time; it may end at AA5 one time and AA250 another.  How do we make the range change as appropriate?  (For the Values dimension)

Sub PrintEGAM2()
'
' PrintEGAM2 Macro
' Print with different Macro
'
Dim wbThis As Workbook
Dim wsInfo As Worksheet
Dim sPDFName As String
Dim Values As Variant

Set wbThis = Application.ThisWorkbook
Set wsInfo = wbThis.Worksheets("Print sheet")

'/// Change the output file name here!///
    sPDFName = InputBox("Enter filename", "Print Quote")
    If sPDFName = "" Then Exit Sub

'Delete the PDF if it already exists
If Dir(ActiveWorkbook.Path & "\" & sPDFName & ".pdf") = sPDFName & ".pdf" Then
    SetAttr ActiveWorkbook.Path & "\" & sPDFName & ".pdf", vbNormal
    'Kill sPDFPath & sPDFName & ".pdf"
End If
    
Values = Application.Transpose([AA1:AA5])

'Select the multiple sheets
Sheets(Values).Select

'Set Filename
stFileName = ActiveWorkbook.Path & "\" & sPDFName

'Print all to PDF
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
       stFileName _
    , Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
       :=True, OpenAfterPublish:=False

'Unselect the three worksheets, otherwise Excel will not allow you to change anything on the three worksheets
Sheets("Print Sheet").Select

'
End Sub

Open in new window

0
Comment
Question by:M_Patton
1 Comment
 
LVL 27

Accepted Solution

by:
Glenn Ray earned 500 total points
Comment Utility
Hi, try this updated code:
Option Explicit
Sub PrintEGAM2()
'
' PrintEGAM2 Macro
' Print with different Macro

Dim wbThis As Workbook
Dim wsInfo As Worksheet
Dim sPDFName, stFilename As String
Dim Values As Variant
Dim rng As Range

Set wbThis = Application.ThisWorkbook
Set wsInfo = wbThis.Worksheets("Print sheet")

'/// Change the output file name here!///
    sPDFName = InputBox("Enter filename", "Print Quote")
    If sPDFName = "" Then Exit Sub

'Delete the PDF if it already exists
If Dir(ActiveWorkbook.Path & "\" & sPDFName & ".pdf") = sPDFName & ".pdf" Then
    SetAttr ActiveWorkbook.Path & "\" & sPDFName & ".pdf", vbNormal
    'Kill sPDFPath & sPDFName & ".pdf"
End If

Set rng = Range("AA1", Range("AA1").End(xlDown))
Values = Application.Transpose(rng)

'Select the multiple sheets
Sheets(Values).Select

'Set Filename
stFilename = ActiveWorkbook.Path & "\" & sPDFName

'Print all to PDF
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
       stFilename _
    , Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
       :=True, OpenAfterPublish:=False

'Unselect the three worksheets, otherwise Excel will not allow you to change anything on the three worksheets
Sheets("Print Sheet").Select

End Sub

Open in new window


There's a range variable (rng) and it is being set just before your Application.Transpose statement.  That, in turn, is now transposing the rng selection.

Regards,
-Glenn
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now