Union 」标签的归档

Union

Union使用之升呢版

  1. Union(R1,R2,R3) 假如其中一个为nothing的话,会报错.
  2.         Dim R1 As Range
            Dim R2 As Range
            Dim R3 As Range
            Dim RR As Range
            Set R1 = Range("A1")
            Set R2 = Range("B1")
            Set RR = Application.Union(R1, R2, R3)' Error 5 - Invalid Parameter
  3. Union2(R1,R2,R3) 假如其中一个为nothing的话,不会报错.
  4. Union 连接重复的range的时候, Count会出错
  5.     Dim RR As Range
        Dim R As Range
        Dim N As Long
        Set RR = Application.Union(Range("A1:C3"), Range("B3:D5"))
        For Each R In RR
            N = N + 1
        Next R
        Debug.Print N ' 18 cells. incorrect
  6. ProperUnion 可以避免3中出现的错误
article clipper remember Union
 

Set Border

通过Union将个Range连接在一起,一次过添加border, 减少与Worsheet之间的交换.
Function SetBoarder(rng)

    rng.Borders(xlEdgeLeft).Weight = xlMedium
    rng.Borders(xlEdgeTop).Weight = xlMedium
    rng.Borders(xlEdgeBottom).Weight = xlMedium
    rng.Borders(xlEdgeRight).Weight = xlMedium

End Function

Sub FormatBoarder()
Dim rng As Range
Dim iRow As Integer
Dim iCol As Integer
Dim iStartRow As Integer, iEndRow As Integer, iStartCol As Integer, iEndCol As Integer

iStartRow = 3
iEndRow = 20
iStartCol = 4
iEndCol = 10

For iRow = iStartRow To iEndRow
    If Rows(iRow).RowHeight = 37 Then
        For iCol = iStartCol To iEndCol Step 2
            If Columns(iCol).ColumnWidth = 8.38 Then
                Set rng = Union(Cells(iRow - 1, iCol), Cells(iRow, iCol), Cells(iRow + 1, iCol), _
                            Cells(iRow - 1, iCol).Offset(0, 1), Cells(iRow, iCol).Offset(0, 1), Cells(iRow + 1, iCol).Offset(0, 1), _
                            Cells(iRow - 1, iCol).Offset(0, -1), Cells(iRow, iCol).Offset(0, -1), Cells(iRow + 1, iCol).Offset(0, -1))
                Call SetBoarder(rng)
            End If
        Next
    End If

Next iRow
End Sub
article clipper remember Set Border