Results 1 to 5 of 5

Thread: VBA To Update Or Refresh Listview Items Or Records Dynamically In Userform

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    21
    Rep Power
    0

    VBA To Update Or Refresh Listview Items Or Records Dynamically In Userform

    I have userform with datasource in worksheet, now when i add data into worksheet through textbox listview records dont get refreshed,
    i have to upload the userform and load again to update the listview.
    Is there anyway when i click on add button the data will transfer to worksheet & listview will be automatically updated.

    i herewith attach sample file.
    Attached Files Attached Files

  2. #2
    Administrator Excel Fox's Avatar
    Join Date
    Mar 2011
    Posts
    1,402
    Rep Power
    10
    One way to do this is to remove the ListItems as well as the ColumnHeaders, and populate the ListView again.

    For your specific case, just replace the first two procedures in your userform module

    Code:
    'insert records in worksheet
    Private Sub CommandButton1_Click()
        Dim r As Long
        Dim m As Long
        Dim rngList As Range
        If Me.TextBox1 = "" Or Me.TextBox2 = "" Or Me.TextBox3 = "" Then
            MsgBox "Please enter All records", vbExclamation
            Exit Sub
        End If
        Set rngList = Worksheets("Sheet1").Range("Listm")
        m = rngList.Rows.Count
        rngList.Cells(m + 1, 2) = Me.TextBox1
        rngList.Cells(m + 1, 3) = Me.TextBox2
        rngList.Cells(m + 1, 4) = Me.TextBox3
        'rngList.Cells(m + 1, 5) = Me.TextBox4
        'Unload Me
    Sheet1.Cells(1, 1).Select
    Sheet1.Cells(2, 1).Select
    LoadListView
    
    End Sub
    
    Private Sub LoadListView()
        'Declare the variables
        Dim wksSource As Worksheet
        Dim rngData As Range
        Dim rngCell As Range
        Dim LstItem As ListItem
        Dim RowCount As Long
        Dim ColCount As Long
        Dim i As Long
        Dim j As Long
        'Set the source worksheet
        Me.ListView1.ListItems.Clear
        Me.ListView1.ColumnHeaders.Clear
        Set wksSource = Worksheets("Sheet1")
            'Set the source range
        Set rngData = wksSource.Range("A1").CurrentRegion
            'Add the column headers
        For Each rngCell In rngData.Rows(1).Cells
            Me.ListView1.ColumnHeaders.Add Text:=rngCell.Value, Width:=90
        Next rngCell
            'Count the number of rows in the source range
        RowCount = rngData.Rows.Count
            'Count the number of columns in the source range
        ColCount = rngData.Columns.Count
            'Fill the ListView
        For i = 2 To RowCount
            Set LstItem = Me.ListView1.ListItems.Add(Text:=rngData(i, 1).Value)
            For j = 2 To ColCount
                LstItem.ListSubItems.Add Text:=rngData(i, j).Value
            Next j
        Next i
        
    End Sub
    A dream is not something you see when you are asleep, but something you strive for when you are awake.

    It's usually a bad idea to say that something can't be done.

    The difference between dream and aim, is that one requires soundless sleep to see and the other requires sleepless efforts to achieve

    Join us at Facebook

  3. #3
    Junior Member
    Join Date
    Apr 2012
    Posts
    21
    Rep Power
    0
    Thank u so much for your time & help !

  4. #4
    Senior Member
    Join Date
    Jun 2012
    Posts
    337
    Rep Power
    13
    I think you might do with much less & much simpler code as well
    Attached Files Attached Files

  5. #5
    Administrator Excel Fox's Avatar
    Join Date
    Mar 2011
    Posts
    1,402
    Rep Power
    10
    Thanks snb. Can you post the code, so that users don't have to necessarily download the file.
    A dream is not something you see when you are asleep, but something you strive for when you are awake.

    It's usually a bad idea to say that something can't be done.

    The difference between dream and aim, is that one requires soundless sleep to see and the other requires sleepless efforts to achieve

    Join us at Facebook

Similar Threads

  1. Replies: 5
    Last Post: 07-21-2014, 08:08 AM
  2. Replies: 10
    Last Post: 10-17-2013, 07:36 PM
  3. Replies: 14
    Last Post: 06-27-2013, 10:57 AM
  4. set ListView on userform
    By Mahesh in forum Excel Help
    Replies: 3
    Last Post: 12-04-2012, 08:50 AM
  5. Volatile Function To Update Or Refresh Only Once
    By StevenC in forum Excel Help
    Replies: 2
    Last Post: 05-11-2012, 03:43 PM

Posting Permissions

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