amending code format number in listbox

abdelfattah

New member
Joined
Jul 17, 2020
Messages
10
Reaction score
0
Points
0
Excel Version(s)
2010
hello
I have this code what I want show the last column in listbox contains values like this $1,345,444.00 , $1,000.00 , $100.00 I know this formula format listbox(i,5)=format(listbox(i,5),"$#,##0.00) but I no know where exactly adjusting
Code:
Option Explicit

Private Sub CommandButton1_Click()
    Dim i As Long, myCol As Long, temp, txt As String, x, a, msg As String
    If Me.TextBox1 = "" Then Exit Sub
    If Me.ComboBox1.Value = "" Then msg = "Select sheet first"
    For i = 1 To 5
        If Me("optionbutton" & i) Then
            myCol = i + 1: Exit For
        End If
    Next
    If myCol = 0 Then msg = msg & vbLf & "Select one of option buttons"
    If Len(msg) Then MsgBox msg, vbCritical: Exit Sub
    If myCol = 6 Then
        myCol = 1
        txt = "month(" & Chr(2) & ")"
    Else
        txt = Chr(2)
    End If
    Me.ListBox1.Clear
    temp = Me.TextBox1
    If Not IsNumeric(temp) Then temp = Chr(34) & temp & Chr(34)
    If myCol Then
        With Sheets(Me.ComboBox1.Value).Cells(1).CurrentRegion.Offset(1)
            txt = Replace(txt, Chr(2), .Columns(myCol).Address)
            Me.ListBox1.ColumnCount = .Columns.Count
            x = Filter(.Parent.Evaluate("transpose(if(" & txt & "=" & _
            temp & ",row(1:" & .Rows.Count & ")))"), False, 0)
            If UBound(x) = 0 Then
                Me.ListBox1.Column = Application.Index(.Value, x(0), 0)
                Me.TextBox2.Value = Application.Index(.Value, x(0), 6)
            ElseIf UBound(x) > 0 Then
                Me.ListBox1.List = Application.Index(.Value, Application.Transpose(x), _
                    Evaluate("column(" & .Address & ")"))
                Me.TextBox2.Value = Application.Sum(Application.Index(.Value, Application.Transpose(x), 6))
            End If
        End With
    End If
I hope somebody help
thanks
 

Attachments

  • code (1).xlsm
    36.7 KB · Views: 10
Last edited:
Code:
Private Sub CommandButton1_Click()
Dim i As Long, myCol As Long, temp, txt As String, x, a, msg As String, zzz
If Me.TextBox1 = "" Then Exit Sub
If Me.ComboBox1.Value = "" Then msg = "Select sheet first"
For i = 1 To 5
  If Me("optionbutton" & i) Then
    myCol = i + 1: Exit For
  End If
Next
If myCol = 0 Then msg = msg & vbLf & "Select one of option buttons"
If Len(msg) Then MsgBox msg, vbCritical: Exit Sub
If myCol = 6 Then
  myCol = 1
  txt = "month(" & Chr(2) & ")"
Else
  txt = Chr(2)
End If
Me.ListBox1.Clear
temp = Me.TextBox1
If Not IsNumeric(temp) Then temp = Chr(34) & temp & Chr(34)
If myCol Then
  With Sheets(Me.ComboBox1.Value).Cells(1).CurrentRegion.Offset(1)
    txt = Replace(txt, Chr(2), .Columns(myCol).Address)
    Me.ListBox1.ColumnCount = .Columns.Count
    x = Filter(.Parent.Evaluate("transpose(if(" & txt & "=" & _
                                temp & ",row(1:" & .Rows.Count & ")))"), False, 0)
    If UBound(x) = 0 Then
      Me.ListBox1.Column = Application.Index(.Value, x(0), 0)
      Me.TextBox2.Value = Application.Index(.Value, x(0), 6)
    ElseIf UBound(x) > 0 Then
      zzz = Application.Index(.Value, Application.Transpose(x), _
                              Evaluate("column(" & .Address & ")"))
      For i = 1 To UBound(zzz)
        zzz(i, 6) = Format(zzz(i, 6), "$#,##0.00")
      Next i
      Me.ListBox1.List = zzz    'Application.Index(.Value, Application.Transpose(x), _
                                Evaluate("column(" & .Address & ")"))
      Me.TextBox2.Value = Application.Sum(Application.Index(.Value, Application.Transpose(x), 6))
    End If
  End With
End If
End Sub
 
awesome buddy ! i have something so as you know $ this shows in listbox without problem but if i enter local currancy it doesnt the numbers correctly i try make like this "LYD#,##0.00"
if this is not possible can i show in listbox based what writing in column 6 in sheet like this LYD 1,200,300.00
 
try:
Code:
        zzz(i, 6) = Application.Text(zzz(i, 6), "[$LYD] #,##0.00")
 
you're legend many thanks it's solved :clap2::clap2:
 
Back
Top