通过数字循环来创build一个大表

我有一个可行的代码,但我想添加更多的function。 它目前做它应该做的事情,加快了一些进程,但是现在我认为它可以加速。 我正在使用解决scheme,我标记为在这里回答: 使用VBA获得一个阈值

我有这个代码:

Sub OutputEnergyToAllSheets() Dim w For Each w In ThisWorkbook.Worksheets If Not InStr(w.Name, "Total") > 0 And Not InStr(w.Name, "eV") Then OutputEnergyToSheet w.Name End If Next w End Sub Sub OutputEnergyToSheet(TheSheet As String) 'y = Columns to check: 2-25 'x = Rows to check: 2-152 'z = check the next 4 cells Dim x, y, z, check 'Clear the range where we store the #N/A or Energy Outputs With Sheets(TheSheet) .Range("B153:Y153") = vbNullString For y = 2 To 25 For x = 2 To 152 If .Cells(x, y) > .Range("Z2") Then 'If value is greater than Z2 check = True 'Let's check the next 4 For z = 1 To 30 'If any of them fail If .Cells(x + z, y) < .Range("Z2") Then check = False 'The check fails Exit For End If Next z If check = True Then 'If the check doesn't fail .Cells(153, y) = Int(.Cells(x, 1)) 'Set cell 153 to the energy level Exit For End If End If Next x 'If no energy level was set - #N/A If .Cells(153, y) = vbNullString Then .Cells(153, y) = "" Next y End With End Sub 

但是这样说:

 for z = 1 to 30 

我必须以1为增量从0改变到100,它会在所有工作表上输出这些值,然后我转到子项并增加值并重复。 这些值在每个工作表中输出,除了第153行中的一些之外。有没有办法让第153行中的0,第154行中的第155个中的第2个等于100? 我明白这是不可能的,但是我会花上一段时间,因为我必须经历许多练习册的这个过程。 如果能做到这一点,将会为我节省几个单调的忙碌时间。 无论如何,感谢阅读。

对于这个第一个代码块,我试图在你的问题中保留代码的一般结构。 例如,我可以用一个While循环replace最内层的两个For循环。 这样做会更有效率,但需要进行重大的逻辑改变。 我虽然做了一些改变。 我把所有的东西都设置为“N / A”而不是结尾,而且我在最后一个If语句中添加了一个条件。 为了实现检查连续单元格的可变数量的新function,我在计数器zFor循环周围包含了另一个For循环,其中计数器k使得z的终点依赖于k 。 我们打印出第152 + k行。

 Sub OutputEnergyToSheet(TheSheet As String) 'y = Columns to check: 2-25 'x = Rows to check: 2-152 'k = number of matches in a row to find 'z = check the next (k - 1) cells Dim x, y, z, check, k 'Clear the range where we store the N/A or Energy Outputs With Sheets(TheSheet) .Range("B153:Y252") = "N/A" For y = 2 To 25 For x = 2 To 151 If .Cells(x, y) > .Range("Z2") Then 'If value is greater than Z2 For k = 1 To 100 check = True 'Let's check the next k - 1 For z = 1 To k - 1 'If any of them fail If .Cells(x + z, y) <= .Range("Z2") Then check = False 'The check fails Exit For End If Next z If check = True And .Cells(152 + k, y) = "N/A" Then .Cells(152 + k, y) = Int(.Cells(x, 1)) End If Next k End If Next x Next y End With End Sub 

在我做完所有这些之前,我把自己的方法放在一起,这个方法更干净,运行更快。 下面的代码逐步下降行,并保持一个连续匹配find了多less连续的计数。 它消除了很多检查,因为它只检查任何给定的单元格。 最多2个循环! 上面的代码在内部循环中多次检查单元格。 下面的代码可能会更好通过维护数组中的值(在Excel中读/写速度慢)和/或维护一个计数器的最大长度我已经实现了当前列。 我将大部分数字存储为可轻松自信地修改的variables。

 Sub EfficientEnergy(ws As Worksheet) Dim r As Integer, c As Integer, ctr As Integer Dim compVal As Double Dim maxRow As Integer, maxCol As Integer, maxConsecutive As Integer maxRow = 151 maxCol = 25 maxConsecutive = 100 compVal = ws.Cells(2, 26).Value ws.Range(ws.Cells(maxRow + 2, 2), ws.Cells(maxRow + maxConsecutive + 1, maxCol)).Value = "N/A" For c = 2 To maxCol ctr = 0 For r = 2 To maxRow If ws.Cells(r, c).Value > compVal Then ctr = ctr + 1 If ws.Cells(maxRow + 1 + ctr, c).Value = "N/A" Then ws.Cells(maxRow + 1 + ctr, c).Value = ws.Cells(r - ctr + 1, 1).Value End If Else ctr = 0 End If Next r Next c End Sub 

我在testing中用来调用这些方法的代码是(只是注释掉你没有使用的那个):

 Public Sub GetConsecutiveVals() 'OutputEnergyToSheet ("Sheet1") Call EfficientEnergy(ActiveWorkbook.Worksheets("Sheet1")) End Sub 

或者在活动工作簿中的每个工作表上运行(未经testing):

 Public Sub GetConsecutiveVals() Dim ws As Worksheet For Each ws In ActiveWorkbook.Worksheets 'OutputEnergyToSheet (ws.Name) Call EfficientEnergy(ws) Next ws End Sub 

将所有代码放在工作簿中的模块中。 使用Sheet1中的数据打开工作簿(或将上面的代码更改为工作表名称)。 按Alt + F8,然后运行GetConsecutiveVals例程。 如果在对话框窗口中没有看到该方法,请确保带有代码和工作簿的工作簿与数据位于同一个Excel应用程序窗口中

@jack这是我读这个代码。 检查列2 – 25,行2 – 152中的所有单元格,如果其中一个大于Z2,则inputZloop,开始检查接下来的30行,以查看是否有更小的行。 如果是这样的话,如果在单元格153中没有任何操作,那么y =第1列同一行,转到下一列..问题:为什么Z只检查30? 为什么不检查剩余的152 … z = 1到152 – x?

无论如何,我认为这是你想要做的,创造另一个variables说

 DIM Result As Integer Result = 153 ''then below If check = True Then 'If the check doesn't fail ''.Cells(153, y) = Int(.Cells(x, 1)) 'Set cell 153 to the energy level .Cells(Result, y) = Int(.Cells(x, 1)) 'Set cell 153 to the energy level Result = Result + 1 EXIT FOR 

为什么要使用三个循环?

 Sub OutputEnergyToAllSheets() Dim w as worksheet For Each w In ThisWorkbook.Worksheets If Not InStr(1, w.Name, "Total") > 0 And Not InStr(1, w.Name, "eV") Then OutputEnergyToSheet w.Name End If Next w End Sub 

 Sub OutputEnergyToSheet(TheSheet As String) Dim check as Boolean Dim rng as Range Dim c Dim ELevel as integer 'Clear the range where we store the #N/A or Energy Outputs With Sheets(TheSheet) ' set all cells in row 153 = 0 .Range("B153:Y153").value = 0 ELevel = .cells(2,26) ' Your range set rng = .Range(.Cells(2,2), .cells(25, 153)) ' Loop through all cells in range For each c in rng.cells ' If value is greater then Z2 and respective column in row 153 = 0 and cell is not in 153 then change 153 = respective row column 1 If c.value > ELevel and .cells(153, c.column) = 0 and c.row <> 153 Then .cells(153,c.column) = .cells(c.row, 1) ' If value is less then Z2 and cell is not in 153 then change 153 = 0 elseif c.value < ELevel and c.row <> 153 then .cells(153, c.column) = 0 ' Clean up - if cell is in row 153 and value = 0 then change to "N/A" elseif c.row = 153 and c.value = 0 then c.value = "N/A" end if Next c End With End Sub 

如果我误解了,请告诉我