[英]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.