Solved

Excel VBA array from dynamic cell list

Posted on 2014-10-24
1
1,129 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

749 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