EXCEL vba子例程范围更新的传递信

EXCEL vba子例程范围更新的传递信,excel,vba,subroutine,Excel,Vba,Subroutine,我想缩短这段代码,我知道有可能我只是不知道正确的语法 我想创建一个子例程来更新字体颜色,并通过仅更改第一个单元格字母从if语句调用它 有人介意帮忙吗?这样我就可以记住这一点,以备将来参考 提前感谢, 编辑:我只是想澄清一下,我想这样做 If Not Intersect(Target, Range("C3")) Is Nothing Then 'do the following if e3 is updated 'reset all font colors to black Range(

我想缩短这段代码,我知道有可能我只是不知道正确的语法

我想创建一个子例程来更新字体颜色,并通过仅更改第一个单元格字母从if语句调用它

有人介意帮忙吗?这样我就可以记住这一点,以备将来参考

提前感谢,

编辑:我只是想澄清一下,我想这样做

    If Not Intersect(Target, Range("C3")) Is Nothing Then 'do the following if e3 is updated

'reset all font colors to black
Range("C8:K9,C14:K14,C37:K37").Font.ColorIndex = 1
Range("C7:K7,C11:K11,C13:K13,C23:K23,C28:K28").Font.ColorIndex = 1
Range("C10:K10,C15:K16").Font.ColorIndex = 1
Range("C17:K17").Font.ColorIndex = 1
Range("C18:K18,C33:K33").Font.ColorIndex = 1
Range("C24:K26,C29:K31").Font.ColorIndex = 1
Range("C32:K32").Font.ColorIndex = 1
Range("C36:K36,C38:K38").Font.ColorIndex = 1

    If Range("C3") = "1" Then ' do the following if C3 is 1
         updateFontColor c
    ElseIf Range("C3") = "2" Then ' do the following if C3 is 2
        updateFontColor d
    ElseIf Range("C3") = "3" Then ' do the following if C3 is 3
          updateFontColor e 
    ElseIf Range("C3") = "4" Then ' do the following if C3 is 4
        updateFontColor f

    ElseIf Range("C3") = "5" Then ' do the following if C3 is 5
        updateFontColor g

    ElseIf Range("C3") = "6" Then ' do the following if C3 is 6
              updateFontColor h     
    ElseIf Range("C3") = "7" Then ' do the following if C3 is 7
            updateFontColor i       
    ElseIf Range("C3") = "8" Then ' do the following if C3 is 8
            updateFontColor j       
    ElseIf Range("C3") = "9" Then ' do the following if C3 is 9
           updateFontColor k
               End If
End If

Sub updateFontColor(x As Range)
        Range("x8:K9,x14:x14,x37:K37").Font.ColorIndex = 2
        Range("x7:K7,x11:K11,x13:K13,x23:K23,x28:K28").Font.ColorIndex = 39
        Range("x10:K10,x15:K16").Font.ColorIndex = 35
        Range("x17:K17").Font.ColorIndex = 28
        Range("x18:K18,x33:K33").Font.ColorIndex = 38
        Range("x24:K26,x29:K31").Font.ColorIndex = 36
        Range("x32:K32").Font.ColorIndex = 44
        Range("x36:K36,x38:K38").Font.ColorIndex = 15
End Sub

字符串连接运算符:&

将变量
x
作为字符串传递:

通过传递字符串调用sub:

updateFontColor("K")
小组:

Sub updateFontColor(x As String) 'pass x as string, not range
    Range(x & "8:K9," & x & "14:K14," & x & "37:K37").Font.ColorIndex = 2
    .
    .
    .
End Sub

chr(65)是“A”,所以
chr(64+yourcellvalue+2)
会给你你需要的列字母。对不起,我不知道这有什么帮助,也许我只是不明白,我编辑了我的帖子来澄清我想做什么……如果你从单元格值计算列字母(并传递给Jacob显示的sub),那么你就不需要所有这些
如果。。。否则…
statementsTHANKS!这正是我需要知道的!