Copying the last three cells with data from a row into new cells

StraxusD

New Member
Joined
May 13, 2022
Messages
2
Office Version
  1. 2019
Platform
  1. Windows
I have a table of data that has at least 60 rows in it that tracks the newest installed gas cylinder on some test equipment. Each piece of equipment has its on row and as we change the bottles, we enter the new cylinder # and reference # to the end of the row. Because some of the equipment uses much more quickly than others, some of the equipment is on its first bottle of the year and some is on its third already. What I want to do is scan each row (starting at row 3, column "I") in columns "I" through "AA" and find the last two columns of data and copy it to columns "C" and "D" of the same row so that when we audit the bottles the latest bottle is listed at the beginning of the sheet to make tracking easier. The data is not always numbers, it is text as well.

I am not versed in VBA at all, but I figured it was the easiest way to accomplish this task. Can anyone help me with this? I want it to be open ended enough that if I add more rows the program will not have to be modified.
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Try this:
It searches each row for the last two values in the row does not stop at column (AA)
See if this will work for you.
VBA Code:
Sub Search_Me()
'Modified 5/13/2022  10:59:16 PM  EDT
Application.ScreenUpdating = False
Dim i As Long
Dim Lastrow As Long
Dim LastColumn As Long
Lastrow = Cells(Rows.Count, "I").End(xlUp).Row

    For i = 3 To Lastrow
        LastColumn = Cells(i, Columns.Count).End(xlToLeft).Column
        Cells(i, 4).Value = Cells(i, LastColumn).Value
        Cells(i, 3).Value = Cells(i, LastColumn - 1).Value
    Next
Application.ScreenUpdating = True
End Sub
 
Upvote 0
My script does this:
You said this:
and find the last two columns of data and copy it to columns "C" and "D"

But your subject title says:
Copying the last three cells with data from a row into new cells
 
Upvote 0
My script does this:
You said this:
and find the last two columns of data and copy it to columns "C" and "D"

But your subject title says:
Copying the last three cells with data from a row into new cells
I didn't realize that I put three columns in the title, but your script does exactly what I needed it to do. This will make it way easier for my guys to audit all of the gas cylinders since the checklist will be on a single sheet of paper now. Thank you so much! Just curious, if we ever decide to modify the sheet and add the expiration date into the data, how would I modify the program to copy the third from the last column over?

Would I just add the line "Cells(i, 2).Value = Cells(i, LastColumn - 2).Value" in the loop?

Thank you again!!
 
Upvote 0

Forum statistics

Threads
1,214,789
Messages
6,121,590
Members
449,039
Latest member
Arbind kumar

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