2017-08-21 201 views
-1

我想根据我在另一个单元格中确定的单元格的值填充单元格。VBA根据单元格数量更改单元格的颜色

例如,

  • 如果我把A33="5"然后填写绿5个细胞用C柱。
  • 如果A34="10"则同时填充C列中的另外10个单元格。

当我更改值,然后我希望C列中的单元格的数量和颜色将相应地改变。

我分享了一个样本图片作为附件。如果可以使用VBA?

Sample Sheet

+1

查找到条件格式;它使用Excel标准工具,而不是要求VBA。如果需要VBA,则可以通过VBA输入条件格式。如果不需要条件格式化,另一种选择是使用VBA中的Change_Events。 – Cyril

+2

是的,这是可能的。 – Luuklag

+1

任何你不只是使用堆叠条形图的理由? –

回答

0

这适用于列A〜F.

表事件代码

Private Sub Worksheet_Change(ByVal Target As Range) 
    If Target.Count > 1 Then Exit Sub 
    If Not Intersect(Range("a33", "f36"), Target) Is Nothing Then 
     setColor Target.Column 
    End If 
End Sub 

模块代码

Sub setColor(col As Integer) 
    Dim vDB, vColor 
    Dim i As Integer, c As Integer, n As Integer 

    vColor = Array(RGB(244, 185, 79), RGB(0, 180, 255), RGB(255, 54, 54), RGB(116, 211, 109)) 
    Range(Cells(1, col), Cells(32, col)).Interior.Color = RGB(36, 36, 36) 
    vDB = Cells(33, col).Resize(4) 
    For i = 1 To 4 
     n = vDB(i, 1) 
     c = c + n 
     If IsEmpty(vDB(i, 1)) Then 
     Else 
      Cells(32, col).Offset(-c, 0).Resize(n).Interior.Color = vColor(i - 1) 
     End If 
    Next i 

End Sub 

enter image description here

+0

,感谢您的支持。非常感谢 –

+0

@SemihUral,祝你好运。 –

+0

,非常感谢。顺便说一句,我想增加数量,让我们假设我把它放在100个A33它会带给我调试。为了避免是和如果我需要使用100,我需要修改哪些部分? –

0
Sub setColor(col As Integer) 
    Dim vDB, vColor 
    Dim i As Integer, c As Integer, n As Integer 

    vColor = Array(RGB(244, 185, 79), RGB(0, 180, 255), RGB(255, 54, 54), RGB(116, 211, 109)) 
    Range(Cells(1, col), Cells(50, col)).Interior.Color = RGB(36, 36, 36) 
    vDB = Cells(51, col).Resize(4) 
    For i = 1 To 10 
     n = vDB(i, 1) 
     c = c + n 
     If IsEmpty(vDB(i, 1)) Or c > 49 Then 
     Else 
      Cells(50, col).Offset(-c, 0).Resize(n).Interior.Color = vColor(i - 
     End If 
    Next i 

End Sub 
0

子的setColor(COL作为整数) 昏暗VDB,vColor 昏暗我作为整数,c以整数,n作为整数

vColor = Array(RGB(244, 185, 79), RGB(0, 180, 255), RGB(255, 54, 54), RGB(116, 211, 109)) 
Range(Cells(1, col), Cells(50, col)).Interior.Color = RGB(36, 36, 36) 
vDB = Cells(51, col).Resize(4) 
For i = 1 To 4 '<~~ your if your range is "a51,f56", then it sould be 4 
n = vDB(i, 1) 
    c = c + n 
    If IsEmpty(vDB(i, 1)) Or c > 49 Then 
    Else 
     Cells(50, col).Offset(-c, 0).Resize(n).Interior.Color = vColor(i - 1) 
    End If 
Next i 

结束子

相关问题