Macro To Loop Through Sheets And Print Them Out Using An Array
Aug 4, 2009
I have created a macro that loops through and creates an array of the visible sheets. Now I would like for it to print each of those sheets out. I think my main mistake is in my declaration of the array type, since I have not worked with arrays much before.
how to make the following code operable. Currently when I run it I'm getting a "Run-time error (9): Subscript out of range" error.
I want to populate an array list based on sheet names, but cannot figure out the last line for syntax:
Dim ws As Worksheet, arr() As String ReDim arr(0 To Sheets.Count-1) For Each ws In Worksheets If InStr(1, ws.Name, "Crp-") Or InStr(1, ws.Name, "Reg-") Or InStr(1, ws.Name, "Grp-") Then arr(counter) = ws.Index counter = counter + 1 End If Next ws Sheets(arr()).printout
As I debug it the array is filled with the proper sheets, but I for some reason cannot figure out the syntax for this line? Does it have anything to do with me declaring the array as a string and using integers as the index?
I've found some code which works to print certain pages with value in cell A1 but I need to print dynamic ranges on some of the sheets as they will have filters on so the rows ranges will be different each time.
So far this is what I have but the dynamic range part is not working:
VB: Sub Print_All_Worksheets_With_Value_In_A1() Dim Sh As Worksheet Dim Arr() As String Dim N As Integer
I'm in the process of placing a button on sheet#1 that will print any of the 5 sheets in the workbook that have been used... a sheets use is determined by cell I53 being > 0...
I've initially used this code to test I53, but it shows all sheets, regardless of I53 value...
Sub PrntUsedShts() Dim ws As Worksheet
For Each ws In Worksheets If Range("I53").Value > 0 Then ws.PrintPreview End If Next ws End Sub
Ideally, the finished code will send all selected sheets collated as a single print job... Should I be looking at putting test positive sheets into a sheet array?, then printing the sheet array?
Once again, your valued input is greatly appreciated...
in creating a macro so that I can print from sheet 1 to sheet name "XYZ" as an array. In other words I want to print selected sheets as one command so that page number in the footer will automatically change.
with an event macro. Basically I have a workbook with 11 worksheets and have created a macro that increases the value of cell J3 by 1 in Sheet1. However at present it only works from Sheet5, but I now need it to run from Sheet6 and Sheet11 as well and not from any other worksheets. The code I have is as follows
Private Sub Workbook_BeforePrint(Cancel As Boolean) If ActiveSheet.CodeName = "Sheet5" Then With Sheet1 .Range("J3").Value = .Range("J3").Value + 1 End With End If End Sub
using a macro to print all sheets in a workbook. During this process each sheet has a print area to print as well. A button will be placed on all the sheets. No matter which sheet you are on I would like it to print all sheets. The code I have works I think, but I would like to not have to go by sheet names in case there are some added later, etc. Also, is there a way to do this without going to each sheet and the user seeing all this? Would like to do it in the "background" so to speak.
Sheets("sheet1").Select 'Range("B2").Select Range("H1:K1").EntireColumn.Hidden = True Range("O1:S1").EntireColumn.Hidden = True Range("V1:W1").EntireColumn.Hidden = True Range("Y1:AC1").EntireColumn.Hidden = True Range("L1:N1").EntireColumn.Hidden = False 'Print range column A thru AD ActiveSheet.PageSetup.PrintArea = "A1:AD84" With ActiveSheet.PageSetup .Orientation = xlPortrait .Zoom = 58 ' .FitToPagesWide = 1 .BlackAndWhite = True .PrintComments = xlPrintNoComments .CenterHorizontally = True End With ActiveWindow.SelectedSheets.PrintOut Copies:=1, Preview:=False, collate:=True
I am trying to write what I thought would be a simple macro to print out specific areas of my worksheets. I have shown the code below; the line causing the problem I have highlighted in RED. I am getting the following error message: "Select method of range class failed".
Reading other posts here. I think this may have something to do with the macro being assigned to a command button in one worksheet (AY114) and I am trying to get the macro to run on both the worksheet that the command button is in (AY114) as well as another worksheet (AY062).
I am trying to build a macro to work with a template file.
The template file has set sheet numbers and names (with one exception, see 4) below).
There are close to 40 sheets in all. Some sheets are never printed. Some sheets are always printed. Many sheets are printed only if they are used. Most of the sheets are 'break out' sheets and a variable number of them will be used.
So... Here is what I'm trying to accomplish.
1) Always print Sheet4(Overview1) 2) Always print Sheet6(Overview2) 3) Print Sheets 11 thru 40 IF value in cell G50 on these sheets is > 0. Note that this cell has a name (Total) and I would like to reference the name if possible. (It could happen that further evolution of the Workbook moves the cell up or down a row.) 4) Print Sheet38(Data Sort) IF it exists. Otherwise, print Sheet1(Data). If the information on the Data sheet is limited (eg only 1 or 2 pages), we print it off directly. If the information is more extensive, we copy it to a new sheet and sort it (Data Sort).
I can achieve 1) and 2). I'm not quite sure how to go about 3) as I'm still not very good with macro loops. 4) I haven't tackled yet.
Creating a macro to compare 2 sheets in a workbook and print the differences to a 3rd sheet.
Each sheet will have the same number of fields, 5 columns with the header in the first row.
All values in the cells are integer except for the last field which will be a character.
The key is the value in the 2nd column. If it's not in the other sheet, then it's a new record. If it's a new record then highlight it a color depending on what sheet contains the new record. Now if the key is the same in both sheets, then check the other columns to see what's different. If there is a difference, print the record for both sheets in the third sheet and highlight the differences. I attached a sample of what I want.
Issue with array that's bolded The way it is set up is to rename the 3rd sheet to MyFileName and rename the last sheet to MyFileNameTwo. The file names will remain constant. They will always be the 3rd and last sheets, but the number in between will vary. Is there anyway to select the 3rd sheet through the last sheet to delete these? When I use the array it wants sheet names but those are based on multiple variables in other workbooks.
I have a Document with multiple sheets in it and I ask it to print entire document which would be 7 sheets (1 Page printed per sheet) but only the first five will print, then it comes up with a second print queue that I need to use and OK to print the balance of them with. I'm printing To a PDF creation program. it saves and Prince the first 5 pages then asks for another new name of the document to save the next few pages. my PDF Creator works fantastic with any other program. I just need to know how to tell it to print as many sheets as I have to print without it doing that. so I can print all sheets in a single PDF document.
I have a workbook that I have automatically update a simple web page listing with which users access files with. In this workbook, I have a table which the information used to update the web page. I need to export cell contents to a central .csv file on the server.
Each time a user opens their version of this workbook, I need to load the data from the central .csv file into the table for the update. I would like the data to be sent to the .csv file as it is in the workbook so I can keep my offsets and loops to load the file back in simple (as well as keep my .csv file size a little smaller).
Right now I have the following which places the cell contents vertically (all in column 1) in the .csv file :
Sheets("Lookup").Range("N7").Activate Open sFilePath2 For Output As #2 For X = 0 To maxMoldsCount For Y = 0 To 99 Print #2, ActiveCell.Offset(Y, X).Value Next Y
This way it would place all of the values in the row into the .csv file in a row then go to the next row. VBA is not a big fan of this syntax and I really don't want to add each offset manually.
Does anyone know of a way to next a loop insid of a Print # line?
I have a print code that I want to run on a loop. In column A Sheet UPSLabels, I have record numbers 1 through 94.
I have a vlookup in "C1" of another sheet that looks at the record number in "B1" and pulls in the correct information from UPSLabels based off the matchin record number in column A. I would like my code to print, update the number in "B1" by 1, and keep printing the records until it reaches 94.
I've probably supplied too much information, but the point is, I need the loop to run until "B1" reaches 94.
I am trying to program a loop in Excel VBA to read the first two characters of a cell in column A and, depending on the characters returned, to print something else in column B, then move to the next row up. What I have so far below is pieced together from little insights from over the web, but currently it does nothing. Not even error messages. It has more fields (if "XX" > print blah blah) but they are all the same, so only the first two iterations are shown here for clarity.
I have written a piece of code that sets the print area for the cells whose formulas are not blank as the result of the if formulas in them. The code works across the page.
In it's current state the loop should repeat four times as that is the number of non-blank cells. The loop I have is:
Code: Do While CollSheet.Offset(0, i).Value "" i = i + 1 Loop
This however, gets as far as i = 2 before completing the loop. To test why I wrote the following piece of code which loops the four times it is meant to. The trouble is I can't work why they aren't returning the same value as to my knowledge, they do the same thing. If I had to guess I think it's something to do with the cell being a merged cell.
Code: Do While ActiveCell.Offset(0, 1).Value "" ActiveCell.Offset(0, 1).Select Loop
I have a sheet that allows users to select a value from a drop down box. The box is linked to cell Y5, which updates values in a print range. A button located with the drop down box then prints the specified range. Everything is functioning fine, but the process is more complicated than I'd like it to be, since users usually want printouts for every value on the list.
The values for the drop down box are located in a dynamic range in column A beginning with row 5. How would I go about creating a loop with VBA which transfers each value in column A to cell Y5, prints the result, then continues down the entire list?
I have a "submit" button macro which user would click after he has finished his input. This macro would update a reference number on the worksheet named "orange" and then print out this worksheet.
My purpose is "orange" is printed out with a reference number.
Subscript 9 error that Ive been getting when I run this macro. It has worked in the past, but when I tried to test it today I keep getting errors on the array portion.
I have 9 named ranges on worksheet Sheet1. I want to print every combination of every non-singular range on worksheet Sheet2. Below is a simplified version of the scenario.
There are three named ranges: Letters, Colors, Animals. Say the below are the entries for each range.
Letters = {A, B, C} Colors = {Red, Blue} Animals = {Dog}I want to print every combination of Letters and Colors but exclude Animals since it only has 1 entry.
Therefore my result would look something like this:
A Red B Red C Red A Blue B Blue C Blue
My thought is to make a multidimensional array GrandArray where GrandArray(1) = Letters and GrandArray(2) = Colors, then recursively go back through every combination and print to Sheet 2. I can set up GrandArray, but stepping through each element is creating mismatch errors.
I'm trying to avoid For loops since my real data has 9 ranges which may or may not be included in the final print.
when is it appropriate to us arr(1 to 10) Vs. For i = 1 to 10. I know this may sound like a silly question and expose my ignorance - but I suppose there is no other way to learn :|
The code prints a series of sheets. Afterwards these sheets are "group"ed together. The code can be accessed from 2 different sheets. I need to figure out how to get rid of the grouping and return to the original sheet.
Private Sub CommandButton1_Click() Sheets( Array("Pipe", "Pipe Flow")).PrintOut UserForm1.Hide End Sub
I faced a problem to attach this small WB as an XLS
I'm looking for some way to some the red cells without looping.
In this example the array was filled with A1:A10 values. In the real situation the array gets its values from other source than a Worksheet Range.
As you can see I manged to transport the Array Values to F1:F10 and from here I could calculate the sum of F3:F8 but I do not want to use any helper columns.
I have created some code of which this is an extract
Dim i As Variant
i = Array(37, 38, 41, 42)
For Each i In i
'Some Code Next i
the routine works fine when the i variable is hard coded, but once the above is included it fails (Error 10 This array is fixed or temporarily locked).
I've built a simple inventory tracking system, and decided a reporting feature would be nice. There are four categories that are entered when inventory is removed...Date, Employee Name, Item Description, Location, and quantity.
Four my reporting purposes I'm only concerned with Date, Employee name and Item Description.
I've been able to write code that does what I want using a multiple Cases and a For loop once the case is identified. However, the more data there is the longer this takes...so I decided to stretch myself and try my hand at arrays (first time really working with arrays), but I'm having trouble figuring out exactly what I need to do.
Here is what I think the steps need to be.
1. Store my data (the categories above) which are located in the Check Out sheet 2. Go through the arrayed data to find exact matches based on my search criteria (here is where the Cases come in) 3. Pull out only that data and write the information to a "Report" Sheet 4. Export that sheet to PDF (this part I already have)
Below is a copy of what my current "working" code looks like (I should mention that the search selections are made from a userform this is what the Cases are deciphering between which ones are blank etc...), also most of my variables are instantiated as Public variables within a Public_Variables module also below.