+ Reply to Thread
Results 1 to 2 of 2

How to generate a new form according to items in listbox

Hybrid View

  1. #1
    Registered User
    Join Date
    09-20-2012
    Location
    美国
    MS-Off Ver
    Excel 2011
    Posts
    1

    Question How to generate a new form according to items in listbox

    I'm creating a program that generate new form using userform.
    So basically there are two list box, the first has all column titles and second is selected column titles. Between those two list boxes are two buttons: add and delete.
    The above works fine, but I have one last step to define the button to generate new form.

    The idea is that once the user clicks the button, it will generate new forms according to the selected column titles.
    My logic is that find the listbox item matches to the form cell, copy the entire column below and paste to the new sheet.

    The following are my code, which can't compile..

    Private Sub Generator_Click()
        
        Dim my_range As Range
        SelectedC.BoundColumn = 1
            
        Dim NewForm As Worksheet
        NewForm = Worksheets.Add(After:="Database").Name = "New Form"
        
        With NewForm
            For Count = 0 To SelectedC.ListCount - 1
                With ThisWorkbook.Worksheets("Database")
                    NameColumn = .Row(3).Find(SelectedC.List(i)).Column
                End With
            
                If NameColumn Is Nothing Then
                    Exit Sub
                Else
                     Set my_range = ThisWorkbook.Worksheets("Database").Range(Cells(4,  NameColumn), Cells(Range("A65536").End(xlUp).Row, NameColumn))
                    my_range.Copy NewForm.Range("A" & NewForm.Rows.Count).End(xlUp).Offset(1, 0)
                End If
            Next Count
        End With
       
        Unload Me
            
    End Sub
    // SelectedC is the 2nd list box name.

    Someone help me please!

    Thank you very much for your help!

  2. #2
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,298

    Re: How to generate a new form according to items in listbox

    If you want to create a separate sheet for each selected value, try:

    Private Sub Generator_Click()
    
        Dim my_range As Range
        Dim NewForm As Worksheet
        Dim Count As Integer
        Dim NameColumn As Range
        Dim shtData As Worksheet
    
        Set shtData = ThisWorkbook.Worksheets("Database")
    
        For Count = 0 To SelectedC.ListCount - 1
            With shtData
                Set NameColumn = .Rows(3).Find(SelectedC.List(Count))
                If Not NameColumn Is Nothing Then
                    Set NewForm = Worksheets.Add(After:=shtData).Name = SelectedC.List(Count)
                    Set my_range = shtData.Range(shtData.Cells(4, NameColumn.Column), shtData.Cells(shtData.Rows.Count, NameColumn.Column).End(xlUp))
                    my_range.Copy NewForm.Range("A2")
                End If
            End With
    FindNext:
        Next Count
    
        Unload Me
    
    End Sub
    If you want all the columns on one new sheet, try
    Private Sub Generator_Click()
    
        Dim my_range As Range
        Dim NewForm As Worksheet
        Dim Count As Integer
        Dim NameColumn As Range
        Dim shtData As Worksheet
    
        Set shtData = ThisWorkbook.Worksheets("Database")
        Set NewForm = Worksheets.Add(After:=shtData).Name = "New Form"
    
        For Count = 0 To SelectedC.ListCount - 1
            With shtData
                Set NameColumn = .Rows(3).Find(SelectedC.List(Count))
                If Not NameColumn Is Nothing Then
                    Set my_range = shtData.Range(shtData.Cells(4, NameColumn.Column), shtData.Cells(shtData.Rows.Count, NameColumn).End(xlUp))
                    my_range.Copy NewForm.Cells(2, Count + 1)
                End If
            End With
    FindNext:
        Next Count
    
        Unload Me
    
    End Sub
    Bernie Deitrick
    Excel MVP 2000-2010

+ 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] Form items chosen in Listbox to populate specific cells in spreadsheet
    By floydian in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 08-14-2013, 07:48 AM
  2. [SOLVED] Auto select items in a Listbox with items from another Listbox
    By perducci in forum Excel Programming / VBA / Macros
    Replies: 16
    Last Post: 05-22-2013, 04:45 PM
  3. Editing listbox items in Dblclick event freezes Excel unless mouse moves over listbox
    By muneebmansoor in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 02-28-2013, 02:21 PM
  4. Transfer random listbox items to new listbox and then loop through selected items
    By narrowgate88 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-31-2013, 05:58 PM
  5. Check if 2 consecutive Form listbox items selected
    By Foreverlearning in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 04-24-2012, 07:55 PM

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