Align numbers between two columns

Panoos64

Well-known Member
Joined
Mar 1, 2014
Messages
882
Hi to all of you! Due to the mix of invoices' numbers and orders' numbers, I would like to alter my data between these two columns “AN” and “AO” so that to align the numbers on its right columns using a vba code. Therefore, that invoice numbers are bigger numbers than orders numbers and I think is the key to move each other. I present below an extract of original data and expected result. Note that the rows arise to 100000+ and between them some cells are blank or contains text. However the align should be just for numbers. Thank you all in advance

Original data

1628835274902.png



Expected result


1628835289011.png
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Give this a try with a copy of your workbook.

VBA Code:
Sub AlignNumbers()
  Dim a As Variant
  Dim i As Long, tmp As Long
  
  With Range("AN5:AO" & Columns("AN:AO").Find(What:="*", LookIn:=xlValues, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row)
    a = .Value
    For i = 1 To UBound(a)
      If Len(a(i, 1)) > 0 And Len(a(i, 2)) > 0 And IsNumeric(a(i, 1)) And IsNumeric(a(i, 2)) Then
        If a(i, 2) > a(i, 1) Then
          tmp = a(i, 2)
          a(i, 2) = a(i, 1)
          a(i, 1) = tmp
        End If
      End If
    Next i
    .Value = a
  End With
End Sub
 
Upvote 0
Other Option:
VBA Code:
Sub Flip_AN_AO()

Dim i, j, Rows1, Rows2 As Integer
Dim Invoice, Order As String


Rows1 = Worksheets("Sheet1").Cells(Rows.Count, "AO").End(xlUp).Row
Rows2 = Worksheets("Sheet1").Cells(Rows.Count, "AN").End(xlUp).Row


'Optimize Macro Speed
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    Application.Calculation = xlCalculationManual

If Rows2 > Rows1 Then Rows1 = Rows2

'Loop Through Rows
    For i = 4 To Rows1                                                      'your screenshot didn't showed Header or row details, so assumed your headers were placed on row 4.
                                                                            'If that's not the case you need to amend the 4 to the correct header Row
        Order = Worksheets("Sheet1").Cells(i, "AO").Value                   'Order Number
        Invoice = Worksheets("Sheet1").Cells(i, "AN").Value                 'Invoice Number
       
        If IsNumeric(Order) And IsNumeric(Invoice) And Order > 999999 And Invoice < 1000000 Then                        'If condition is true Order and Invoice values are mixed up
                Worksheets("Sheet1").Cells(i, "AO") = Invoice
                Worksheets("Sheet1").Cells(i, "AN") = Order
        End If
    Next i
   
'Reset Macro Optimization Settings
    Application.EnableEvents = True
    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True

End Sub
 
Upvote 0
Not sure if you read or tested for speed but the OP has up to 100,000 rows. :eek:
Yep tested it with 40K Lines and your solution is the better option because when i expanded my range to 300k+ lines it took quit some time.
 
Upvote 0
Thank you Peter, it works perfect and based to my data! Thank you once again for your support. Hv a great day!
 
Upvote 0
Hi joris, thank for your support. I tested several times but the code stops on the line "If IsNumeric(Order) And IsNumeric(Invoice) And Order > 999999 And Invoice < 1000000 Then " and the message is below:
However i appreciated what you done for my project and if is not so complicate i would be thankful if you could check it again. Hv a lovely day!

1628844225898.png
 
Upvote 0
Hi joris, thank for your support. I tested several times but the code stops on the line "If IsNumeric(Order) And IsNumeric(Invoice) And Order > 999999 And Invoice < 1000000 Then " and the message is below:
However i appreciated what you done for my project and if is not so complicate i would be thankful if you could check it again. Hv a lovely day!

View attachment 44771
I noticed i copied the old version of the VBA. However @Peter_SSs version was the better solution anyway due to timeconstraint on lager number of lines.
Thx for the feedback anyway!
 
Upvote 0
I tested several times but the code stops on the line "If IsNumeric(Order) And IsNumeric(Invoice) And Order > 999999 And Invoice < 1000000 Then
The problem with that line is if Order and/or Invoice is not numeric it still tries to do the last two comparisons Order > 999999 And Invoice < 1000000 which error if either Order or Invoice is not numeric.
That is why in my code I broke those sort of comparisons into two separate lines of code.
Rich (BB code):
If Len(a(i, 1)) > 0 And Len(a(i, 2)) > 0 And IsNumeric(a(i, 1)) And IsNumeric(a(i, 2)) Then
  If a(i, 2) > a(i, 1) Then

Thank you Peter, it works perfect and based to my data! Thank you once again for your support. Hv a great day!
You are very welcome once again. :)
 
Upvote 0

Forum statistics

Threads
1,214,812
Messages
6,121,699
Members
449,048
Latest member
81jamesacct

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