Beginner Seeking Help

Viking1221

New Member
Joined
May 25, 2017
Messages
32
Hello,

I have a macro that use the autofilter function to find blanks and then deletes the rows. However, I am struggling with how to write a macro that will find the last column in row 10 and filter on zero and delete all 0's. Then after it executes this code, I want 'that' entire column deleted. Any ideas?

This is the code I have, but is specifically looks at Column C:

Sub DeleteRowsC()

Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Journals")
ws.Activate
On Error Resume Next
ws.ShowAllData
On Error GoTo 0

ws.Range("A10:BZ10000").AutoFilter Field:=3, Criteria1:=""

Application.DisplayAlerts = False
ws.Range("A10:BZ10000").SpecialCells(xlCellTypeVisible).Delete
Application.DisplayAlerts = True

On Error Resume Next
ws.ShowAllData
On Error GoTo 0

End Sub
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
I am probably misreading your post, but if you get the last column, delete the zeros and then delete the column, why not just delete the column to begin with?
Code:
Sub t()
With ActiveSheet
    . Columns(.Cells(10, Columns.Count).End(xlToLeft).Column).Delete xlShiftLeft
End With
End Sub
 
Last edited:
Upvote 0
Are you wanting to DELETE Column of Row 10 Only?

Hello,

What I am doing in my last row is adding up columns D:End to identify rows with all zero's. I then want to filter on 0 and delete all rows with 0 in this column. Then I want to delete that column as it is not in my original dataset.
 
Upvote 0
Wouldn't just changing the 3 to a 10 work?

ws.Range("A10:BZ10000").AutoFilter Field:=3, Criteria1:=""

ws.Range("A10:BZ10000").AutoFilter Field:=10, Criteria1:=0

Then delete your rows and then do Range("j:j").delete

I think that will give you what you've asked for.
 
Last edited:
Upvote 0
Oops, misread the row 10 as column 10. Ok, find the last cell in row 10 by going all the way to the right of column then and then back to the first filled cell. Once you have that cell, use it's column reference to adjust your autofilter line as above.

Code:
[FONT=Courier][COLOR=#00007f]Sub[/COLOR] xxx()

    [COLOR=#00007f]Dim[/COLOR] lCol [COLOR=#00007f]As[/COLOR] [COLOR=#00007f]Long[/COLOR]
    lCol = Cells(10, Columns.Count).End(xlToLeft).Column
    [/FONT][FONT=Courier]Range("A10:BZ10000").AutoFilter Field:=lCol, Criteria1:=0   
    Range("A10:BZ10000").SpecialCells(xlCellTypeVisible).Delete
    Range(Cells(1,10),cells(1,10)).column.delete
[COLOR=#00007f]End[/COLOR] [COLOR=#00007f]Sub[/COLOR][/FONT]

I'm guessing on the last line whether that will get you the column ok.
 
Last edited:
Upvote 0
correct last line:
Code:
[FONT=Courier][COLOR=#00007f]Sub[/COLOR] xxx()

    [COLOR=#00007f]Dim[/COLOR] lCol [COLOR=#00007f]As[/COLOR] [COLOR=#00007f]Long[/COLOR]
    lCol = Cells(10, Columns.Count).End(xlToLeft).Column
    [/FONT][FONT=Courier]Range("A10:BZ10000").AutoFilter Field:=lCol, Criteria1:=0   
    Range("A10:BZ10000").SpecialCells(xlCellTypeVisible).Delete
    Range(Cells(1, lCol), Cells(1, lCol)).EntireColumn.Delete
[COLOR=#00007f]End[/COLOR] [COLOR=#00007f]Sub[/COLOR][/FONT]
 
Upvote 0
correct last line:
Code:
[FONT=Courier][COLOR=#00007f]Sub[/COLOR] xxx()

    [COLOR=#00007f]Dim[/COLOR] lCol [COLOR=#00007f]As[/COLOR] [COLOR=#00007f]Long[/COLOR]
    lCol = Cells(10, Columns.Count).End(xlToLeft).Column
    [/FONT][FONT=Courier]Range("A10:BZ10000").AutoFilter Field:=lCol, Criteria1:=0   
    Range("A10:BZ10000").SpecialCells(xlCellTypeVisible).Delete
    Range(Cells(1, lCol), Cells(1, lCol)).EntireColumn.Delete
[COLOR=#00007f]End[/COLOR] [COLOR=#00007f]Sub[/COLOR][/FONT]

hello, this works great. However, it appears to only be deleting my top row and not all with row with zero. From my understanding the codes seems to be written correctly. Any ideas on why this may be?
 
Upvote 0
@Viking1221
You didn't respond to the post from @JLGWhiz.....which to me, seems to be the easiest and simplest method


Code:
Sub t()
With ActiveSheet
    . Columns(.Cells(10, Columns.Count).End(xlToLeft).Column).Delete xlShiftLeft
End With
End Sub
 
Last edited:
Upvote 0
hello, this works great. However, it appears to only be deleting my top row and not all with row with zero. From my understanding the codes seems to be written correctly. Any ideas on why this may be?

Never mind, I figured it out, it is the way I have the numbers formatted. Zero's show as "-" I just had to change the 0 to a - and it worked. Thanks for your help.
 
Upvote 0

Forum statistics

Threads
1,213,510
Messages
6,114,040
Members
448,543
Latest member
MartinLarkin

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