Combining 2 cell vaules -- without losing data

Kabeer456

New Member
Joined
Aug 20, 2012
Messages
10
Good afternoon all-

I have a data set that I'm trying to sort in order to be efficient for some of my team members.


Below is an example of the raw data and how I've currently got it sorted. I think my problem may be that I am trying to join a number with a string but I'm not positive. Any insight will be helpful.
Col A
123-45678-A-1
123-45678-A-10
123-45678-B-2

<TBODY>
</TBODY>

I need to eliminate the letter from the data, add a leading zero to all single digit numbers and sort ascendingly.

My first step is to split the data into columns using the "-" as a delimiter. I end up with 2 columns as shown below.
Col A
Col B
123-45678-
1
123-45678-
10
123-45678-
2

<TBODY>
</TBODY>


Next I add a leading zero to Col B. The assumption is there will never be more than 99 numbers, so I use the following code:

Columns("B:B").Select
Selection.NumberFormat = "00"

To produce:
Col A
Col B
123-45678-
01
123-45678-
10
123-45678-
02

<TBODY>
</TBODY>

Now all I need to do is rejoin these 2 columns before sorting. My current code is:

Sub Rejoin_Container_Number()
x = 1
Columns("A:A").Select
Selection.Insert Shift:=xlToRight
Do While Cells(x, 2).Value <> ""
Cells(x, 1).Value = Cells(x, 2).Value & Cells(x, 3).Value

x = x + 1
Loop
Columns("B:C").Select
Selection.Delete Shift:=xlToLeft
End Sub

As many have probably already guessed, this produces the following result:

Col A
123-45678-1
123-45678-10
123-45678-2

<TBODY>
</TBODY>

I'm looking for:

Col A
123-45678-01
123-45678-10
123-45678-02

<TBODY>
</TBODY>
**zeroes in BOLD for reference only, result does not need to be bold**

Anyone have any ideas?

Thanks, as always!

-Brian
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Replace

Code:
Cells(x, 1).Value = Cells(x, 2).Value & Cells(x, 3).Value

with

Code:
Cells(x, 1).Value = Cells(x, 2).Value & iif(len(Cells(x, 3))=1,"0" & cells(x,3),cells(x,3))


Tim
 
Upvote 0
this takes care of it in the cell no spliting it out into columns

Code:
Sub Rejoin_Container_Number()
Dim mycell As Range
Dim lastDash As Long
Dim endNum As String
x = 1
Range("A1:A" & Cells(Rows.Count, Columns("A").Column).End(xlUp).Row).Select   'find depth of list

 For Each mycell In Selection
    If mycell.Value <> "" Then
      lastDash = InStrRev(mycell.Value, "-")  'get pos of last dash
      endNum = Format(Mid(mycell.Value, lastDash + 1, 5), "00")
      lastDash = InStrRev(Left(mycell.Value, lastDash - 1), "-")
      mycell.Value = Left(mycell.Value, lastDash) & endNum
    End If
  Next mycell
End Sub
 
Upvote 0
Genius!

You can't put a price on another brain and set of eyes. You managed to save me a lot of trouble with minimal code. Thank you!

Cheers-
Brian
 
Upvote 0
Sorry if I wasn't clear, but I meant to send kudos to both of you who replied so quickly.

Cheers-
Brian
 
Upvote 0

Forum statistics

Threads
1,214,599
Messages
6,120,448
Members
448,966
Latest member
DannyC96

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