Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default need help formatting text in macro

I have a spreadsheet that i scan barcodes into. Some of the barcodes are old
and omit the - in the serial number. I need to format the text to add it
back in. below is an example of the data. Also when the - is omitted it
reads the cell as text instead of general and omits the leading 0.

02-123456
2123456

You see no - and no leading 0. I need to add both back in. Can anyone give
me some help? I have tried a few things but i have another issue that breaks
all of them. Thanks.

dm.


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,494
Default need help formatting text in macro

you could also use the mid function

Sub test()
Dim ws As Worksheet
Dim lastrow As Long
Dim cell As Range
Set ws = Worksheets("sheet1")
lastrow = ws.Cells(Rows.Count, "A").End(xlUp).Row
For Each cell In ws.Range("A1:A" & lastrow)
If InStr(1, cell.Value, "-") = 0 Then
cell.Value = "0" & Left(cell.Value, 1) & "-" & _
Mid(cell.Value, 2, 30)
End If
Next
End Sub

--


Gary


"Gary Keramidas" <GKeramidasATmsn.com wrote in message
...
here's one way. i used sheet 1 and column A.

Sub test()
Dim ws As Worksheet
Dim lastrow As Long
Dim cell As Range
Set ws = Worksheets("sheet1")
lastrow = ws.Cells(Rows.Count, "A").End(xlUp).Row
For Each cell In ws.Range("A1:A" & lastrow)
If InStr(1, cell.Value, "-") = 0 Then
cell.Value = "0" & Left(cell.Value, 1) & "-" & _
Right(cell.Value, Len(cell.Value) - 1)
End If
Next
End Sub

--


Gary


"Daniel M" wrote in message
...
I have a spreadsheet that i scan barcodes into. Some of the barcodes are old
and omit the - in the serial number. I need to format the text to add it back
in. below is an example of the data. Also when the - is omitted it reads the
cell as text instead of general and omits the leading 0.

02-123456
2123456

You see no - and no leading 0. I need to add both back in. Can anyone give me
some help? I have tried a few things but i have another issue that breaks all
of them. Thanks.

dm.






  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,494
Default need help formatting text in macro

here's one way. i used sheet 1 and column A.

Sub test()
Dim ws As Worksheet
Dim lastrow As Long
Dim cell As Range
Set ws = Worksheets("sheet1")
lastrow = ws.Cells(Rows.Count, "A").End(xlUp).Row
For Each cell In ws.Range("A1:A" & lastrow)
If InStr(1, cell.Value, "-") = 0 Then
cell.Value = "0" & Left(cell.Value, 1) & "-" & _
Right(cell.Value, Len(cell.Value) - 1)
End If
Next
End Sub

--


Gary


"Daniel M" wrote in message
...
I have a spreadsheet that i scan barcodes into. Some of the barcodes are old
and omit the - in the serial number. I need to format the text to add it back
in. below is an example of the data. Also when the - is omitted it reads the
cell as text instead of general and omits the leading 0.

02-123456
2123456

You see no - and no leading 0. I need to add both back in. Can anyone give me
some help? I have tried a few things but i have another issue that breaks all
of them. Thanks.

dm.




  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10,124
Default need help formatting text in macro

try

Sub addzeroanddash()
mc = "f"
For i = 1 To Cells(Rows.Count, mc).End(xlUp).Row
If Left(Cells(i, mc), 1) < 0 Then
Cells(i, mc) = "0" & Mid(Cells(i, mc), 1, 1) _
& "-" & Right(Cells(i, mc), 6)
End If
Next i
End Sub

--
Don Guillett
Microsoft MVP Excel
SalesAid Software

"Daniel M" wrote in message
...
I have a spreadsheet that i scan barcodes into. Some of the barcodes are
old and omit the - in the serial number. I need to format the text to add
it back in. below is an example of the data. Also when the - is omitted it
reads the cell as text instead of general and omits the leading 0.

02-123456
2123456

You see no - and no leading 0. I need to add both back in. Can anyone give
me some help? I have tried a few things but i have another issue that
breaks all of them. Thanks.

dm.



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default need help formatting text in macro

I've never worked with a bar code scanner before... I presume it put the
values it reads into the cells as text (or possibly numeric) constants. If
so, give this a try...

Sub AddZeroAndDash()
Dim C As Range
For Each C In Columns("F").SpecialCells(xlCellTypeConstants)
C.Value = Format(Replace(C.Value, "-", ""), "00-000000")
Next
End Sub

Rick


"Daniel M" wrote in message
...
I have a spreadsheet that i scan barcodes into. Some of the barcodes are
old and omit the - in the serial number. I need to format the text to add
it back in. below is an example of the data. Also when the - is omitted it
reads the cell as text instead of general and omits the leading 0.

02-123456
2123456

You see no - and no leading 0. I need to add both back in. Can anyone give
me some help? I have tried a few things but i have another issue that
breaks all of them. Thanks.

dm.



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Find formatting in text in cell, insert tags around formatting. CarlC Excel Programming 2 February 27th 08 09:26 PM
Formula Text String: Formatting Text and Numbers? dj479794 Excel Discussion (Misc queries) 5 June 30th 07 12:19 AM
Adding 4 rows after each total criteria in the column A with formatting and added text using macro. [email protected] Excel Programming 9 March 20th 07 06:08 PM
Conditional Formatting based on text within a cell w/ text AND num Shirley Excel Worksheet Functions 2 December 22nd 06 01:40 AM
Formatting text in a user form text box DB Excel Programming 0 July 23rd 05 08:09 PM


All times are GMT +1. The time now is 06:03 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"