Cdbl textBox showing as Text Error in Worksheet

Denny57

Board Regular
Joined
Nov 23, 2015
Messages
246
Office Version
  1. 365
Platform
  1. Windows
I have an issue which has arisen on a number of files during the past few days. I am now getting error messages next to each worksheet cell advising that the entry of the cell is as text, even when the code sends the content as cDbl or currency. This has only happened since a recent Windows Update which I think may have reset a default value.
Consequently, I have needed to revert to long-hand code to get around this. This is ok when I only have a few textboxes, but I have some where there are over 200 textboxes x 4 conditions.

If there is not a generic setting, then please could someone please advise how I might change the following codes to ensure the cells are populated in the correct format.

(Maybe one day MS might introduce a control dedicated to sending numeric values to the ws instead if needing code to achieve this, especially as Excel is used for calculations)

Code 1 - The ws cells this code refers to still show the error despite the inclusion of cDbl.

VBA Code:
Private Sub cmdAddRecord_Click()
'Used to add new transation records to the database

    Dim r As Long, sCredit As String, sDebit As String
    
    sDebit = Me.txtTransactionAmountDebit
    sCredit = Me.txtTransactionAmountCredit
    
    With Sheets("Spending Account")
        r = 1 + .Cells(.Rows.Count, "A").End(xlUp).Row
        .Cells(r, "A").Value = DTPicker1
        .Cells(r, "B").Value = cboVendorDetails
        .Cells(r, "C").Value = cboTransactionType
        .Cells(r, "F").Value = cboTransactionStatus
     
        ' credit or debit
        If Len(sDebit) > 0 Then
            If Len(sCredit) > 0 Then
                MsgBox "Warning - Both Credit and Debit", vbExclamation
            Else
                .Cells(r, "D").Value = CDbl(sDebit)
            End If
        ElseIf Len(sCredit) > 0 Then
           .Cells(r, "E").Value = CDbl(sCredit)
        End If
          
        If r > 21 Then
            Application.Goto Reference:=.Cells(r - 20, "A"), Scroll:=True
        End If
    End With

    'Unload Me
    'frmRegularTransactions.Show
    
    Call UserForm_Initialize
    
End Sub

Code 2 -Extract of the full code only

VBA Code:
Private Sub cmdUpdateEuromillionsResultRecords_Click()

    Set EuroToColsDict = CreateObject("Scripting.Dictionary")
    
        With EuroToColsDict
            Add "All", Array("B", "C", "D", "E", "F", "G", "H")
        End With

         With Me
                 vCols = EuroToColsDict(.cboLotteryAll.Value)
        Set ws = Sheets("Draw Information")
            tbCounter = 1
                For lngRowLoop = 19 To 22
                    For Each vCol In vCols
                        ws.Cells(lngRowLoop, vCol).Value = .Controls("txtEuromillionsResult" & tbCounter).Text
                        tbCounter = tbCounter + 1
                    Next
                Next
    End With
     MsgBox "Euromillions Records have been updated", 0, "Records Updated"
End Sub

In both instances I need the TextBox Values to be treated as either curreny or numeric values in the worksheet.

Many thanks
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Unfortunately, I ran into a similar issue like that. The only solution that I came up with was to set the number format after each statement. Like:
VBA Code:
           Else
                .Cells(r, "D").Value = CDbl(sDebit)
                .Cells(r, "D").NumberFormat = "0"
            End If
        ElseIf Len(sCredit) > 0 Then
           .Cells(r, "E").Value = CDbl(sCredit)
           .Cells(r, "E").NumberFormat = "0"
I hope it works for you, too.
 
Upvote 0
Unfortunately, I ran into a similar issue like that. The only solution that I came up with was to set the number format after each statement. Like:
VBA Code:
           Else
                .Cells(r, "D").Value = CDbl(sDebit)
                .Cells(r, "D").NumberFormat = "0"
            End If
        ElseIf Len(sCredit) > 0 Then
           .Cells(r, "E").Value = CDbl(sCredit)
           .Cells(r, "E").NumberFormat = "0"
I hope it works for you, too.
Thank you. This is easy to apply to the second example, however, I am unsure how I woudl apply this to the first code which uses arrays and not an individual column "D" or "E"
 
Upvote 0
I think you should apply like this:
VBA Code:
ws.Cells(lngRowLoop, vCol).Value = .Controls("txtEuromillionsResult" & tbCounter).Text
ws.Cells(lngRowLoop, vCol).NumberFormat = "0"
 
Upvote 0
Solution
A reminder:

Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!

Cross posted at:

There is no need to repeat the link(s) provided above but if you have posted the question at other places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,157
Members
453,021
Latest member
Justyna P

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