'Declare PtrSafe Function timeGetTime Lib "winmm.dll" () As Long
'52pojie.com
Sub 编号()
Dim 编号表 As String, 左上角 As String, 右下角 As String, 编号位置 As String, 输出编号 As String, 原数据区域 As Range, 复制到区域 As Range, 复制区高 As Integer, 复制区宽 As Integer, 复制列个数 As Long, 复制行个数 As Long, 编号前缀 As String, 编号后缀 As String, 编号位数 As Long, 起始编号 As Long, 编号步距 As Long, 编号所在单元格横向 As Integer, 编号所在单元格纵向 As Integer, i As Long, j As Long, 每页放几行 As Integer
编号表 = Worksheets("批量编号设置").[b1].Value
左上角 = Worksheets("批量编号设置").[b2].Value
右下角 = Worksheets("批量编号设置").[b3].Value
每页放几行 = Worksheets("批量编号设置").[D4].Value
复制区宽 = Worksheets(编号表).Range(Worksheets(编号表).Range(左上角), Worksheets(编号表).Range(右下角)).Columns.Count
复制区高 = Worksheets(编号表).Range(Worksheets(编号表).Range(左上角), Worksheets(编号表).Range(右下角)).Rows.Count
'编号位置 = Worksheets("批量编号设置").[b6].Value
'删除无用的
'删除右下
If Worksheets("批量编号设置").[d3] = "" Then
Worksheets(编号表).Rows(Worksheets(编号表).Range(右下角).Offset(1).Row & ":" & Worksheets(编号表).Rows.Count).Delete
Worksheets(编号表).Columns(Worksheets(编号表).Range(右下角).Column + 1).Resize(, Worksheets(编号表).Columns.Count - Worksheets(编号表).Range(右下角).Column).Delete
'删除左上
If Worksheets(编号表).Range(左上角).Column <> 1 Then
Worksheets(编号表).Columns(1).Resize(, Worksheets(编号表).Range(左上角).Column - 1).Delete
End If
If Worksheets(编号表).Range(左上角).Row <> 1 Then
Worksheets(编号表).Rows(1).Resize(Worksheets(编号表).Range(左上角).Row - 1).Delete
End If
End If
Set 原数据区域 = Worksheets(编号表).Range(Worksheets(编号表).Cells(1, 1), Worksheets(编号表).Cells(复制区高, 复制区宽))
'复制数量取整
Do While Worksheets("批量编号设置").[b5].Value Mod Worksheets("批量编号设置").[b4].Value <> 0
Worksheets("批量编号设置").[b5].Value = Worksheets("批量编号设置").[b5].Value + 1
Loop
'批量复制
复制列个数 = Worksheets("批量编号设置").[b4].Value
复制行个数 = Worksheets("批量编号设置").[b5].Value / Worksheets("批量编号设置").[b4].Value
'复制格式
'列复制带列宽 ,感谢pzx_240反映的bug。
Worksheets(编号表).Select
If 复制列个数 > 1 Then
Worksheets(编号表).Columns(1).Resize(, 复制区宽).Copy
Worksheets(编号表).Columns(1 + 复制区宽).Resize(, (复制列个数 - 1) * 复制区宽).Select
ActiveSheet.Paste
End If
'行复制带行高
Worksheets(编号表).Select
Worksheets(编号表).Rows(1).Resize(复制区高).Copy
Worksheets(编号表).Rows(1 + 复制区高).Resize((复制行个数 - 1) * 复制区高).Select
ActiveSheet.Paste
Application.CutCopyMode = False
'编号 '加分页符
''老版本
' For j = 0 To 复制行个数 - 1
' For i = 0 To 复制列个数 - 1
' 输出编号 = CStr(起始编号)
' Do While Len(输出编号) < 编号位数
' 输出编号 = "0" & 输出编号
' Loop
' 输出编号 = 编号前缀 & 输出编号 & 编号后缀
' Worksheets(编号表).Cells(j * 复制区高 + 编号所在单元格纵向, i * 复制区宽 + 编号所在单元格横向).Value = 输出编号
' 起始编号 = 起始编号 + 编号步距
' Next i
' If (j + 1) Mod 每页放几行 = 0 Then
' Worksheets(编号表).HPageBreaks.Add Before:=Worksheets(编号表).Cells((j + 1) * 复制区高 + 编号所在单元格纵向, 1)
' End If
' Next j
'新版
'循环第一层,有多少列编号
Dim m As Long '列轮询
Dim n As Long '行轮询
Dim 分页符数量 As Long
分页符数量 = Worksheets("批量编号设置").[b5].Value / Worksheets("批量编号设置").[b4].Value / 每页放几行
m = 2
Do While (Worksheets("批量编号设置").Cells(6, m).Value <> "")
n = 12
编号位置 = Worksheets("批量编号设置").Cells(6, m).Value
编号前缀 = Worksheets("批量编号设置").Cells(7, m).Value
编号后缀 = Worksheets("批量编号设置").Cells(8, m).Value
编号位数 = Worksheets("批量编号设置").Cells(9, m).Value
起始编号 = Worksheets("批量编号设置").Cells(10, m).Value
编号步距 = Worksheets("批量编号设置").Cells(11, m).Value
编号所在单元格横向 = Worksheets(编号表).Range(编号位置).Column - Worksheets(编号表).Range(左上角).Column + 1
编号所在单元格纵向 = Worksheets(编号表).Range(编号位置).Row - Worksheets(编号表).Range(左上角).Row + 1
'判断是否为预设序列
If (Worksheets("批量编号设置").Cells(10, m) <> "") Then
'不是预设序列则执行步距编号
For j = 0 To 复制行个数 - 1
For i = 0 To 复制列个数 - 1
输出编号 = CStr(起始编号)
Do While Len(输出编号) < 编号位数
输出编号 = "0" & 输出编号
Loop
输出编号 = 编号前缀 & 输出编号 & 编号后缀
Worksheets(编号表).Cells(j * 复制区高 + 编号所在单元格纵向, i * 复制区宽 + 编号所在单元格横向).Value = 输出编号
起始编号 = 起始编号 + 编号步距
Next i
Next j
Else
'是预设序列则按预设序列编号,留空的留空,有多少数据用多少,
'拍号可能有很多,既然序列就不加判断了
'如果编号位数不为空,则自动补充0
For j = 0 To 复制行个数 - 1
For i = 0 To 复制列个数 - 1
输出编号 = Worksheets("批量编号设置").Cells(n, m)
If (Not (IsEmpty(Cells(9, 2)))) Then
Do While Len(输出编号) < 编号位数
输出编号 = "0" & 输出编号
Loop
End If
输出编号 = 编号前缀 & 输出编号 & 编号后缀
Worksheets(编号表).Cells(j * 复制区高 + 编号所在单元格纵向, i * 复制区宽 + 编号所在单元格横向).Value = 输出编号
n = n + 1
Next i
Next j
End If
m = m + 1
Loop
'插分页符
For i = 1 To 分页符数量
Worksheets(编号表).HPageBreaks.Add Before:=Worksheets(编号表).Cells((i) * 复制区高 * 每页放几行 + 1, 1)
Next i
'
' If (j + 1) Mod 每页放几行 = 0 Then
' Worksheets(编号表).HPageBreaks.Add Before:=Worksheets(编号表).Cells((j + 1) * 复制区高 + 编号所在单元格纵向, 1)
' End If
'页面居中,列居中
Worksheets(编号表).PageSetup.FitToPagesWide = 1
Worksheets(编号表).PageSetup.CenterVertically = True
Worksheets(编号表).PageSetup.CenterHorizontally = True
Worksheets(编号表).Select
Worksheets(编号表).UsedRange.Select
'Worksheets(编号表).PrintPreview
'Application.Dialogs(xlDialogPrint).Show
End Sub
'52pojie.com
'更新于:20200217 by etkane
'https://www.52pojie.cn/thread-1029960-1-1.html
'Declare PtrSafe Function timeGetTime Lib "winmm.dll" () As Long
'52pojie.com
Sub 编号()
Dim 编号表 As String
Dim 左上角 As String
Dim 右下角 As String
Dim 编号位置 As String
Dim 输出编号 As String
Dim 原数据区域 As Range
Dim 复制到区域 As Range
Dim 复制区高 As Integer
Dim 复制区宽 As Integer
Dim 复制列个数 As Long
Dim 复制行个数 As Long
Dim 编号前缀 As String
Dim 编号后缀 As String
Dim 编号位数 As Long
Dim 起始编号 As Long
Dim 编号步距 As Long
Dim 编号所在单元格横向 As Integer
Dim 编号所在单元格纵向 As Integer
Dim i As Long
Dim j As Long
Dim 每页放几行 As Integer
编号表 = Worksheets("批量编号设置").[b1].Value
左上角 = Worksheets("批量编号设置").[b2].Value
右下角 = Worksheets("批量编号设置").[b3].Value
编号位置 = Worksheets("批量编号设置").[b6].Value
编号前缀 = Worksheets("批量编号设置").[b7].Value
编号后缀 = Worksheets("批量编号设置").[b8].Value
编号位数 = Worksheets("批量编号设置").[b9].Value
起始编号 = Worksheets("批量编号设置").[b10].Value
编号步距 = Worksheets("批量编号设置").[b11].Value
每页放几行 = Worksheets("批量编号设置").[D4].Value
复制区宽 = Worksheets(编号表).Range(Worksheets(编号表).Range(左上角), Worksheets(编号表).Range(右下角)).Columns.Count
复制区高 = Worksheets(编号表).Range(Worksheets(编号表).Range(左上角), Worksheets(编号表).Range(右下角)).Rows.Count
编号所在单元格横向 = Worksheets(编号表).Range(编号位置).Column - Worksheets(编号表).Range(左上角).Column + 1
编号所在单元格纵向 = Worksheets(编号表).Range(编号位置).Row - Worksheets(编号表).Range(左上角).Row + 1
'删除无用的
'删除右下
If Worksheets("批量编号设置").[d3] = "" Then
Worksheets(编号表).Rows(Worksheets(编号表).Range(右下角).Offset(1).Row & ":" & Worksheets(编号表).Rows.Count).Delete
Worksheets(编号表).Columns(Worksheets(编号表).Range(右下角).Column + 1).Resize(, Worksheets(编号表).Columns.Count - Worksheets(编号表).Range(右下角).Column).Delete
'删除左上
If Worksheets(编号表).Range(左上角).Column <> 1 Then
Worksheets(编号表).Columns(1).Resize(, Worksheets(编号表).Range(左上角).Column - 1).Delete
End If
If Worksheets(编号表).Range(左上角).Row <> 1 Then
Worksheets(编号表).Rows(1).Resize(Worksheets(编号表).Range(左上角).Row - 1).Delete
End If
End If
Set 原数据区域 = Worksheets(编号表).Range(Worksheets(编号表).Cells(1, 1), Worksheets(编号表).Cells(复制区高, 复制区宽))
'复制数量取整
Do While Worksheets("批量编号设置").[b5].Value Mod Worksheets("批量编号设置").[b4].Value <> 0
Worksheets("批量编号设置").[b5].Value = Worksheets("批量编号设置").[b5].Value + 1
Loop
'批量复制
复制列个数 = Worksheets("批量编号设置").[b4].Value
复制行个数 = Worksheets("批量编号设置").[b5].Value / Worksheets("批量编号设置").[b4].Value
'复制格式
'列复制带列宽
Worksheets(编号表).Select
Worksheets(编号表).Columns(1).Resize(, 复制区宽).Copy
Worksheets(编号表).Columns(1 + 复制区宽).Resize(, (复制列个数 - 1) * 复制区宽).Select
ActiveSheet.Paste
'行复制带行高
Worksheets(编号表).Select
Worksheets(编号表).Rows(1).Resize(复制区高).Copy
Worksheets(编号表).Rows(1 + 复制区高).Resize((复制行个数 - 1) * 复制区高).Select
ActiveSheet.Paste
Application.CutCopyMode = False
'编号 '加分页符
For j = 0 To 复制行个数 - 1
For i = 0 To 复制列个数 - 1
输出编号 = CStr(起始编号)
Do While Len(输出编号) < 编号位数
输出编号 = "0" & 输出编号
Loop
输出编号 = 编号前缀 & 输出编号 & 编号后缀
Worksheets(编号表).Cells(j * 复制区高 + 编号所在单元格纵向, i * 复制区宽 + 编号所在单元格横向).Value = 输出编号
起始编号 = 起始编号 + 编号步距
Next i
If (j + 1) Mod 每页放几行 = 0 Then
Worksheets(编号表).HPageBreaks.Add Before:=Worksheets(编号表).Cells((j + 1) * 复制区高 + 编号所在单元格纵向, 1)
End If
Next j
'页面居中,列居中
Worksheets(编号表).PageSetup.FitToPagesWide = 1
Worksheets(编号表).PageSetup.CenterVertically = True
Worksheets(编号表).PageSetup.CenterHorizontally = True
Worksheets(编号表).Select
Worksheets(编号表).UsedRange.Select
'Worksheets(编号表).PrintPreview
'Application.Dialogs(xlDialogPrint).Show
End Sub
'52pojie.com