简体   繁体   中英

VBA, select row based on cell value

My data is dynamic and the only values I must store are greater than zero and are located in column H. Therefore, I must delete the rows that contain 0 in column H before saving the record.

My current selection is just hardcoded below the data set.

EDIT : How can I change my selection process from hardcode to formula based on Column H values for the entire row before i clear.contents *Specifically the portion

`Range("A4:J8").Select

    Selection.ClearContents`

Here's my code for reference. Thank you!

Sub openCSV() 
    Range("A4:J8").Select

    Selection.ClearContents
    Selection.ClearContents
    ActiveWorkbook.Save
    ActiveWorkbook.Save
    SendKeys String:="%slc{enter}", Wait:=True
    Application.Wait (Now + TimeValue("00:00:03"))
    ActiveWindow.Close
End Sub

I am not sure I 100% understand the questions. If all you are asking for is how to clear the contents of a row based off of a columns value then this code should work for you:

Sub openCSV()
    Dim lRow As Long

    lRow = Cells(Rows.Count, 1).END(xlUp).Row 'finds last row

    'Loop from the last row to the first (assuming row 1 is header data so stopping at row 2)
    For i = lRow To 2 Step -1
        'Perform whatever test you want on column H
        If ActiveSheet.Range("H" & i).Value = 0 Then
            ActiveSheet.Rows(i).ClearContents
        End If
    Next i

    Range("A4:J8").Select

    ActiveWorkbook.Save
    ActiveWorkbook.Save
    SendKeys String:="%slc{enter}", Wait:=True
    Application.Wait (Now + TimeValue("00:00:03"))
    ActiveWindow.Close
End Sub

I am using a reverse loop because I am not sure of what you are asking. If you end up wanting to delete the row, then this code still works as it's going from end to beginning. If you only clear contents then you can go either direction and it will still work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM