macro to reverse numbers in a cell

michaelsmith559

Well-known Member
Joined
Oct 6, 2013
Messages
881
Office Version
  1. 2013
  2. 2007
I need help with a macro to reverse numbers in a cell. I am unsure of the best way to go about this. My first thought was a select case, but I am unsure. Here is what I am trying to do. I have a list of numbers starting in A2 and going on down the column. New data is added to the column a couple of times each week. Each number in the column is one of three lengths: 5, 6, or 7. For example: suppose A2=1003620, A3=134898, and A4=22575. I need the macro to calculate the length of the number in the cell and if it is a 5 digit number put the first two numbers to the right of the last three numbers, for a 6 digit number put the first three numbers to the right of last three numbers, and for a 7 digit number leave the first digit, but move the next three numbers to the right of the last three numbers. So using the example above the macro would change the numbers like so:
A2=1003620 To A2=1620003
A3=134898 To A3=898134
A4=22575 To A4=57522

Thanks for any help.

Mike
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Try
Code:
Sub rearrangenum()

Dim lr As Long
Dim mylen As Long


lr = Cells(Rows.Count, 1).End(xlUp).Row


For x = 2 To lr
    mylen = Len(Cells(x, 1))
    If mylen = 5 Then Cells(x, 2) = Mid(Cells(x, 1), 3, 3) & Left(Cells(x, 1), 2)
    If mylen = 6 Then Cells(x, 2) = Mid(Cells(x, 1), 4, 3) & Left(Cells(x, 1), 3)
    If mylen = 7 Then Cells(x, 2) = Left(Cells(x, 1), 1) & Right(Cells(x, 1), 3) & Mid(Cells(x, 1), 2, 3)


Next x




End Sub
 
Upvote 0
Try this:

Code:
Function ReverseNumbers(strNum As String) As Long

  a = Left(strNum, IIf(Len(strNum) >= 6, Len(strNum) - 6, 0))
  b = Right(strNum, 3)
  c = Left(Right(strNum, 6), IIf(Len(strNum) >= 6, 3, IIf(Len(strNum) >= 3, Len(strNum) - 3, 0)))
  
  ReverseNumbers = a & b & c

End Function
 
Upvote 0
Thanks for the replies. After looking at both options, I chose to go with the udf. Again, thanks both of you.
 
Upvote 0

Forum statistics

Threads
1,215,012
Messages
6,122,682
Members
449,091
Latest member
peppernaut

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