level 7
想写一个vba代码,for each rng in [a1:f1,如果rng包含“库存数量“字样,则rng整列往左偏移到第一个rng包含“库存数量“的列后面,比如标题行A1=1月库存数量,b1=1月库存金额,c1=2月库存数量,d1=2月库存金额,我要实现A1=1月库存数量,b1=2月库存数量,c1=1月库存金额,d1=2月库存金额,就是先把库存数量排了,再到库存金额
2024年08月28日 05点08分
2
level 1
沟通有点费劲啊,你试下是不是你想要的效果
Sub demo()
Dim arr, brr, crr, res, dic As Object, i&, j%, col%, lastRow&, n%
col = Cells(1, Columns.Count).End(1).Column
lastRow = Cells(Rows.Count, "n").End(3).Row
arr = Range("n1", Cells(lastRow, col))
Set dic = CreateObject("scripting.dictionary")
dic("数量") = "": dic("金额") = ""
For i = 1 To UBound(arr, 2)
If InStr(arr(1, i), "数量") Then
dic("数量") = dic("数量") & "," & i
Else
dic("金额") = dic("金额") & "," & i
End If
Next i
brr = Split(Mid(dic("数量"), 2), ",")
crr = Split(Mid(dic("金额"), 2), ",")
ReDim res(1 To UBound(arr), 1 To UBound(arr, 2))
For i = 0 To UBound(brr)
n = n + 1
For j = 1 To UBound(arr)
res(j, n) = arr(j, Val(brr(i)))
res(j, n + UBound(arr, 2) / 2) = arr(j, Val(crr(i)))
Next j
Next i
Columns("n").Resize(, UBound(res, 2)) = ""
Range("n1").Resize(UBound(res), UBound(res, 2)) = res
End Sub
2024年08月28日 06点08分
7
厉害啊,是这样的
2024年08月28日 07点08分
这个代码我发现了一个问题,就是数量和金额如果不是1:1的,那么运算的结果就不对了,就是假如有11个数量,有17个金额,那么就不对了。
2024年09月04日 05点09分