|
楼主 |
发表于 2016-6-16 15:44
|
显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
本帖最后由 longe1013 于 2016-6-17 08:51 编辑
唉,找了半天没人帮忙 自己做下来想了想,无意中想到了个方式,已经实现,了把代码贴出来记录下:- Sub 自由组合()
- Application.ScreenUpdating = False
- Dim num(0 To 8), i%, j%, k%, l%, s%
- Dim carr()
-
- num(0) = Sheet1.Cells(1, Columns.Count).End(1).Column
- Sheet1.Columns(num(0) + 1).Clear
- If Sheet1.Cells(1, num(0)) <> "组合" Or Sheet1.Cells(Rows.Count, num(0)).End(3).Row < 2 Then
- MsgBox "请设定组合序列"
- Exit Sub
- End If
-
- s = 1
- For i = 2 To Sheet1.Cells(Rows.Count, num(0)).End(3).Row
- myarr = Split(Sheet1.Cells(i, num(0)), ":")
- num(1) = UBound(myarr)
- If num(1) >= 1 Then
- For j = 0 To num(1) - 1
- m = CInt(myarr(j))
- n = CInt(myarr(j + 1))
- If num(4) <> 1 Then
- num(5) = Sheet1.Cells(Rows.Count, m).End(3).Row - 1
- num(6) = num(5)
- If num(5) < 2 Then num(5) = 2
- aarr = Sheet1.Cells(2, m).Resize(num(5))
- End If
- If num(6) < 2 Then
- num(2) = UBound(aarr) - 1
- Else
- num(2) = UBound(aarr)
- End If
- num(7) = Sheet1.Cells(Rows.Count, n).End(3).Row - 1
- num(8) = num(7)
- If num(7) < 2 Then num(7) = 2
- barr = Sheet1.Cells(2, n).Resize(num(7))
- If num(8) < 2 Then
- num(3) = UBound(barr) - 1
- Else
- num(3) = UBound(barr)
- End If
- ReDim carr(1 To num(2) * num(3), 1 To 1)
- For k = 1 To num(2)
- For l = 1 To num(3)
- carr(s, 1) = aarr(k, 1) & barr(l, 1)
- s = s + 1
- Next
- Next
- aarr = carr
- num(6) = UBound(aarr)
- If num(6) = 1 Then num(6) = 2
- Erase carr
- s = 1
- num(4) = 1
- Next
- End If
- Sheet1.Cells(Sheet1.Cells(Rows.Count, num(0) + 1).End(3).Row + 1, num(0) + 1).Resize(UBound(aarr)) = aarr
- Erase aarr
- num(4) = 0
- Next
- MsgBox "组合完成!"
- Application.ScreenUpdating = True
- End Sub
复制代码
|
|