How can I set a variable to this cell in VBA?

storemannequin

Board Regular
Joined
May 29, 2010
Messages
108
I'm trying to set up this variable so I can loop in this column to find the 0's and then when found, to delete the entire row. I'm trying to set Q to equal the column header of QTY but cannot seem to get it to execute.

also, if anybody knows of a better way to do this task without looping, that's be instructive as well!

Thanks

Code:
Sub FindQTY()
With Rows("1:1")
    Set Q = Cells.Find(What:="QTY", After:=ActiveCell, LookIn:=xlValues, LookAt:= _
        xlWhole, SearchOrder:=xlByRows).Select
 
 
 
End With
 
 
End Sub
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Try

Code:
Sub FindQTY()
Dim Q As Long
With Rows("1:1")
    Q = .Find(What:="QTY").Column
End With
End Sub
 
Upvote 0
Are you referring to something like this?
Code:
Sub Delete_zeroValue_row()
Dim myzero As Range
    For Each myzero In Range("A1:A100")
 
    If myzero.Value = 0 Then
     myzero.EntireRow.Delete
    End If
    Next myzero
End Sub

I re-read and i think this is not related....apologies
 
Upvote 0
Code:
Sub FindQTY()

    Dim Q As Range

    Set Q = Rows("1:1").Find(What:="QTY", LookIn:=xlValues, _
                            LookAt:=xlWhole, MatchCase:=True)
 
    If Not Q Is Nothing Then
        Q.EntireColumn.Select
    Else
        MsgBox "No match found."
    End If
 
End Sub
 
Upvote 0
I'm wondering why this doesn't work then:

Code:
Dim Q As Long
FR = Cells(Rows.Count, 1).End(xlUp).Row

        With Rows("1:1")
          Q = .Find(What:="QTY").Column
          
          
          
        End With
    For k = FR To 2
        If Cells(k, Q).Value = "0" Then
            Rows(k).Delete
        End If
    Next k
    
    
End Sub
 
Upvote 0
I tried this way and works....:)
Code:
[/FONT]
[FONT=Courier New]Sub TEST1()
Dim Q As Long
Dim FR As Long
Dim k As Long[/FONT]
[FONT=Courier New]    With Rows("1:1")
      Q = .Find(What:="QTY").Column
    End With[/FONT]
[FONT=Courier New]    FR = Cells(Rows.Count, Q).End(xlUp).Row
    
    For k = FR To 2 Step -1
       If Cells(k, Q).Value = "0" Then
          Cells(k, Q).EntireRow.Delete
        End If
    Next k
    
End Sub
 
Upvote 0
I'm trying to set up this variable so I can loop in this column to find the 0's and then when found, to delete the entire row. I'm trying to set Q to equal the column header of QTY but cannot seem to get it to execute.

also, if anybody knows of a better way to do this task without looping, that's be instructive as well!
Here's some VBA code that should do as you request in bold above without looping. I hope that you find it instructive.

I must add that non-looping isn't necessarily advantageous here but, for what it's worth, this code deleted about 33,000 rows out of 100,000 with zeros in column headed QTY in under a second.
Code:
Sub delrowswithzero()
Dim lr&, lc&, x&, q, y&
lr = Cells.Find("*", after:=Cells(1), searchorder:=xlByRows, _
    searchdirection:=xlPrevious).Row
lc = Cells.Find("*", after:=Cells(1), searchorder:=xlByColumns, _
    searchdirection:=xlPrevious).Column
On Error GoTo nd
x = Application.Match("QTY", Rows("1:1"), 0)
On Error GoTo 0
Cells(1, x).Resize(lr).Name = "q"
Cells(1, lc + 1).Resize(lr) = Evaluate("if(q<>0,1,"""")")
Range("A1").Resize(lr, lc + 1).Sort Cells(1, lc + 1), 1, Header:=xlYes
y = Cells(1, lc + 1).End(4).Row
Range(Cells(y + 1, 1), Cells(lr, lc)).ClearContents
Cells(1, lc + 1).Resize(lr).ClearContents
nd:
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,518
Messages
6,179,259
Members
452,901
Latest member
LisaGo

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