como poder reconocer ceros en una variable

Osasa54

New Member
Joined
Sep 10, 2002
Messages
31
Hola, en la celda A1 tengo :
0123ABC SEAT AMARILLO

Entonces en un modulo de VBA meto:
Dim datos as string
datos = Sheets("hoja1").Cells(1, 1).Value
Dim a as string
a = left(a, 4)

lo malo es que de este mod la variable a = 123A, y necesito que también me coja los ceros es decir, que a sea = 0123
Puedo ponerlo de algún modo para que me lo coja así?

utilizando
a = mid(a, 1, 4)
me pasa lo mismo

Muchas gracias
 
Mhmm, a ver si lo puedo explicar.

Cuando uno define una variable, por ejemplo (que casualidad lo que sigue... :p ), de tipo string, uno lo puede hacer así:

Dim a As String

O

Dim a$

La segunda metodología es muy utilizada en VB, pero, a mí, personalmente, me parece muy confusa ! porque nunca me acuerdo cuál símbolo es cual !!!

Ahora, VBA tiene dos funciones Left (Así como dos funciones Mid, o Right o Format, entre otras), Left y Left$

La primera devuelve una variable de tipo "Variant", lo que pasa es que normalmente es un string, así que VBA lo "traduce" de Variant a String.

La segunda devuelve de una vez un "String", así que no hay que realizar un paso intermedio (Así no lo haga uno !) para llevar la variable al tipo correcto...
 
Upvote 0

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.

Forum statistics

Threads
1,215,678
Messages
6,126,176
Members
449,296
Latest member
tinneytwin

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