Help tweaking this code from macro recorder so it works correctly with my code: (pretty simple stuff)

kbishop94

Active Member
Joined
Dec 5, 2016
Messages
458
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
the code simply needs to find the first empty cell in row 1 (starting with column H(8), and going over to the last column that has text in it, then plus 1 column), and then from that point select all the way down, and all the way to the right. Then clear all, then delete.
I have it working fine where it finds the correct cell/column, but I cannot get what the macro I used as a basis to delete everything from that point (which happens to be column "OF") and revise it so that instead of column "OF", it should select the last last column with text in it, +1 column: (my variable "rCell" would be this column.)

Here is my code:
Code:
Private Sub cmdLastRow_Click()
Dim rCell As Range
Dim lCol As Long
    lCol = ActiveSheet.UsedRange.Columns.Count

For Each rCell In Range(Cells(1, 8), Cells(1, lCol + 1))
If rCell = "" Then

    [B][COLOR=#0000cd]Columns("OF:OF").Select[/COLOR][/B]

    Range(Selection, Selection.End(xlToRight)).Select
    Selection.clear
    Selection.Delete Shift:=xlToLeft
Else
End If
Next rCell

End Sub

So I need the:

Code:
[B][COLOR=#0000CD]Columns("OF:OF").Select[/COLOR][/B]
part to be changed so that its actually column "lCol +1" (or rCol) and not the "OF:OF" which is from my macro I used as the basis to get the clearing/deleting functions.

Please and Thank you for any assistance.
icon14.png
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Try using

Code:
    Columns(1).Resize(, lCol + 1).EntireColumn.Select
 
Upvote 0
Try using

Code:
    Columns(1).Resize(, lCol + 1).EntireColumn.Select

Thank you, Michael M.

Using the original "macro" code I started with (and omitting the clear/delete code so it just selects the intended area) it selects the correct column.
118gbcz.jpg




But using your supplied code I see this:

104p3kj.jpg


and then when the clear/delete code is added obviously its deleting everything on the worksheet. :confused: Thank you, I do appreciate your input, but how can i make it so it goes the other way (away from A1 and out towards to the right and out to infinity? (the end of the sheet, I mean ) Please and Thank you, sir. (y)
 
Upvote 0
I got it, Michael.
icon14.png


Code:
For Each rCell In Range(Cells(1, 8), Cells(1, lCol + 1))
If rCell = "" Then

[COLOR=#0000cd][B]Columns.EntireColumn(lCol + 1).EntireColumn.Select[/B][/COLOR]

    Range(Selection, Selection.End(xlToRight)).Select
    Selection.clear
    Selection.Delete Shift:=xlToLeft

Else
End If
Next rCell
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,561
Members
449,089
Latest member
Motoracer88

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