简体   繁体   English

VBA,根据单元格值选择行

[英]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.我的数据是动态的,我必须存储的唯一值大于零并且位于 H 列中。因此,我必须在保存记录之前删除 H 列中包含 0 的行。

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编辑:在我清除内容之前,如何根据整行的 H 列值将我的选择过程从硬编码更改为公式 *特别是部分

`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.我不确定我 100% 理解这些问题。 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.如果您只清除内容,那么您可以朝任一方向前进,它仍然可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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