Writing currency to worksheet

Pacman52

Active Member
Joined
Jan 29, 2009
Messages
428
Office Version
  1. 365
Platform
  1. Windows
Hi all. I have a test userform called Userform2

On the userform I have the a textbox called txbAmount. once a user has entered an amount into the textbox I want it to show as the UK currency. I have accomplished this with the following After Update code.

VBA Code:
 txbAmount = Format(txbAmount.text, "£#,##0.00")

I then want to write it to a worksheet table so that when its written Excel see's it as a currency and allows me to reference the cell for calculations ect. This is where I am getting stuck as no matter what I've tried It always writes the amount as text and indents it to the left on the worksheet.

I've tried so many different ways to get this to write to the sheet as currency NumberFormat, .value, etc but I've actually forgotten what I've tried and become over confused, so the code below is what I started with (taken from a backup of the workbook)

VBA Code:
Private Sub cmbWrite_Click()

 Dim newRow As Long
    With sh1Test

    newRow = .Cells(.Rows.Count, 1).End(xlUp).row + 1

    .Cells(newRow, 8).Value = txbAmount.Value
     
    End With

End Sub

(There are other textboxes on the userform date's, notes etc that are written to the sheet in various columns hence this line writing to Col 8)

Would someone be able to let me know what I'm doing wrong please and how to get whatever amount a user enters on the userform to show on the worksheet as currency.

Many thanks


Paul
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
The issue is that the VBA FORMAT function is similar to the Excel TEXT function, and it returns a text result (where those currency signs are hard-coded characters in the string).
You do NOT want to use the FORMAT function. You simply want to write the numeric value to the cell, and apply cell formatting to get it to display with the currency symbols, but remain a number.

So to format the cell, use a line like this:
VBA Code:
.Cells(newRow, 8).NumberFormat = "£#,##0.00"
 
Upvote 0
Hi,
when posting to the range you can use a Type Conversion Function to coerce text to the required data type

Rich (BB code):
 .Cells(newRow, 8).Value = CCur(txbAmount.Value)

Just be mindful that the function will error if the value entered is not of a valid type.

Dave
 
Upvote 0
Thanks Joe & Dave for the replies. I will adjust the code when I'm back on Monday and let you know the out come.

Paul
 
Upvote 0
I should have added that CCur recognises currency symbols as well as thousand’s separators embedded in a currency string.
you should therefore, be able to continue to include the currency symbol in your textbox if wanted.

Dave
 
Upvote 0

Forum statistics

Threads
1,224,817
Messages
6,181,147
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