+ Reply to Thread
Results 1 to 5 of 5

Adding a Call command to an existing VBA

Hybrid View

  1. #1
    Forum Contributor PY_'s Avatar
    Join Date
    09-23-2008
    Location
    Houston
    MS-Off Ver
    Office 2016
    Posts
    289

    Adding a Call command to an existing VBA

    I am not sure if this needs a new thread or not so i posted a new one just in case.

    I had a previous thread that repeates the same set of commands on all visible sheets located here:

    http://www.excelforum.com/excel-gene...n-combine.html

    The one problem i am having with the code is i need to add a Call command ( Call Copy_Punches ) to run on each visible sheet and im not sure where to put it. Here is the code i am trying but it only Calls it on the first visible sheet and it does not call it on the rest of the visible sheets. Where should i move it so it works properly on each sheet.

    Sub Generate_CSV()
        'Application.ScreenUpdating = False
        
        Dim wks         As Worksheet
        Dim j           As Long
    
        Sheets("Customer Info").Visible = False
        Set wks = Worksheets("CSV_Export")
            Worksheets("CSV_Export").Visible = True
            Worksheets("CSV_Export").Select
            ActiveSheet.Unprotect Password:="2010Turbo"
            
            ' Clears out previous run of Copy_Sort command
            
            Worksheets("CSV_Export").Range("A1:IV200").ClearContents
            Worksheets("CSV_Export").Range("A1").Value = "*** JOB NUMBER"
            Worksheets("CSV_Export").Range("A3").Value = "*** PUNCH PATTERNS"
            Worksheets("CSV_Export").Range("A4").Value = "*** MEMBER DEFINITIONS"
            
            Worksheets("CSV_Export").Visible = False
            Call Select_First_Visible_Sheet
            
            With wks
                For j = 1 To Worksheets.Count
                    If Worksheets(j).Visible = xlSheetVisible Then ' shg added this
                        
                       Call Copy_Punches ' I need this to run on all visible sheets _
                                            before it performs the copy/paste below.
                                            
                    .Unprotect
                             Worksheets(j).Range("G101:IC112").Copy
                    .Cells(.Rows.Count, "A").End(xlUp).Offset(1).PasteSpecial _
                            Paste:=xlPasteValues, _
                            Operation:=xlNone, _
                            SkipBlanks:=False, _
                            Transpose:=False
                    .Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
                    
                    Range("A10").Select
                    
                End If
            Next j
        End With
    
        Application.CutCopyMode = False
        Application.ScreenUpdating = True
        
        End Sub
    Last edited by PY_; 09-14-2010 at 02:24 PM.

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Adding a Call command to an existing VBA

    Where's the code for Copy_Punches?
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Forum Contributor PY_'s Avatar
    Join Date
    09-23-2008
    Location
    Houston
    MS-Off Ver
    Office 2016
    Posts
    289

    Re: Adding a Call command to an existing VBA

    Code here:

    Its located in the same Module right below the above code

    Sub Copy_Punches()
    
        ActiveSheet.Unprotect Password:="**********"
        
        Rows("30:130").EntireRow.Hidden = False
        Columns("S:AR").EntireColumn.Hidden = False
        
        Range("W70:AH81").Copy 'Standard Punches
            Range("A86").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        Range("E55:P65").Copy ' Continue Test 1
            Range("M86").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        Range("E70:P79").Copy ' Continue Test 2
            Range("Y86").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        Range("V33:AN44").Copy ' Leading Punch
            Range("AK86").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        Range("V46:AN57").Copy ' Trailing Punch
            Range("BD86").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        
        Rows("30:130").EntireRow.Hidden = True
        Columns("S:AR").EntireColumn.Hidden = True
        Range("A10").Select
        ActiveSheet.Protect Password:="**********"
        
    End Sub

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Adding a Call command to an existing VBA

    Try this:
    Sub Generate_CSV()
        Dim wksCSV      As Worksheet
        Dim wks         As Worksheet
        Dim i           As Long
    
        Sheets("Customer Info").Visible = xlSheetHidden
    
        Set wksCSV = Worksheets("CSV_Export")
        With wksCSV
            .Unprotect Password:="2010Turbo"
            .Rows("1:200").ClearContents
            .Range("A1").Value = "*** JOB NUMBER"
            .Range("A3").Value = "*** PUNCH PATTERNS"
            .Range("A4").Value = "*** MEMBER DEFINITIONS"
        End With
    
        For i = 1 To Worksheets.Count
            Set wks = Worksheets(i)
            With wks
                If .Visible = xlSheetVisible Then
                    CopyPunches wks
                    .Unprotect
                    .Range("G101:IC112").Copy
                    wksCSV.Cells(.Rows.Count, "A").End(xlUp).Offset(1).PasteSpecial _
                            Paste:=xlPasteValues
                    .Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
                End If
            End With
        Next i
    End Sub
    
    Sub CopyPunches(wks As Worksheet)
        With wks
            .Unprotect Password:="**********"
            
            .Range("W70:AH81").Copy    'Standard Punches
            .Range("A86").PasteSpecial Paste:=xlPasteValues
            
            .Range("E55:P65").Copy    ' Continue Test 1
            .Range("M86").PasteSpecial Paste:=xlPasteValues
            
            .Range("E70:P79").Copy    ' Continue Test 2
            .Range("Y86").PasteSpecial Paste:=xlPasteValues
            
            .Range("V33:AN44").Copy    ' Leading Punch
            .Range("AK86").PasteSpecial Paste:=xlPasteValues
            
            .Range("V46:AN57").Copy    ' Trailing Punch
            .Range("BD86").PasteSpecial Paste:=xlPasteValues
    
            .Protect Password:="**********"
        End With
    End Sub
    You need to be consistent with the passwords on the visible sheets, and it appears you are not.

  5. #5
    Forum Contributor PY_'s Avatar
    Join Date
    09-23-2008
    Location
    Houston
    MS-Off Ver
    Office 2016
    Posts
    289

    Re: Adding a Call command to an existing VBA

    I fixed the passwords in the vba code and it seems your new code is working perfectly. I made a tweak to it so i can see a specific sheet again.


    As always Shg, thanks for your help!
    Last edited by PY_; 09-14-2010 at 02:24 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1