Ouside与动态范围接壤

时间:2016-02-18 14:29:25

标签: excel-vba dynamic border vba excel

我已经搜索过我的答案但没有运气。我要做的是移动底部边框我目前在一个页面上有8组数据。范围为B4:E7G4:I7B11:E14G11:I14B18:E40G18:I40G44:E66G44:I66 。我在模板中设置了边框,如果没有值,我有VBA来隐藏单元格。

我期待底部边框中的宏,因为范围有数据。我已经尝试命名范围并使用borderaround但保持原始命名范围。我找不到有用的代码。

2 个答案:

答案 0 :(得分:1)

我写了下面的模块,它正是你所要求的。我刚刚注意到你的答案,但从你的问题来看,你似乎在寻找更具动感的东西,而不仅仅是添加边框的代码。就像一个FYI,将来你可以使用宏记录来获得这样的代码。

Sub DynamicBorders()

Dim arrRange1() As String
Dim arrRange2() As String
Dim i As Integer, x As Integer
Dim strLeft As String
Dim strRight As String

arrRange1 = Split("B4,G4,B11,G11,B18,G18,B44,G44", ",")
arrRange2 = Split("E7,I7,E14,I14,E40,I40,E66,I66", ",")

For i = LBound(arrRange1) To UBound(arrRange1)
    'Determine if the range is 2 or 3 charaters long, then set x = row number
    If Len(arrRange2(i)) = 2 Then
      x = Right(arrRange2(i), 1)
    ElseIf Len(arrRange2(i)) = 3 Then
      x = Right(arrRange2(i), 2)
    End If

    If ActiveSheet.Rows(x).Hidden = True Then
      'Find the first row that is not hidden
      Do Until ActiveSheet.Rows(x).Hidden = False
        x = x - 1
      Loop

      'Get the column letter of the range
      strLeft = Left(arrRange1(i), 1)
      strRight = Left(arrRange2(i), 1)

      'Select the range of cells across the bottom and set the border to black
      ActiveSheet.Range(ActiveSheet.Cells(x, strLeft), ActiveSheet.Cells(x, strRight)).Select
      With Selection.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .ColorIndex = vbBlack
        .TintAndShade = 0
        .Weight = xlThin
      End With

    Else
      'If the last row is not hidden, then clear any previous border that was added
      'Note - you may want to add this as a seperate module to 'reset' the borders
      strLeft = arrRange1(i)
      strRight = arrRange2(i)
      ActiveSheet.Range(strLeft & ":" & strRight).Select
      Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
    End If

  Next i

  ActiveSheet.Range("A1").Select
  End Sub

答案 1 :(得分:0)

我能够找到答案并进行了测试。

With Range("s_" & i)  
    With .Rows(.SpecialCells(xlCellTypeVisible).Rows.Count)   
        With .Borders(xlEdgeBottom)  
            .LineStyle = xlContinuous  
            .Weight = xlMedium  
            .ColorIndex = xlAutomatic  
        End With  
    End With  
End With