|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
代码供参考:
Sub FillData()
Dim wsInput As Worksheet
Dim wsOutput As Worksheet
Dim rngInput As Range
Dim rngOutput As Range
Dim startDate As Date
Dim endDate As Date
Dim strengthLevel As String
Dim rowCounter As Long
Dim colCounter As Long
Dim maxEntries As Long
' Set worksheets
Set wsInput = ThisWorkbook.Sheets("试块强度输入")
Set wsOutput = ThisWorkbook.Sheets("抗压强度评定")
' Set input range
Set rngInput = wsInput.Range("B2:K" & wsInput.Cells(wsInput.Rows.Count, "B").End(xlUp).Row)
' Set output range
Set rngOutput = wsOutput.Range("E5:L35")
' Get start date, end date, and strength level
startDate = wsOutput.Range("O3").Value
endDate = wsOutput.Range("O4").Value
strengthLevel = wsOutput.Range("P4").Value
' Initialize counters
rowCounter = 1
colCounter = 1
maxEntries = 248
' Clear output range
rngOutput.ClearContents
' Fill output range
For Each cell In rngInput.Rows
If cell.Columns("B").Value >= startDate And cell.Columns("B").Value <= endDate And cell.Columns("K").Value = strengthLevel Then
rngOutput.Cells(rowCounter, colCounter).Value = cell.Columns("I").Value
colCounter = colCounter + 1
If colCounter > 8 Then ' change rows when reaching the end of a row
colCounter = 1
rowCounter = rowCounter + 1
End If
If rowCounter > 31 Then ' stop when reaching the max number of entries
MsgBox "已达到最大条目数 (" & maxEntries & "). 无法添加更多数据."
Exit Sub
End If
End If
Next cell
End Sub
|
|