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

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
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.


[TABLE="width: 1024"]
<colgroup><col width="64" span="16" style="width:48pt"> </colgroup><tbody>[TR]
[TD="width: 64"]aa[/TD]
[TD="width: 64"]ss[/TD]
[TD="width: 64"]dd[/TD]
[TD="width: 64"]fh[/TD]
[TD="width: 64"]j[/TD]
[TD="width: 64"]k[/TD]
[TD="width: 64"]k[/TD]
[TD="width: 64"];'[/TD]
[TD="width: 64"]h[/TD]
[TD="width: 64"][/TD]
[TD="width: 64"][/TD]
[TD="width: 64"][/TD]
[TD="width: 64"][/TD]
[TD="width: 64"][/TD]
[TD="width: 64"][/TD]
[TD="width: 64"][/TD]
[/TR]
[TR]
[TD]g[/TD]
[TD]h[/TD]
[TD]hfd[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]df[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]hsdf[/TD]
[TD]fg[/TD]
[TD]hsdf[/TD]
[TD]fg[/TD]
[TD]hsdf[/TD]
[TD]fg[/TD]
[TD]hsdf[/TD]
[TD]fg[/TD]
[TD]hsdf[/TD]
[TD]fg[/TD]
[TD]hsdf[/TD]
[TD]fg[/TD]
[TD]hsdf[/TD]
[TD]fg[/TD]
[TD]hsdf[/TD]
[TD]fg[/TD]
[/TR]
</tbody>[/TABLE]
 
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,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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