How to convert textbox to number and date in VBA

Asked

Viewed 2,299 times

0

I have a vba in Excel, however the command saves the entries as text, so it is not possible to apply any formula. I’ve done some trials and research but all of them were failed, follow the codes:

Private Sub CommandButton_gravar_Click()
Dim valor As Currency
Dim data As Date
valor = TextBox_valor
data = TextBox_data
Range("b11").Select
If Range("b12").Value <> "" Then
Selection.End(xlDown).Select
End If
ActiveCell.Offset(1, 0).Select
Selection.Value = TextBox_data
ActiveCell.Offset(0, 1).Select
Selection.Value = ComboBox_categoria
ActiveCell.Offset(0, 1).Select
Selection.Value = TextBox_historico
ActiveCell.Offset(0, 1).Select
Selection.Value = ComboBox_movimento
ActiveCell.Offset(0, 1).Select
Selection.Value = TextBox_valor
Unload UserForm_lançamento

1 answer

2

I believe that what is missing is to change the cell format. In Excel the default format is text.

  1. After the line: Selection.Value = TextBox_data; insert: Selection.NumberFormat = "yyyy-mm-dd;@"
  2. After the line: Selection.Value = TextBox_valor; insert: Selection.NumberFormat = "$#,##0.00"

For more information, take a look here: https://www.excelhowto.com/macros/formatting-a-range-of-cells-in-excel-vba/

  • Thank you very much but the proposal did not work, which I managed to solve the problem by adding a conversion: Selection.Value = Ccur(Textbox_value)

  • Glad you found a solution! Thanks for sharing.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.