我正在嘗試創建一個工具來生成連續性和隔離檢查。
這是我想要的順序:

我基本上是嘗試在我的表格右側創建一個序列,以生成遞減模式。1 到 5、2 到 5、3 到 5 等等。
在左邊,每組數字都在 1 旁邊,然后是 2,等等。就像圖片一樣。
到目前為止,這是我的代碼。這里有一些序列,我不知道如何為以下內容創建代碼:
Private Sub CommandButton1_Click()
Dim i As Integer
For i = 1 To 5
Cells(i, 4).Value = i '' 1 -15 starting after the 15th integer
Cells(i 4, 4).Value = i '' 2 -15 starting after the 15th integer
Cells(i 7, 4).Value = i '' 3 -15 starting after the 15th integer
Cells(i 9, 4).Value = i
Cells(i 10, 4).Value = i
Next i
End Sub
接下來我可以嘗試什么?
uj5u.com熱心網友回復:
您需要 2 個嵌套回圈:
n = 5
row = 1
for i = 1 to n
for j = i to n
cells(row, 1) = i
cells(row, 2) = j
row = row 1
next
Next
uj5u.com熱心網友回復:
比回圈中重復寫入單元格更快的陣列版本:
Option Explicit
Private Sub Test()
GenerateDiminishingPattern 5
End Sub
Private Sub GenerateDiminishingPattern(argLimit As Long)
Const startRow As Long = 1
Const repeatCol As Long = 1 'Column A
Const diminishingCol As Long = 4 'Column D
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Sheet1") 'Change the sheet name accordingly
'=== Uncomment if you need to apply strikethrough to the first value of each loop
'ws.Columns(repeatCol).Clear
'ws.Columns(diminishingCol).Clear
'=== Determine the size for array based on sum of consecutive number
Dim outputSize As Long
outputSize = (argLimit * (argLimit 1)) / 2
Dim repeatOutput() As Long
Dim diminishingOutput() As Long
ReDim repeatOutput(1 To outputSize, 1 To 1) As Long
ReDim diminishingOutput(1 To outputSize, 1 To 1) As Long
Dim i As Long
Dim j As Long
Dim rowIndex As Long
rowIndex = 1
For i = 1 To argLimit
'=== Uncomment if you need to apply strikethrough to the first value of each loop
'ws.Cells(startRow, repeatCol).Offset(rowIndex - 1).Font.Strikethrough = True
'ws.Cells(startRow, diminishingCol).Offset(rowIndex - 1).Font.Strikethrough = True
For j = i To argLimit
repeatOutput(rowIndex, 1) = i
diminishingOutput(rowIndex, 1) = j
rowIndex = rowIndex 1
Next j
Next i
'Write output to worksheet
ws.Cells(startRow, repeatCol).Resize(outputSize).Value = repeatOutput
ws.Cells(startRow, diminishingCol).Resize(outputSize).Value = diminishingOutput
Erase repeatOutput
Erase diminishingOutput
Set ws = Nothing
End Sub
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/364558.html
下一篇:檢測具有特定值的單元格的變化
