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
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
En Excel 2000 este código funciona bien:

<font face=Courier New>
<SPAN style="color:#00007F">Sub</SPAN> Test()
    <SPAN style="color:#00007F">Dim</SPAN> datos <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>
    datos = Sheets("hoja1").Cells(1, 1).Value
    <SPAN style="color:#00007F">Dim</SPAN> a <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>
    a = Left(datos, 4)
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>
</FONT>

Que versión de Excel se está utilizando ?
 
Upvote 0
Hola,

A mi me funciono esto:

Dim a As String
a = Sheets("sheet1").Cells(1, 1).Value
ActiveCell.Value = Left(a, 5)

Creo que el problema esta en las dos variables: tienes "datos" para la celda a1 y "a" como parte de la funcion left.

Espero y esto sea de ayuda.

Suerte.

Ben
 
Upvote 0
Perdonad a todos en vez de:

Dim datos As String
datos = Sheets("hoja1").Cells(1, 1).Value
Dim a As String
a = Left(a, 4)

quería decir :

Dim datos As String
datos = Sheets("hoja1").Cells(1, 1).Value
Dim a As String
a = Left(datos, 4)


tengo XP, y no me está funcionando
 
Upvote 0
A mi me funciono este:

bmacias said:
Hola,

A mi me funciono esto:

Dim a As String
a = Sheets("sheet1").Cells(1, 1).Value
ActiveCell.Value = Left(a, 5)

Creo que el problema esta en las dos variables: tienes "datos" para la celda a1 y "a" como parte de la funcion left.

Espero y esto sea de ayuda.

Suerte.

Ben

y este:
Code:
Sub x()

Dim rngCell As Range
Dim strCell As String
Dim intMaxChar As Integer

intMaxChar = 4
For Each rngCell In Selection
strCell = rngCell.Value
If Len(strCell) <= intMaxChar Then GoTo 1
rngCell = Left(strCell, intMaxChar)
1 Next rngCell

End Sub

pero en el ultimo tengo que seleccionar la celda y cambiarla a formato de texto para que aparezca el 0.

Espero te sirva de algo
 
Upvote 0
El contenido de A1 si es

0123ABC SEAT AMARILLO

o la celda tiene algun formato en particular que hace que ese sea el texto que se "muestra" pero no el que "es" ?? mejor dicho... qué se ve en la barra de fórmulas al seleccionar A1 ?
 
Upvote 0
En XP este también me funcionó bien:

<font face=Courier New>
<SPAN style="color:#00007F">Sub</SPAN> Test()
    <SPAN style="color:#00007F">Dim</SPAN> datos <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>
    datos = Sheets("Hoja1").Cells(1, 1).Value
    <SPAN style="color:#00007F">Dim</SPAN> a <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>
    a = Left(datos, 4)
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>
</FONT>

pero bueno, intentemos cosas... este sirve ?

<font face=Courier New>
<SPAN style="color:#00007F">Sub</SPAN> Test2()
    <SPAN style="color:#00007F">Dim</SPAN> datos <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>
    datos = Sheets("Hoja1").Cells(1, 1).Text
    <SPAN style="color:#00007F">Dim</SPAN> a <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>
    a = Left$(datos, 4)
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>
</FONT>
 
Upvote 0
Perfecto, ahora si me lo coge bien, que tiene que ver el $ para que me lo coja?

Muchas gracias
 
Upvote 0

Forum statistics

Threads
1,216,144
Messages
6,129,120
Members
449,488
Latest member
qh017

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