You are here:

Excel/Looping through selected sheets for editing and changes in each

Advertisement


Question
Hello Mr. Umlas.
I have a 40 sheet WB with the same structure in each sheet where user can add from one line to 30 lines of items. Like a different list in each sheet.
At some point it is needed to print a selection of 4 to 10 sheets, after making some changes in each sheet such as writing in some cells, inserting blank rows, deleting blank rows, changing a format, etc, etc., all this needed for the final presentation of each sheet to be printed in one print job. We are basically improving our printing process so just with one Sub, we can prepare and print all the sheets selected by user in one click. At this moment we run a routine for preparation and printing for each sheet separately. We want to select the sheets we need and then run a SUB that does the job at once.
The problem we have is that any change you make to a sheet in a selection is reflected exactly in the other selected sheets. As in this case they vary in size (each list is different), inserting/deleting rows creates chaos and writing in some cells as well.
We have tried to loop with following procedure but we can not get rid of the group solidarity. We want to loop into each sheet, make necessary arrangements in each one, not affecting the others and then loop to the next.
Here is the code we have so far

Sub PreparingPrintingSelectedSheets()

Dim N As Long
Dim M As Long
Dim Arr() As String
Dim ws As Worksheet

' We make all the modifications for each list
'''''''''''''''''''''''''''''''''''''''''''''''''''''''

For Each ws In ActiveWindow.SelectedSheets
   ws.Activate
   ws.Range("body").Select
   On Error GoTo base2
   Selection.SpecialCells(xlCellTypeBlanks).Select
   Selection.EntireRow.Delete


base2:

   ws.Range("Print_Area").Select

   ActiveCell.Offset(0, 1).Range("A1").Select
   ActiveCell.FormulaR1C1 = "Central Office"
   ActiveCell.Offset(1, 0).Range("A1").Select
   Selection.EntireRow.Insert
   
   Selection.EntireRow.Insert
   ActiveCell.FormulaR1C1 = "Logistics"
   
   ActiveCell.Offset(1, 0).Range("A1").Select
   ActiveCell.FormulaR1C1 = "Form L-2628"
   
   ActiveCell.Offset(3, 0).Range("A1").Select
   Selection.NumberFormat = """Job No. ""###0"
   
   ActiveCell.Offset(1, 0).Range("A1").Select
   Selection.EntireRow.Insert
   '
   ActiveCell.FormulaR1C1 = "'"
   
   '
   
   ActiveCell.Offset(1, 0).Range("A1").Select
   Selection.EntireRow.Insert
   '
   ActiveCell.FormulaR1C1 = "'"
   
   '
   
   Cells.Find(What:="omiso", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
       :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
       False, SearchFormat:=False).Activate
   ActiveCell.Offset(-9, 0).Range("A1:A10").Select
   ActiveCell.Activate
   
   Selection.EntireRow.Delete
   ActiveSheet.Tab.ColorIndex = 7
   Call PositionAtGreenSpot
   
Next

' This prints all selected sheets.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''
With ActiveWindow.SelectedSheets
       ReDim Arr(1 To .Count)
       For N = 1 To .Count
         Arr(N) = .Item(N).Name
       Next N
   End With
   Sheets(Arr).PrintOut

Sheets(Arr).Move Before:=Sheets(2)


End Sub

I guess I need to work with sheets as an array, then iterate with each one making the changes, then printing the group; but I am unable to work with the array properly.
Your help will be very much appreciated.
Gabriel

Answer
If you've selected many sheets at once (group mode), then your code will work on all the sheets simultaneously, by design. You need to UNGROUP the sheets before running your code. What you'd have to do after you manually group them is save the names of the sheets in the activewindow.selected sheets, then ungroup them, then work with each named sheet that you saved, then perhaps regroup them, print, ungroup them again (or leave them grouped (dangerous)).

Let me know if you need a snippet of code to simulate this.
About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

Excel

All Answers


Answers by Expert:


Ask Experts

Volunteer


Bob Umlas

Expertise

I`m a Microsoft Excel MVP (Most Valuable Professional) and have been since the inception of the program in 1995. I can answer every kind of Excel question except: API, Importing/exporting to other programs (powerpoint, word,...) Also check out my in-person training link at http://www.thumbtack.com/ny/new-york/excel-training/

Experience

Worked with MS Excel since version 0.99 (on the Mac!). Was contributing editor to Excellence Magazine, having written >300 articles. John Walkenbach said of me "I finally met someone who knows as much about Excel as I do."

Publications
Excellence, The Expert, Microsoft

Education/Credentials
BA in math, Hofstra University, 1965

Awards and Honors
MVP
Led sessions for the Convergence 2004-2006 seminar on Excel tips & tricks

©2016 About.com. All rights reserved.