Solved

Excel VBA array from dynamic cell list

Posted on 2014-10-24
1
1,079 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
ID: 40402578
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

914 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

13 Experts available now in Live!

Get 1:1 Help Now