|
选定区域 选定行列数 转换
Sub Aki()
'功能 :将选中区域按照 inputbox 输入的 两个参数分割并合并,将结果放置在所选单元格
Dim rng As Range, a As Variant
Dim lr As Integer, lr2 As Integer
Dim lc As Integer, lc2 As Integer
Dim i As Integer, J As Integer, t As Integer, u As Integer, rowint As Integer, colint As Integer, bol As Integer
Dim arr
Dim BRR
On Error GoTo msg
Set rng = Selection
lr = rng.Rows.Count '选中区域总行数
lc = rng.Columns.Count '选中区域总列数
a = Split(Application.InputBox("请输入2个参数,以,隔开:" & vbCrLf & "分割的列数 ,分割的行数" & vbCrLf & "(分割方向 向右&向下)"), ",")
colint = a(0) '分割列数
rowint = a(1) '分割行数
If lc Mod colint <> 0 Then
MsgBox "分割列数只能为区域总列数整除的倍数!"
Exit Sub
ElseIf lr Mod rowint <> 0 Then
MsgBox "分割行数只能为区域总行数整除的倍数!"
Exit Sub
End If
i = lr * lc / colint
ReDim BRR(1 To i, 1 To colint)
arr = rng
m = 1
u1 = 1
If rowint > 1 Then
For u = 1 To (lr * lc) / (colint * rowint) '根据行列得出分割块数
For t = 1 To colint * rowint '每个分割块内单元格总数
Y = Y + 1
y1 = y1 + 1
BRR(u1, y1) = arr(m, Y)
'MsgBox BRR(u1, y1)
If t Mod colint = 0 And t <> 1 And t < colint * rowint Then '循环到每个分割块第一行右边一列时 □□■
m = m + 1 '□□□
If t Mod colint = 0 Then '□□□
Y = Y - colint
Else
Y = 0
End If
y1 = 0
u1 = u1 + 1
ElseIf t Mod colint = 0 And t = colint * rowint And u * colint Mod lc <> 0 Then '循环到每个分割块右边一列时 且最后一个单元格时 □□□
m = m - rowint + 1 '□□□
u1 = u1 + 1 '□□■
y1 = 0
ElseIf t Mod colint = 0 And t <> colint * rowint Then '循环到分割块右边一列时 且 非最后一个单元格时 '□□□
m = m + 1 '□□■
u1 = u1 + 1 '□□□
If t Mod colint = 0 Then
Y = Y - colint
Else
Y = 0
End If
y1 = 0
ElseIf t Mod colint = 0 And t = colint * rowint And u * colint Mod lc = 0 Then '循环到区域右侧分割块最后一个单元格时 □□□ □□□
m = m + 1 '□□□ □□□
Y = 0 '□□□ □□■
y1 = 0
u1 = u1 + 1
Else
End If
Next
Next
Else
For u = 1 To lr * lc / colint
For t = 1 To colint * rowint
Y = Y + 1
BRR(u, t) = arr(m, Y)
If u * colint Mod lc = 0 And t = colint Then
m = m + 1
Else
End If
If Y = lc Then
Y = 0
Else
End If
Next
Next
End If
Set rng = Application.InputBox("请选择结果存放单元格:", Type:=8)
Range(rng.Address).Resize(i, colint) = BRR
Exit Sub
msg:
MsgBox ("分割参数错误,请确认!")
Exit Sub
End Sub
|
|