Macro To Take Date Only From The Furthest Right Hand Column Of Rows

CONFUSED_AS_USUAL

Board Regular
Joined
Jul 6, 2017
Messages
59
Self explanatory, but for clarity...



1. Row 1 has 5 columns of data (A to E). I want only the 5th column data. The rest is to be deleted. That 5th column data would now be in column A.

2. Row 2 has 10 columns of data (A to J). I want only the 10th column data. The rest is to be deleted. That 10th column data would now be in column A.

3 Row 3 has 1 column of data. It remains where it is.


There could be thousands of rows and dozens of columns.

Thanks.
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
How about
Code:
Sub DeleteAllButLastCol()

   Dim Cnt As Long
   
   For Cnt = 1 To Range("A" & Rows.Count).End(xlUp).Row
      Range(Cells(Cnt, 1), Cells(Cnt, Columns.Count).End(xlToLeft).Offset(, -1)).Delete xlToLeft
   Next Cnt
   
End Sub
 
Upvote 0
Close... it worked on the 1st 3 columns below - but not the fourth.


aassddfhjkk;'h
ghhfd
df
hsdffghsdffghsdffghsdffghsdffghsdffghsdffghsdffg

<colgroup><col width="64" span="16" style="width:48pt"> </colgroup><tbody>
</tbody>
 
Upvote 0
I assume you meant it doesn't work on the 3rd row, in which case try
Code:
Sub DeleteAllButLastCol()

   Dim Cnt As Long
   Dim UsdCols As Long
   
   For Cnt = 1 To Range("A" & Rows.Count).End(xlUp).Row
      UsdCols = Cells(Cnt, Columns.Count).End(xlToLeft).Column
      If UsdCols > 1 Then Range(Cells(Cnt, 1), Cells(Cnt, UsdCols - 1)).Delete xlToLeft
   Next Cnt
   
End Sub
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,215,388
Messages
6,124,641
Members
449,177
Latest member
Sousanna Aristiadou

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