Como mantener en numeros decimales una base de datos usando "userform"

Tengo esta macro que utilizo, para actualizar y para añadir a una base de datos, el problema que tengo es, cuando actualizo un dato. Los demás datos se vuelven numero enteros, yo quisiera que se mantenga como son (en decimales)

Gracias ...

Public control As Integer
Public filalibre As String
Public ubica As String

Private Sub CommandButton1_Click()
Sheets("hoja1").Select
If control > 0 Then
'actulizar datos
Range(ubica).Value = TextBox1
Range(ubica).Offset(0, 1).Value = TextBox2
Range(ubica).Offset(0, 2).Value = Format(Val(TextBox3), "#.0")
Range(ubica).Offset(0, 3).Value = Val(TextBox4)
Range(ubica).Offset(0, 4).Value = Val(TextBox5)
Range(ubica).Offset(0, 6).Value = Val(TextBox6)
Range(ubica).Offset(0, 8).Value = Val(TextBox7)
Range(ubica).Offset(0, 10).Value = Val(TextBox8)
Range(ubica).Offset(0, 12).Value = Val(TextBox9)
Range(ubica).Offset(0, 18).Value = Val(TextBox10)
Range(ubica).Offset(0, 16).Value = Val(TextBox11)
Range(ubica).Offset(0, 14).Value = Val(TextBox12)
Range(ubica).Offset(0, 20).Value = Val(TextBox13)
Range(ubica).Offset(0, 22).Value = Val(TextBox14)
Range(ubica).Offset(0, 24).Value = Val(TextBox15)
Range(ubica).Offset(0, 27).Value = Val(TextBox16)
Range(ubica).Offset(0, 26).Value = Val(TextBox17)
Range(ubica).Offset(0, 29).Value = Val(TextBox18)

Range(ubica).Offset(0, 7).Value = Val(TextBox19)
Range(ubica).Offset(0, 9).Value = Val(TextBox20)
Range(ubica).Offset(0, 11).Value = Val(TextBox21)
Range(ubica).Offset(0, 13).Value = Val(TextBox22)
Range(ubica).Offset(0, 15).Value = Val(TextBox23)
Range(ubica).Offset(0, 17).Value = Val(TextBox24)
Range(ubica).Offset(0, 19).Value = Val(TextBox25)
Range(ubica).Offset(0, 28).Value = Val(TextBox26)
Range(ubica).Offset(0, 21).Value = Val(TextBox27)
Range(ubica).Offset(0, 23).Value = Val(TextBox28)
Range(ubica).Offset(0, 25).Value = Val(TextBox29)
Range(ubica).Offset(0, 5).Value = Val(TextBox30)
control = 0
Else
' crear nuevos datos
Cells(filalibre, 1).Value = TextBox1
Cells(filalibre, 2).Value = TextBox2
Cells(filalibre, 3).Value = CCur(TextBox3)
Cells(filalibre, 4).Value = CCur(TextBox4)
Cells(filalibre, 5).Value = Val(TextBox5)
Cells(filalibre, 7).Value = Val(TextBox6)
Cells(filalibre, 9).Value = Val(TextBox7)
Cells(filalibre, 11).Value = Val(TextBox8)
Cells(filalibre, 13).Value = Val(TextBox9)
Cells(filalibre, 19).Value = Val(TextBox10)
Cells(filalibre, 17).Value = Val(TextBox11)
Cells(filalibre, 15).Value = Val(TextBox12)
Cells(filalibre, 21).Value = Val(TextBox13)
Cells(filalibre, 23).Value = Val(TextBox14)
Cells(filalibre, 25).Value = Val(TextBox15)
Cells(filalibre, 28).Value = Val(TextBox16)
Cells(filalibre, 27).Value = Val(TextBox17)
Cells(filalibre, 30).Value = Val(TextBox18)

Cells(filalibre, 8).Value = Val(TextBox19)
Cells(filalibre, 10).Value = Val(TextBox20)
Cells(filalibre, 12).Value = Val(TextBox21)
Cells(filalibre, 14).Value = Val(TextBox22)
Cells(filalibre, 16).Value = Val(TextBox23)
Cells(filalibre, 18).Value = Val(TextBox24)
Cells(filalibre, 20).Value = Val(TextBox25)
Cells(filalibre, 29).Value = Val(TextBox26)
Cells(filalibre, 22).Value = Val(TextBox27)
Cells(filalibre, 24).Value = Val(TextBox28)
Cells(filalibre, 26).Value = Val(TextBox29)
Cells(filalibre, 6).Value = Val(TextBox30)

End If
TextBox1 = Empty
TextBox2 = Empty
TextBox3 = Empty
TextBox4 = Empty
TextBox5 = Empty
TextBox6 = Empty
TextBox7 = Empty
TextBox8 = Empty
TextBox9 = Empty
TextBox10 = Empty
TextBox11 = Empty
TextBox12 = Empty
TextBox13 = Empty
TextBox14 = Empty
TextBox15 = Empty
TextBox16 = Empty
TextBox17 = Empty
TextBox18 = Empty
TextBox1.SetFocus
End Sub

Private Sub CommandButton2_Click()
Unload UserForm1
End Sub

Private Sub CommandButton3_Click()
TextBox1 = Empty
TextBox2 = Empty
TextBox3 = Empty
TextBox4 = Empty
TextBox5 = Empty
TextBox6 = Empty
TextBox7 = Empty
TextBox8 = Empty
TextBox9 = Empty
TextBox10 = Empty
TextBox11 = Empty
TextBox12 = Empty
TextBox13 = Empty
TextBox14 = Empty
TextBox15 = Empty
TextBox16 = Empty
TextBox17 = Empty
TextBox18 = Empty
TextBox19 = Empty
TextBox20 = Empty
TextBox21 = Empty
TextBox22 = Empty
TextBox23 = Empty
TextBox24 = Empty
TextBox25 = Empty
TextBox26 = Empty
TextBox27 = Empty
TextBox28 = Empty
TextBox29 = Empty
TextBox30 = Empty

TextBox1.SetFocus
End Sub

Private Sub TextBox1_AfterUpdate()
Sheets("hoja1").Select
filalibre = Range("a5").End(xlDown).Offset(1, 0).Row
' la variable filalibre guarda el nro, de la primer celda vacia.
control = 0
dato = TextBox1
rango = "a5:a" & filalibre
Set midato = ActiveSheet.Range(rango).Find(dato, LookIn:=xlValues, lookat:=xlWhole)
If (midato) Is Nothing Then
MsgBox "NO EXISTE LA FUNDACION"
Else
ubica = midato.Address(False, False)
TextBox2.Value = Range(ubica).Offset(0, 1).Value
TextBox3.Value = Range(ubica).Offset(0, 2).Value
TextBox4.Value = Range(ubica).Offset(0, 3).Value
TextBox5.Value = Range(ubica).Offset(0, 4).Value
TextBox6.Value = Range(ubica).Offset(0, 6).Value
TextBox7.Value = Range(ubica).Offset(0, 8).Value
TextBox8.Value = Range(ubica).Offset(0, 10).Value
TextBox9.Value = Range(ubica).Offset(0, 12).Value
TextBox10.Value = Range(ubica).Offset(0, 18).Value
TextBox11.Value = Range(ubica).Offset(0, 16).Value
TextBox12.Value = Range(ubica).Offset(0, 14).Value
TextBox13.Value = Range(ubica).Offset(0, 20).Value
TextBox14.Value = Range(ubica).Offset(0, 22).Value
TextBox15.Value = Range(ubica).Offset(0, 24).Value
TextBox16.Value = Range(ubica).Offset(0, 27).Value
TextBox17.Value = Range(ubica).Offset(0, 26).Value
TextBox18.Value = Range(ubica).Offset(0, 29).Value

TextBox19.Value = Range(ubica).Offset(0, 7).Value
TextBox20.Value = Range(ubica).Offset(0, 9).Value
TextBox21.Value = Range(ubica).Offset(0, 11).Value
TextBox22.Value = Range(ubica).Offset(0, 13).Value
TextBox23.Value = Range(ubica).Offset(0, 15).Value
TextBox24.Value = Range(ubica).Offset(0, 17).Value
TextBox25.Value = Range(ubica).Offset(0, 19).Value
TextBox26.Value = Range(ubica).Offset(0, 28).Value
TextBox27.Value = Range(ubica).Offset(0, 21).Value
TextBox28.Value = Range(ubica).Offset(0, 23).Value
TextBox29.Value = Range(ubica).Offset(0, 25).Value
TextBox30.Value = Range(ubica).Offset(0, 5).Value

control = 1
MsgBox "CARGANDO"
End If
Set midato = Nothing

End Sub

1 respuesta

Respuesta
1

Conozco parte de este código... cuando sepas el autor de las macros debieras dejar su nombre.

Cuando los valores deben guardarse como decimales utiliza la función CDBL en lugar de VAL. Te dejo parte del código con un cambio. Por supuesto que también debieras ya tener la hoja formateada con la cantidad de decimales deseados así no necesitas la función FORMAT que veo en otra línea.

Private Sub CommandButton1_Click()

'x Elsamatilde
Sheets("hoja1").Select
If control > 0 Then
'actulizar datos
Range(ubica).Value = TextBox1
Range(ubica).Offset(0, 1).Value = TextBox2
Range(ubica).Offset(0, 2).Value = Format(Val(TextBox3), "#.0")
Range(ubica).Offset(0, 3).Value = CDBL(TextBox4)            'se guarda como decimal
Range(ubica).Offset(0, 4).Value = Val(TextBox5)

* En la página 6 de la sección Macros de mi sitio encontrarás algunos ejemplos de bucles... podrías utilizar uno para limpiar todos los textbox. Sino lo podés resolver deja una nueva consulta para ese tema. Indica en el cuerpo del mensaje que es Para Elsa.

Sdos y no olvides valorar la respuesta (excelente o buena).

Estimada Elsa cambien el código según como me indicaste pero no funciona....

El macro lo realice viendo un vídeo en youtube el que lo explicaba se llama  "DOSTIN HURTADO"  

como se ve en la imagen lo utilizo a para añadir  datos o actulizar. el problema que tengo es cunado actualizo.  busco el nombe encuentra el nombre, cuando cambio un dato o modifico, " los de mas datos se vuelven  numeros enteros" . quisiera que solo se modifique el dato que estoy modificando, y que los demás mantengan sus valores y no se vuelvan números enteros.

Gracias ......

        

Este fue un ejemplo que hace un par de años dejé en mi sitio ... se llamaba 'Interactuando con userforms'.

Bien, lo que observo en la imagen es que tus datos tienen punto en lugar de coma decimal. Por lo tanto es muy posible que tengas un conflicto en el tema 'separadores'.

Te explico: Excel toma los formatos que se encuentran definidos en la Configuración Regional de Windows. Y allí lo correcto es no repetir un mismo separador para 2 o más items.

Por ej, en la imagen se observan los 3 más comunes y son diferentes entre sí. Es posible que en tu caso tengas 'coma' para los miles y 'punto' para los decimales... en ese caso también estaría correcto si para 'listas' utilizas punto y coma.

Revisa y ajusta este detalle en el Panel de control de Windows.

Luego queda el tema del teclado. ¿Los decimales los ingresas con punto o coma?

Si no logras resolver el problema podés enviarme tu libro para que lo revise. Mis correos aparecen en la portada del sitio que dejo al pie.

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas