Solved

Excel VBA array from dynamic cell list

Posted on 2014-10-24
1
1,166 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

Technology Partners: 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

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 …
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

724 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