+ Reply to Thread
Results 1 to 16 of 16

Adding currency format to ListView columns

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    08-29-2006
    Location
    London
    MS-Off Ver
    Excel 2016
    Posts
    314

    Adding currency format to ListView columns

    Hi all

    I have a Userfrom with a List View, below code loads list view items from an excel sheet, it has 24 columns

    how do I add currency format below code that List view columns 7,8,9,10,11,12,13,14 shows as currency

    any help will be apricated

    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
        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

    many thanks in advance

  2. #2
    Forum Expert
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    O365 32bit (Windows)
    Posts
    2,047

    Re: Adding currency format to ListView columns

    Format the value before adding it.


    Set LstItem = Me.ListView1.ListItems.Add(Text:=Format(rngData(i, 1).Value, "currency"))
    or

    Set LstItem = Me.ListView1.ListItems.Add(Text:=Format(rngData(i, 1).Value, "#,##0.00"))
    Same with the ListSubItems

  3. #3
    Forum Contributor
    Join Date
    08-29-2006
    Location
    London
    MS-Off Ver
    Excel 2016
    Posts
    314

    Re: Adding currency format to ListView columns

    thank you for your reply, I have added your suggestion, but it hasn't worked

    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
        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:=Format(rngData(i, 1).Value, "#,##0.00"))
            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

  4. #4
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,282

    Re: Adding currency format to ListView columns

    The line in bold replaces the line below it.
    Remember what the dormouse said
    Feed your head

  5. #5
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,592

    Re: Adding currency format to ListView columns

    If data in those columns is formatted as currency, try change all .Value to .Text

  6. #6
    Forum Contributor
    Join Date
    08-29-2006
    Location
    London
    MS-Off Ver
    Excel 2016
    Posts
    314

    Re: Adding currency format to ListView columns

    Quote Originally Posted by jindon View Post
    If data in those columns is formatted as currency, try change all .Value to .Text
    only yellow highlighted should have currency values the others date or some references

    Capture.PNG

  7. #7
    Forum Contributor
    Join Date
    08-29-2006
    Location
    London
    MS-Off Ver
    Excel 2016
    Posts
    314

    Re: Adding currency format to ListView columns

    I tries this one and it didn't work either, sorry to be a pain

    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
        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)
            
            Set LstItem = Me.ListView1.ListItems.Add(Text:=Format(rngData(i, 1).Value, "#,##0.00"))
            
            For j = 2 To ColCount
                 
                LstItem.ListSubItems.Add Text:=rngData(i, j).Value
                 
            Next j
        Next i
       
          
    End Sub

  8. #8
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,592

    Re: Adding currency format to ListView columns

    I meant to say the data in those columns in the worksheet.

    If the columns are formatted as you want to see on list view, .Text property should appear as what you see on the worksheet.

  9. #9
    Forum Contributor
    Join Date
    08-29-2006
    Location
    London
    MS-Off Ver
    Excel 2016
    Posts
    314

    Re: Adding currency format to ListView columns

    Quote Originally Posted by jindon View Post
    I meant to say the data in those columns in the worksheet.

    If the columns are formatted as you want to see on list view, .Text property should appear as what you see on the worksheet.
    This is how the excel sheet look behind the user form

    Attachment 805564

  10. #10
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,592

    Re: Adding currency format to ListView columns

    Your attachement is invalid, so can not open.

  11. #11
    Forum Contributor
    Join Date
    08-29-2006
    Location
    London
    MS-Off Ver
    Excel 2016
    Posts
    314

    Re: Adding currency format to ListView columns

    Quote Originally Posted by jindon View Post
    Your attachement is invalid, so can not open.
    sorry here it is again

    Capture2.PNG

  12. #12
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,592

    Re: Adding currency format to ListView columns

    Did you change all the .Value property to .Text in the code posted in your initial post #1?

    If so and doesn't work, I need to see your workbook.

  13. #13
    Forum Contributor
    Join Date
    08-29-2006
    Location
    London
    MS-Off Ver
    Excel 2016
    Posts
    314

    Re: Adding currency format to ListView columns

    here is the workbook

    thanks
    Attached Files Attached Files

  14. #14
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,592

    Re: Adding currency format to ListView columns

    ListView control doen't seem to accept special characters.
    So, if you can change it to ListBox, it is lot easier.

    example
    Private Sub UserForm_Initialize()
        With Sheets("sheet1").ListObjects(1)
            Me.ListBox1.ColumnCount = .ListColumns.Count
            Me.ListBox1.ColumnHeads = True
            Me.ListBox1.RowSource = .DataBodyRange.Address
        End With
    End Sub
    Attached Files Attached Files

  15. #15
    Forum Contributor
    Join Date
    08-29-2006
    Location
    London
    MS-Off Ver
    Excel 2016
    Posts
    314

    Re: Adding currency format to ListView columns

    this code worked on ListView

    thank you very much for your time nad Thank you to ByteMarks too

    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, s: s = ActiveCell
        
        'Set the source worksheet
        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
                If (j > 6) * (j < 15) Then
                    LstItem.ListSubItems.Add Text:=VBA.Format$(rngData(i, j).Value, """" & VBA.ChrW(163) & """#,##0.00")
                Else
                    LstItem.ListSubItems.Add Text:=rngData(i, j).Value
                End If
            Next j
        Next i

  16. #16
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,592

    Re: Adding currency format to ListView columns

    That was my attempt but it doesn't work here, so I didn't mention.

    Perhaps it is the difference of local settings...

    That's good if it is working at your end anyway.
    Last edited by jindon; 11-18-2022 at 10:27 AM.

+ 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] Format Listview Columns to Currency
    By Andrew Andromeda in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-26-2016, 10:28 AM
  2. Adding currency broken in two columns?
    By Blue Hornet in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 09-06-2005, 07:05 PM
  3. [SOLVED] Adding currency broken in two columns?
    By lperisich in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2005, 06:05 PM
  4. Adding currency broken in two columns?
    By lperisich in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2005, 03:05 PM
  5. Adding currency broken in two columns?
    By lperisich in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2005, 10:05 AM
  6. Adding currency broken in two columns?
    By lperisich in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2005, 03:05 AM
  7. Adding currency broken in two columns?
    By lperisich in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2005, 01:05 AM

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