|
本帖最后由 爱吃蜂蜜的狼 于 2019-5-17 16:43 编辑
- Sub huizong()
- Dim sh As Worksheet, k, k1, ar(), r(), rr()
- k1 = Sheet1.Range("c:c").Find("*", , , , , xlPrevious).Row
- '=====================将总表中的省份和销售网点写书数组arr中
- Sheet1.Range("d4:ai" & k1) = ""
- arr = Sheet1.Range("b4:c" & k1)
- For i = 1 To k1 - 3
- If arr(i, 1) = "" Then
- arr(i, 1) = arr(i - 1, 1)
- arr(i, 2) = arr(i, 1) & arr(i, 2)
- Else
- arr(i, 2) = arr(i, 1) & arr(i, 2)
- End If
- Next
- '======================将总表中的表头写入数组arr1中
- arr1 = Sheet1.Range("d2:ai3")
- For ii = 1 To UBound(arr1, 2)
- If arr1(1, ii) = "" Then
- arr1(1, ii) = arr1(1, ii - 1)
- arr1(2, ii) = arr1(1, ii) & arr1(2, ii)
- Else
- arr1(2, ii) = arr1(1, ii) & arr1(2, ii)
- End If
- Next
- '=======================定义数组ar大小与总表中需要输入的单元格范围相同
- ReDim ar(1 To k1 - 3, 1 To 32)
- '==============遍历工作簿的所有工作表(For Each sh In ThisWorkbook.Sheets)
- For Each sh In ThisWorkbook.Sheets
- '==============将非总表的省份和销售网点写入数组r,表头写入数组rr
- If sh.Name <> "总表" Then
- k = sh.Range("c:c").Find("*", , , , , xlPrevious).Row
- k2 = sh.Range("3:3").Find("*", , , , , xlPrevious).Column
- ReDim r(1 To k - 3, 1 To 2)
- ReDim rr(1 To 2, 1 To k2 - 3)
- r = sh.Range("b4:c" & k)
- rr = sh.Range(sh.Cells(2, 4), sh.Cells(3, k2))
- For i1 = 1 To UBound(r)
- If r(i1, 1) = "" Then
- r(i1, 1) = r(i1 - 1, 1)
- r(i1, 2) = r(i1, 1) & r(i1, 2)
- Else
- r(i1, 2) = r(i1, 1) & r(i1, 2)
- End If
- Next
- For i2 = 1 To UBound(rr, 2)
- If rr(1, i2) = "" Then
- rr(1, i2) = rr(1, i2 - 1)
- rr(2, i2) = rr(1, i2) & rr(2, i2)
- Else
- rr(2, i2) = rr(1, i2) & rr(2, i2)
- End If
- Next
- '======================将符合要求的表的内容写入数组rrr()中
- rrr = sh.Range(sh.Cells(4, 4), sh.Cells(k, k2))
- '=======以下通过4个循环的嵌套分别对比总表省份、销售网点、表头的编号、颜色
- For i3 = 1 To UBound(arr)
- For i4 = 1 To UBound(r)
- If arr(i3, 2) = r(i4, 2) Then
- For i5 = 1 To UBound(arr1, 2)
- For i6 = 1 To UBound(rr, 2)
- If arr1(2, i5) = rr(2, i6) Then
- '===========将符合要求的数据进行累加写如事先声明号的数组ar数组中
- ar(i3, i5) = ar(i3, i5) + rrr(i4, i6)
-
- End If
- Next
- Next
- End If
- Next
- Next
- End If
- Next
- '=遍历完成所有的工作表后进行整体赋值(事先声明的数组与总表的区域单元格大小相同)
- Sheet1.Range("d4:ai" & k1) = ar
- End Sub
复制代码
|
|