简体   繁体   English

VBA 根据单元格值循环到 select 行?

[英]VBA loop to select rows based on cell value?

I have data from a machine which I import into excel and it gives a dataset which is around 7 columns wide and 66000 rows deep .我有一台机器的数据,我将其导入 excel,它提供了一个大约 7 列宽和66000 行深的数据集。 (sometimes it will be more, sometimes less). (有时会更多,有时会更少)。

What I want to is to be able to select data based on Column E and then that will select all the rows so far, I can mirror that across to another sheet.我想要的是能够基于 E 列的 select 数据,然后 select 到目前为止的所有行,我可以将其镜像到另一张纸上。

Column E by the way basically contains a couple of thousand rows of zeros, then it changes to a number greater than zero for a few thousand rows.顺便说一句,E 列基本上包含几千行零,然后在几千行中变为大于零的数字。 Then it loops back to zero etc. It will do this cycle around 25 times per data set.然后它循环回到零等。它将对每个数据集执行此循环约 25 次。 (Always different between number of rows per loop etc). (每个循环的行数等总是不同)。

So what I think what I want is basically two loops (I have never done any loop work before I only usually use VBA for graphing and other things etc).所以我认为我想要的基本上是两个循环(在我通常只使用 VBA 进行绘图和其他操作之前,我从未做过任何循环工作)。

Loop 1:循环 1:

Look at column E if it equals zero then select the row.查看 E 列,如果它等于零,则查看该行的 select。

Do this until Column E Does not equal zero这样做直到 E 列不等于零

Mirror this selection (I can use the Range.Resize method)into different area将这个选择(我可以使用 Range.Resize 方法)镜像到不同的区域

Delete the selection删除选择

Shift cells up向上移动单元格

I would then call a similar loop for the values greater than zero and then recall loop 1 again and do this 25 times (but that doesn't matter here)...然后我会为大于零的值调用一个类似的循环,然后再次调用循环 1 并执行 25 次(但这并不重要)......

What I want help with is:我需要帮助的是:

How do I create a loop to select cells/rows of data based on the value of a cell/column?如何根据单元格/列的值创建到 select 单元格/数据行的循环? The loop should end once it has selected the cells because each group will be moved to a different area on the work book...一旦选择了单元格,循环就应该结束,因为每个组都将移动到工作簿上的不同区域......

Or is there something better to use than loops?或者有什么比循环更好用的东西吗?

Hope this all makes sense and I dont sound to stupid...希望这一切都是有道理的,我听起来不傻......

Cheers干杯

Retrieve Non-Zero Areas检索非零区域

Option Explicit

Sub RetrieveNonZeroAreas()
    
    Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
    
    Dim sws As Worksheet: Set sws = wb.Worksheets("Sheet1")
    If sws.FilterMode Then sws.ShowAllData
    
    Dim srg As Range: Set srg = sws.Range("A1").CurrentRegion
    Dim sdrg As Range: Set sdrg = srg.Resize(srg.Rows.Count - 1).Offset(1)
    
    srg.AutoFilter 5, "<>0"
    
    Dim svdrg As Range
    On Error Resume Next
        Set svdrg = sdrg.SpecialCells(xlCellTypeVisible)
    On Error GoTo 0
    sws.AutoFilterMode = False
    
    If svdrg Is Nothing Then Exit Sub
    
    Dim arg As Range
    For Each arg In svdrg.Areas
        ' Now do what you need to do with each of the 25 or so datasets, e.g.:
        
        Debug.Print arg.Address
        
    Next arg
    
    MsgBox "Data... done.", vbInformation
    
End Sub

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

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