VBA不相邻动态列的选择范围

时间:2018-10-27 16:37:38

标签: excel vba excel-vba

我有6个数据组,这些数据组基于工作日是动态的,并且每个月都在变化。全部从R6开始,并且各组之间用空白列分隔。我想将它们全部选择为格式化范围(边框,对齐方式等),但不格式化将它们分开的空白列。我是VBA的新手,感谢您的帮助。

Sub Macro3()

Dim d_hend As Object, d_qend As Object, d_zend As Object, d_aiend As Object, 
d_arend As Object, d_bdend As Object
Dim d_all1 As Range, d_all2 As Range, d_all3 As Range, d_all4 As Range, 
d_all5 As Range, d_all6 As Range

Set d_hend = Range("H6").End(xlDown)
Set d_qend = Range("Q6").End(xlDown)
Set d_zend = Range("Z6").End(xlDown)
Set d_aiend = Range("AI6").End(xlDown)
Set d_arend = Range("AR6").End(xlDown)
Set d_bdend = Range("BD6").End(xlDown)

Set d_all1 = Range("A6", d_hend)
Set d_all2 = Range("J6", d_qend)
Set d_all3 = Range("S6", d_zend)
Set d_all4 = Range("AB6", d_aiend)
Set d_all5 = Range("AK6", d_arend)
Set d_all6 = Range("AT6", d_bdend)
Set d_all = Range("Range("d_all1"), Range("d_all2"), Range("d_all3"), 
    Range("d_all4"), Range("d_all5"), Range("d_all6")")

End Sub

1 个答案:

答案 0 :(得分:1)

您非常,非常亲密:

Sub Macro3()

    Dim d_hend As Range, d_qend As Range, d_zend As Range, d_aiend As Range, d_arend As Range, d_bdend As Range
    Dim d_all1 As Range, d_all2 As Range, d_all3 As Range, d_all4 As Range, d_all5 As Range, d_all6 As Range, d_all As Range

    Set d_hend = Range("H6").End(xlDown)
    Set d_qend = Range("Q6").End(xlDown)
    Set d_zend = Range("Z6").End(xlDown)
    Set d_aiend = Range("AI6").End(xlDown)
    Set d_arend = Range("AR6").End(xlDown)
    Set d_bdend = Range("BD6").End(xlDown)

    Set d_all1 = Range("A6", d_hend)
    Set d_all2 = Range("J6", d_qend)
    Set d_all3 = Range("S6", d_zend)
    Set d_all4 = Range("AB6", d_aiend)
    Set d_all5 = Range("AK6", d_arend)
    Set d_all6 = Range("AT6", d_bdend)
    Set d_all = Union(d_all1, d_all2, d_all3, d_all4, d_all5, d_all6)

    d_all.Interior.ColorIndex = 27

End Sub

enter image description here