VBA or formula to move data in last used column to column A

JPM

Active Member
Joined
Aug 1, 2002
Messages
409
Office Version
  1. 365
Platform
  1. Windows
I have a system generated report that, when imported into Excel, has financial data in the last used column of each row. However, the last column varies from row to row between columns B and P.
I created a simple macro that gets the job done, but takes a long time when the report get big, because it moves down row by row and does a copy and paste routine.
I'm looking for some faster solution, either VBA or even a formula that I can just copy down. Any ideas?
Here is my existing code:
VBA Code:
Sub GetDataInLastColumn()
    'Purpose is to copy the data from the last used column into added column A
    Application.ScreenUpdating = False
    'Insert a new column A and highlight yellow
    Sheets("Copy").Select
    MaxRow = ActiveSheet.UsedRange.Rows.Count
    Columns("A:A").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Columns("A:A").Interior.Color = 65535
    Range("A1").Select
    'Find last used column and copy data to column A. Go row by row
Repeat:
    If ActiveCell.row > MaxRow Then GoTo Done
    Application.StatusBar = "The macro is running please wait...checking row " & ActiveCell.row & " of " & MaxRow
    ActiveCell.Offset(rowOffset:=1, columnOffset:=0).Activate
    Cells(ActiveCell.row, "XFD").Select
    Selection.End(xlToLeft).Select
    Cost = ActiveCell.Formula
    Cells(ActiveCell.row, "A").Activate
    ActiveCell.Formula = Cost
GoTo Repeat
Done:
    Application.ScreenUpdating = True
    Application.StatusBar = ""
End Sub

Thanks
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
I suggest that you update your Account details (or click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)

See if this is a bit faster.
Also wondering if you really want/need the whole of the new column coloured? This just colours the part that is populated by the rest of the code.

VBA Code:
Sub LastCol()
  Dim a As Variant
  Dim i As Long, j As Long, uba2 As Long
 
  Application.ScreenUpdating = False
  With Sheets("Copy")
    a = .UsedRange.Value
    uba2 = UBound(a, 2)
    .Columns("A").Insert
    Intersect(.UsedRange.EntireRow, .Columns("A")).Interior.Color = 65535
    For i = 1 To UBound(a)
      j = uba2
      Do While Len(a(i, j)) = 0 And j > 1
        j = j - 1
      Loop
      a(i, 1) = a(i, j)
    Next i
    .Range("A1").Resize(UBound(a)).Value = a
  End With
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
What about something like this?

VBA Code:
Sub GetDataInLastColumn()
    Dim lastRow As Long, i As Long, lastCol As Long
    Dim rowArr As Variant
    Columns("A:A").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Columns("A:A").Interior.Color = 65535
    lastRow = Worksheets("Copy").Cells.Find("*", _
        SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    For i = 1 To lastRow
        lastCol = Sheets("Copy").Range("B" & i).End(xlToRight).Column
        rowArr = Range(Cells(i, 2), Cells(i, lastCol))
        Range("A" & i).Value = rowArr(1, UBound(rowArr, 2))
    Next
End Sub
 
Upvote 0
Peter_SSs and myall_blues, thank you both for your elegant solutions. When I timed my macro for my latest report of 1200+ rows, it took me 12.97 seconds to execute the routine. myall_blues solution got it done in 0.19 seconds and Peter_SSs in only 0.05 seconds. I certainly still have a lot to learn. Thanks for your help.
 
Upvote 0
You’re welcome. I’m glad we were able to help.
You might like to investigate using arrays in VBA, which both our solutions make use of. Paul Kelly has some very good videos on the subject.
 
Upvote 0
You're welcome. Thanks for the follow-up. :)

I don't know if your data will increase in size but if it does then the speed difference factor will also increase. In your example my code was about 4 times faster. With my testing with about 5,000 rows it was about 30 times faster.

BTW, you mentioned formulas and this could also be done with formulas. Insert the new column A yourself and put a formula like this in the top cell.
This is a smaller example but for my 5,000 row sample data, the recalculation time was just as fast as my vba code time.

JPM.xlsm
ABCDEFGHIJKLMNOP
1800873833886479018302460918129906084934561133013737023152217315740511305800873
2152781694432680184152781
3365569365569
445073723320158110447101048367822334524639611615534635327497505501848001834308450737
5338231667883990308184905183552349575515817106319374623338231
6312018763310219130163700610449831923569981504628157301644408948931931361118312018
75134451344
861362156465262526753416186129235785613621
993812858839732489698689074611125877503279417152882384315216938128
10287044346035698744492012657118980488505208944476894203831715702603665388417828967393752209287044
11
123468234682
13817381572411817381
14797331293990616887851187797331
158242182373170557917867882421
16688092702109869374948127688092
17661249491944661249
18104164104164
19147468136405147468
20800402456615800402
Copy (2)
Cell Formulas
RangeFormula
A1:A20A1=BYROW(B1:P20,LAMBDA(r,XLOOKUP("?*",r&"",r,"",2,-1)))
Dynamic array formulas.
 
Upvote 0

Forum statistics

Threads
1,224,847
Messages
6,181,327
Members
453,032
Latest member
Pauh

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