简体   繁体   English

逐行循环浏览大型电子表格,该电子表格已过滤并具有隐藏的行。 如何复制以前的可见行并创建表?

[英]Looping through a large spreadsheet row by row that is filtered and has hidden rows. How do I copy the previous visible rows and make a table?

My program works like this: 我的程序是这样的:

I have a running total of dollar amounts (from $0 to $260m). 我的总金额(从0美元到2.6亿美元)一直在持续。 Each row has data related to the cost. 每行都有与成本相关的数据。 My objective was to extract each row when it goes up by a budget increment eg $5mil, so I would extract the row that has $4.9mil then $9.9mil then $14.9mil (to stay little bit under $5mil) in the running total column. 我的目标是在预算增加时提取每一行,例如增加500万美元,因此我将提取在总行列中依次包含490万美元,990万美元和1490万美元(略低于500万美元)的行。 To do this I created a new column that subtracts $5mil from the column with the running total. 为此,我创建了一个新的列,从该列中减去总运行金额为500万美元。 Starting from $0 running total will give me a negative number until the running total is bigger than the budget increment of $5mil which will then give me a positive number. 从$ 0的总运行额开始,我会得到一个负数,直到总运行额大于预算增量500万美元,然后再给我一个正数。 When it reaches this positive number the running total will be $5+mil but I would want something like $4.9mil. 当它达到这个正数时,运行总额将是$ 5 + mil,但我想要的是$ 490万。 (When it reaches a positive number I add $5mil to the budget increment. I need to extract the previous (last) negative number. (当它达到正数时,我会在预算增量中增加500万美元。我需要提取前一个(最后一个)负数。

Here is an annotated picture: 这是带注释的图片:

在此处输入图片说明

Here is the code I have so far: 这是我到目前为止的代码:

Private Sub CommandButton2_Click()

    Dim number_of_rows As Long
    Dim budget_increment As Long
    Dim rng_data As Range
    Dim prev_visible_row As Object

    number_of_rows = Cells(Rows.Count, 5).End(xlUp).Row
    Set rng_data = Range("I2:I" & number_of_rows)
    budget_increment = 5000000

    For i = 180 To number_of_rows
        Range("I" & i & ":I" & i) = Range("H" & i & ":H" & i) - budget_increment
        If Range("I" & i).Value > 0 Then
            budget_increment = budget_increment + 5000000
            'find out how to copy the last visible row and paste onto new sheet my attempt at solving this (did not work):
            AutoFilter.Range.Offset(i - 1).SpecialCells(xlCellTypeLastCell).Cells.Copy
            'Sheets("Sheet3").Range("T1").Select
            'Sheets("Sheet3").Paste
        End If
    Next
    Application.ScreenUpdating = True
End Sub

Try adding this to the top Dim inc, counter As Integer: counter = 0 尝试将其添加到顶级Dim inc, counter As Integer: counter = 0

and change the If Range("I" & i).Value > 0 Then to : 并将If Range("I" & i).Value > 0 Then更改为:

    If Range("I" & i).Value < Range("I" & (i-1)).Value Then
        For inc = 1 to 999
            If Rows(i-inc).RowHeight > 0 And Range("I" & (i-inc)).Value < 0 Then
                Sheets("Sheet3").Range("A” & (counter + 1) & ":Z" & (counter + 1)).Value = Range("A" & (i-inc) & ":Z" & (i-inc)).Value ' Or something
                counter = counter + 1
                Exit For
            End If
        Next
    End If

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

相关问题 我有一个具有1251行的excel电子表格和一个具有1800行的excel电子表格。 如何找到不在1251行表中的549行? - I have one excel spreadsheet with 1251 rows and one with 1800 rows. How might I find the 549 rows that are not in the 1251 row sheet? 我已经过滤了我的 Excel 数据,现在我想对行进行编号。 我怎么做? - I have filtered my Excel data and now I want to number the rows. How do I do that? 遍历电子表格中的行,在每一行中运行一个函数,然后将结果输出回同一行的末尾 - Looping through rows from a spreadsheet, running a function through each row and outputting the result back to the end of the same row 循环过滤表中的行 - Looping rows in filtered table 使用隐藏的列和已过滤的行进行复制 - Copy with hidden colums and filtered rows 如何将数据行复制到特定的起始行? - How do I copy over rows of data to a specific starting row? 删除表中的过滤行而不是整个行 - Delete Filtered Rows in Table and not entire Row 循环遍历行直到最后一行 - Looping through the rows until last row 包含隐藏行和隐藏列的范围中最后可见的行是什么 - what is last visible row in a range that contain hidden rows and hidden columns 如何减去筛选后的 Excel 表格上的前一行 - How to substract previous row on a filtered Excel table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM