How to input decimal places with this userform

kandso

New Member
Joined
Jun 29, 2019
Messages
7
Hi,

New here and I took over using a workbook at my company and I'm trying to learn VBA to make some changes. One of the issues is with this userform code that we use to add part order quantities, it works great except for it only inputs a whole number in the cells. As an example if I input 2.5 as a quantity it will round it to 3 and input 3.00. It's not a formatting issue, as the cells are formatted to out put two decimal places.

I've been trying to change the textbox1 values but have not had any luck getting it to input decimals into the sheet. Any help getting the code to do this would be greatly appreciated.

Code:
<code style="margin: 0px; padding: 0px; font-style: inherit; font-weight: inherit; line-height: 12px;">Option Explicit
Private Sub UserForm_Initialize()
    
    Dim rngSource As Range
    Dim rngMyCell As Range
    Dim wsMySheet As Worksheet
    
    Set wsMySheet = Sheets("PRODUCTs")
    
    wsMySheet.Range("$F$5:$F$" & wsMySheet.Cells(Rows.Count, "F").End(xlUp).Row).AutoFilter Field:=1, Criteria1:="<>", Operator:=xlFilterValues
    Set rngSource = wsMySheet.Range("F5:F" & wsMySheet.Cells(Rows.Count, "F").End(xlUp).Row).SpecialCells(xlCellTypeVisible)
    
    ComboBox1.Clear
    
    For Each rngMyCell In rngSource
        ComboBox1.AddItem rngMyCell.Value
    Next rngMyCell
    
    wsMySheet.AutoFilterMode = False 'Remove above filter

End Sub
Private Sub ComboBox1_Change()

    Call SetQTY(False)

End Sub
Private Sub CommandButton1_Click()

    Call SetQTY(True)
    
End Sub
Sub SetQTY(blnOutputToSheet As Boolean)

    Dim rngFound As Range
    Dim wsMySheet As Worksheet
    Dim lngLastRow As Long
    
    Set wsMySheet = Sheets("PRODUCTs")
    lngLastRow = wsMySheet.Cells(Rows.Count, "F").End(xlUp).Row
    Set rngFound = wsMySheet.Range("F5:F" & lngLastRow).Find(What:=CStr(ComboBox1.List(ComboBox1.ListIndex)), LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
        
    If ComboBox1.ListIndex <> -1 And Not rngFound Is Nothing Then
        If blnOutputToSheet = True Then
            rngFound.Offset(0, 1).Value = CLng(TextBox1.Value)
        Else
            TextBox1.Value = CLng(rngFound.Offset(0, 1))
        End If
    End If

End Sub</code>
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Code:
<code style="margin: 0px; padding: 0px; font-style: inherit; font-weight: inherit; line-height: 12px;">Sub SetQTY(blnOutputToSheet As Boolean)

    Dim rngFound As Range
    Dim wsMySheet As Worksheet
    Dim lngLastRow As Long
    
    Set wsMySheet = Sheets("PRODUCTs")
    lngLastRow = wsMySheet.Cells(Rows.Count, "F").End(xlUp).Row
    Set rngFound = wsMySheet.Range("F5:F" & lngLastRow).Find(What:=CStr(ComboBox1.List(ComboBox1.ListIndex)), LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
        
    If ComboBox1.ListIndex <> -1 And Not rngFound Is Nothing Then
        If blnOutputToSheet = True Then
            rngFound.Offset(0, 1).Value = [COLOR=#0000ff]CDbl[/COLOR](TextBox1.Value)
        Else
            TextBox1.Value = [COLOR=#0000ff]CDbl[/COLOR](rngFound.Offset(0, 1))
        End If
    End If

End Sub</code>
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,747
Messages
6,180,719
Members
452,995
Latest member
isldboy

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top