如果我打印电子表格,我正在尝试将我的最后一行数据放在最后一页底部。每页的最后一行为= 81 + 80x。所以我要插入所需的行数。我需要帮助循环下面的代码,所以我不需要200行代码的潜在100页。
下面的代码可以工作,但我需要增加100行其他行,增加80倍的倍数才能完成我的目标。
Sub Draft()
Dim LR As Long
LR = Range("A" & Rows.Count).End(xlUp).Row
If LR < 81 Then
Range("A" & LR).EntireRow.Resize(81 - LR).Insert Shift:=xlDown
ElseIf LR < 161 Then
Range("A" & LR).EntireRow.Resize(161 - LR).Insert Shift:=xlDown
ElseIf LR < 241 Then
Range("A" & LR).EntireRow.Resize(241 - LR).Insert Shift:=xlDown
ElseIf LR < 321 Then
Range("A" & LR).EntireRow.Resize(321 - LR).Insert Shift:=xlDown
ElseIf LR < 401 Then
Range("A" & LR).EntireRow.Resize(401 - LR).Insert Shift:=xlDown
End If
End Sub发布于 2022-03-09 20:09:41
这对你有用吗?
诀窍是在数据行附近计算工作表的底部行。如果你低于这个底线,那么你的计算底线也是你的目标线。如果您的数据超过了计算表的底线,那么将下一个底线设置为您的目标。
通过对除法使用长数据类型,可以消除结果变量shtNum中不必要的小数点数。获得此数字使您能够在整个工作表步骤中进行计算。
转换到数据类型long是四舍五入的工作表编号,底线是最接近你的数据。
Sub Draft2()
Dim LR As Long
Dim shtNum As Long
LR = Range("A" & Rows.Count).End(xlUp).Row
shtNum = LR / 80
If (LR < (80 * shtNum + 1)) Then
' Datas are inside of the 80 Rows of the page. So we set them to the bottom of the page
Range("A" & LR).EntireRow.Resize((80 * shtNum + 1) - LR).Insert Shift:=xlDown
ElseIf (LR = (80 * shtNum + 1)) Then
'do nothing, we are at the right row
ElseIf (LR > (80 * shtNum + 1)) Then
' Datas are off the page. put them on the bottom of the next page
shtNum = shtNum + 1
Range("A" & LR).EntireRow.Resize((80 * shtNum + 1) - LR).Insert Shift:=xlDown
End If
End Sub发布于 2022-03-09 20:34:13
谢谢你的帮助。这是我找到并使用的解决方案:
Sub Draft()
Dim LR As Long
LR = Range("E" & Rows.Count).End(xlUp).Row
Dim j, x As Integer
For x = 0 To 200
j = 81 + (80 * x)
If LR < j Then
Range("A" & LR).EntireRow.Resize(j - LR).Insert Shift:=xlDown
Exit For
End If
Next x
End Subhttps://stackoverflow.com/questions/71415168
复制相似问题