|
楼主 |
发表于 2020-7-30 23:38
|
显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
Option Explicit
Sub 测试()
Dim arr, brr, mark, i, j, k, kk, m, t
Dim ds, dw, dz, dy, dx, mk, xh, wz, wy, hh, zh, sl, dh, zm, eow As Integer
Dim sn, x
Sheets("预览").Rows("2:5000").Clear '清除预览工作表第二行往下所有的数据
With Sheets("数据表")
dw = .Range("下长").Column
dz = .Range("定左").Column
dy = .Range("定右").Column
ds = .Range("定上").Column
dx = .Range("定下").Column
mk = .Range("门宽").Column
wz = .Range("左开").Column
wy = .Range("右开").Column
hh = .Range("行号").Column
zh = .Range("组号").Column
sl = .Range("数量").Column
dh = .Range("单号").Column
zm = .Range("字母").Column
sn = .Range("顺逆").Column
eow = .Cells(1, sn).End(xlDown).row
arr = .Cells(3, sn).Resize(eow - 2, dw - sn + 1).Value '表1-10列数据
x = 18
brr = .Cells(3, dw + 1).Resize(UBound(arr, 1), 4 * x).Value '表10列往后的数据
mark = .Cells(1, dw + 1).Resize(, UBound(brr, 2)).Value '模具keys
End With
ReDim crr(1 To UBound(arr, 1) * 4, 1 To 6 + 2 * x)
'建立动态数组crr为全部数值=6可以替换为数据前面总列数,意思是前面6列逐列赋值,后面x列
'由于是一对一对的所以是两个两个的赋值即2x
'4=后面有几组数据,本例中有四组数据,故=4
For i = 1 To UBound(arr, 1) '行循环
For j = 7 To 10 '列循环
If arr(i, j) > 0 Then
m = m + 1: crr(m, 1) = m - 1: crr(m, 2) = arr(i, 3): crr(m, 3) = arr(i, j) '将crr1-3列赋值:序号+组好+总长,m为crr的行坐标
For k = 4 To 6 'crr 第i行第4列-第6列,数量,樘号,子母列=值循环方式放入crr
crr(m, k) = arr(i, k) '4-6列赋值给crr
Next
For k = (j - 7) * x + 1 To (j - 7) * x + x 'crr数据列7-18列循环赋值
crr(m, 2 * (k - ((j - 7) * x + 1)) + 7) = mark(1, k) 'crr 模具 7’9‘11’13‘15’17列赋值
crr(m, 2 * (k - ((j - 7) * x + 1)) + 8) = IIf(brr(i, k) > 0, brr(i, k), 10 ^ 8) 'crr 位置 8,10,12,14,16,18列赋值且将0值替换为100000
Next
'====以上将当前行全部赋值完成,以下为排序去空行
'MsgBox UBound(crr, 2)
For k = 7 To UBound(crr, 2) - 3 Step 2
For kk = 7 To UBound(crr, 2) + 4 - k Step 2
If crr(m, kk + 1) > crr(m, kk + 3) Then
t = crr(m, kk): crr(m, kk) = crr(m, kk + 2): crr(m, kk + 2) = t
t = crr(m, kk + 1): crr(m, kk + 1) = crr(m, kk + 3): crr(m, kk + 3) = t
End If
Next
Next
For k = 8 To UBound(crr, 2) Step 2
If crr(m, k) = 10 ^ 8 Then crr(m, k) = vbNullString: crr(m, k - 1) = crr(m, k)
Next
End If
Next
Next
Sheets("预览").[a2].Resize(m, UBound(crr, 2)) = crr
End Sub
小刀老师,根据您给的代码,我拿来主义,直接copy啦,终于挪到我的表格上了,再三感谢!!!
还有做了些注解,花了好些功夫才理解了一半,下面没有注解的您能给解释下吗,实在看不懂了! |
|