For i = 0 To n
If i <= 48 Then 'left section of first page
signatureSheet.Cells(i + 2, 1) = i + 1
signatureSheet.Cells(i + 2, 2) = empname(i)
signatureSheet.Cells(i + 2, 4) = personnum(i)
ElseIf i <= 97 Or i <= 197 Or i <= 297 Or i <= 397 Then 'right section of first
signatureSheet.Cells(i - 47, 5) = i + 1
signatureSheet.Cells(i - 47, 6) = empname(i)
signatureSheet.Cells(i - 47, 8) = personnum(i)
ElseIf i <= 147 Or i <= 247 Or i <= 347 Or i <= 497 Then
signatureSheet.Cells(i - 47, 1) = i + 1
signatureSheet.Cells(i - 47, 2) = empname(i)
signatureSheet.Cells(i - 47, 4) = personnum(i)
End If
Next i
这将从A2:A50粘贴阵列的前49个,从B2:B50粘贴下一个49,其余以50个为单位从A51:A100,B51:B100,A101:A150,B101交替列: B150等是否有更好的方法可以理想地重写它,以便它可以处理无限大小的n?
答案 0 :(得分:2)
友 我试着制作下面的代码。
我想知道你的意见:
1)关于我的逻辑;
2)关于变量的命名。
3)关于重复结构的使用;
4)你认为我可以更多地简化我的逻辑吗?
' Constants
Const INITIAL_ROW As Long = 2
' Variables
Dim lngCounter As Long
Dim lngInternCounter As Long
Dim lngNumberOfReptions As Long
Dim lngNumberOfRegistersPerReptions As Long
Dim lngNumberOfFirstField As Long: lngNumberOfFirstField = 1
Dim lngNumberOfSecondField As Long: lngNumberOfSecondField = 2
Dim lngNumberOfThirdField As Long: lngNumberOfThirdField = 3
Dim lngIndexMinArray As Long
Dim lngIndexMaxArray As Long
Dim lngIndexMinAux As Long
Dim lngIndexMaxAux As Long
Dim lngIndexRowCell As Long
' .
' .
' .
' Variables Initialize
lngNumberOfRegistersPerReptions = 49
lngIndexMinArray = LBound(empname, 1)
lngIndexMaxArray = UBound(empname, 1)
If lngIndexMaxArray <= lngNumberOfRegistersPerReptions Then
lngNumberOfReptions = lngIndexMaxArray + 1
ElseIf lngIndexMinArray = 0 Then
lngNumberOfReptions = (lngIndexMaxArray + 1) / lngNumberOfRegistersPerReptions
Else
lngNumberOfReptions = lngIndexMaxArray / lngNumberOfRegistersPerReptions
End If
With signatureSheet
For lngCounter = 1 To lngNumberOfReptions Step 1
If lngNumberOfReptions = 1 Then
lngIndexMinAux = LBound(empname, 1)
lngIndexMaxAux = UBound(empname, 1)
ElseIf lngCounter > 0 And lngCounter < lngNumberOfReptions Then
lngIndexMaxAux = lngCounter * (lngNumberOfRegistersPerReptions - LBound(empname, 1))
lngIndexMinAux = lngIndexMaxAux - (lngNumberOfRegistersPerReptions - 1)
Else
lngIndexMaxAux = lngCounter * (lngNumberOfRegistersPerReptions - LBound(empname, 1))
lngIndexMinAux = UBound(empname, 1)
End If
lngIndexRowCell = INITIAL_ROW
For lngInternCounter = lngIndexMinAux To lngIndexMaxAux Step 1
.Cells(lngIndexRowCell, lngNumberOfFirstField).Value = lngInternCounter
.Cells(lngIndexRowCell, lngNumberOfSecondField).Value = empname(lngInternCounter)
.Cells(lngIndexRowCell, lngNumberOfThirdField).Value = personnum(lngInternCounter)
lngIndexRowCell = lngIndexRowCell + 1
Next lngInternCounter
Next lngCounter
End With
答案 1 :(得分:1)
也许如果您使用Select Case
,您会注意到If语句的冗余程度。
你在说
If i < 10
- 或 -
If i < 20
- 或 -
If i <= 30 Then ...
这意味着与
完全相同If i <= 30
如果
i < 10
,则始终小于20且始终小于30。
尝试使用Select Case
,以便更好地了解您的目标:
For i = 0 To n
Select Case i
Case 0 To 48
signatureSheet.Cells(i + 2, 1) = i + 1
signatureSheet.Cells(i + 2, 2) = empname(i)
signatureSheet.Cells(i + 2, 4) = personnum(i)
Case 49 To 397 'Your first 3 i <=... are pointless
signatureSheet.Cells(i - 47, 5) = i + 1
signatureSheet.Cells(i - 47, 6) = empname(i)
signatureSheet.Cells(i - 47, 8) = personnum(i)
Case 398 To 497 'same with your if line here
signatureSheet.Cells(i - 47, 1) = i + 1
signatureSheet.Cells(i - 47, 2) = empname(i)
signatureSheet.Cells(i - 47, 4) = personnum(i)
Case Else
Exit For
End Select
Next i