+ Reply to Thread
Results 1 to 8 of 8

Call macro for the sheets selected in listbox

Hybrid View

  1. #1
    Registered User
    Join Date
    09-10-2012
    Location
    Malaysia
    MS-Off Ver
    Excel 2007
    Posts
    61

    Call macro for the sheets selected in listbox

    Please help me
    what is wrong with my code, i can't seem to loop the code for the sheets that I have selected using the listbox.

    please see my codes here and advise what i need to change.

    Private Sub UserForm_Initialize()
        Dim ExcludedSheets As Variant
        Dim oneSheet As Worksheet
        ExcludedSheets = Array("Master", "Vessel", "Location")
        For Each oneSheet In ThisWorkbook.Sheets
            With oneSheet
                If IsError(Application.Match(.Name, ExcludedSheets, 0)) Then
                    ListBox1.AddItem .Name
                End If
            End With
        Next oneSheet
    End Sub
    This is my userform listbox


    Private Sub btn_Vessel_Click()
        
        'This macro will copy all rows from the first sheet
        '(including headers)
        'and on the next sheets will copy only the data
        '(starting on row 3)
    
        Dim i As Integer
        Dim j As Long
        Dim SheetCnt As Integer
        Dim lastRow1 As Long
        Dim lastRow2 As Long
        Dim lastCol As Integer
        Dim ws1 As Worksheet
    
        With Application
            .DisplayAlerts = False
            .EnableEvents = False
            .ScreenUpdating = False
        End With
    
        On Error Resume Next
    
        'Delete the Target Sheet on the document (in case it exists)
        Sheets("Vessel").Delete
        'Count the number of sheets on the Workbook
        SheetCnt = Worksheets.Count
    
        'Add the Target Sheet
        Sheets.Add after:=Worksheets(SheetCnt)
        ActiveSheet.Name = "Vessel"
        Set ws1 = Sheets("Vessel")
        lastRow2 = 3
        'Define the row where to start copying
        '(first sheet will be row 3 to include headers)
        j = 3
    
        'Combine the sheets
        For i = 0 To ListBox1.ListCount
            Worksheets(i).Select
    
            'check what is the last column with data
            lastCol = ActiveSheet.Cells(2, ActiveSheet.Columns.Count).End(xlToLeft).Column
    
            'check what is the last row with data
            lastRow1 = ActiveSheet.Cells(ActiveSheet.Rows.Count, "C").End(xlUp).Row + 3 '+3 due to row 1 to 3 of column C is blank
            
    
            'Define the range to copy
            Range("A" & j, Cells(lastRow1, lastCol)).Select
    
            'Copy the data
            Selection.Copy
            ws1.Range("A" & lastRow2).PasteSpecial
            Application.CutCopyMode = False
    
            'Define the new last row on the Target sheet
            With ws1
            lastRow2 = .Cells(.Rows.Count, "C").End(xlUp).Row + 3 '+3 to start on the next row after the last row
            End With
            
            'Define the row where to start copying
            '(2nd sheet onwards will be row 5 to only get data)
            j = 5
        Next
    
        With Application
            .DisplayAlerts = True
            .EnableEvents = True
            .ScreenUpdating = True
        End With
    
        Sheets("Vessel").Select
        Cells.EntireColumn.AutoFit
        Range("A1").Select
    
        Application.CutCopyMode = False
        Unload UserForm1
        
    End Sub
    This is the button where I want to run the macro for the sheets selected.

  2. #2
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,953

    Re: Call macro for the sheets selected in listbox

    solved elsewhere
    Last edited by protonLeah; 10-30-2014 at 10:30 PM.
    Ben Van Johnson

  3. #3
    Registered User
    Join Date
    09-10-2012
    Location
    Malaysia
    MS-Off Ver
    Excel 2007
    Posts
    61

    Re: Call macro for the sheets selected in listbox

    Thanks protonLeah

    Do I need to use the For i = 0 To ListBox1.ListCount - 1 if in my listbox i'm selecting by the sheetnames?
    Can I just loop the codes for the sheetnames instead of by selecting the sheet?

    I can't seem to get it right, maybe is because in my listbox I'm excluding 3 sheets (Vessels, Location and summary)


    Can you teach me how to just select the sheetnames?

  4. #4
    Registered User
    Join Date
    09-10-2012
    Location
    Malaysia
    MS-Off Ver
    Excel 2007
    Posts
    61

    Re: Call macro for the sheets selected in listbox

    i keep selecting the sheets that i have not selected... is there something broken in my first code for the userform?

    i wanna cry...

  5. #5
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,953

    Re: Call macro for the sheets selected in listbox

    solved elsewhere
    Last edited by protonLeah; 10-30-2014 at 10:30 PM.

  6. #6
    Registered User
    Join Date
    09-10-2012
    Location
    Malaysia
    MS-Off Ver
    Excel 2007
    Posts
    61

    Re: Call macro for the sheets selected in listbox

    still no luck.


    i'm able to populate all the sheetnames in my listbox in the userform.

    Now i need to run a code on each of the sheets selected from the listbox.

    how do i do this?

  7. #7
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,953

    Re: Call macro for the sheets selected in listbox

    solved elsewhere
    Last edited by protonLeah; 10-30-2014 at 10:30 PM.

  8. #8
    Registered User
    Join Date
    09-10-2012
    Location
    Malaysia
    MS-Off Ver
    Excel 2007
    Posts
    61

    Re: Call macro for the sheets selected in listbox

    Thanks protonleah

    yes, i have got it solves in another thread.

    here is the sample file that i have successfully done it.

    just to share to anyone who is going through the same problem.

    Combine Success1.xlsm

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Macro to run when item is selected from listbox
    By kosherboy in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 04-30-2014, 09:58 PM
  2. [SOLVED] Run 1 macro when any item from listbox is selected
    By kosherboy in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-30-2014, 09:52 PM
  3. Call macro if specific text is within listbox
    By EthanM in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-02-2011, 12:00 AM
  4. Print only selected sheets from listbox
    By stevemcleod in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-09-2010, 03:24 PM
  5. call macro if cell is selected
    By Corey in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-17-2006, 03:45 AM

Tags for this Thread

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