macros(VBA)在Excel中添加边框并合并单元格,如果单元格不为空

我logging了下面的macros:

Sub Macro1() Range("E66:F68").Select Selection.Borders(xlDiagonalDown).LineStyle = xlNone Selection.Borders(xlDiagonalUp).LineStyle = xlNone With Selection.Borders(xlEdgeLeft) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlThin End With With Selection.Borders(xlEdgeTop) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlThin End With With Selection.Borders(xlEdgeBottom) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlThin End With With Selection.Borders(xlEdgeRight) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlThin End With Selection.Borders(xlInsideVertical).LineStyle = xlNone Selection.Borders(xlInsideHorizontal).LineStyle = xlNone Range("D66:D68,C66:C68,B66:B68,A66:A68").Select Range("A66").Activate With Selection .HorizontalAlignment = xlCenter .VerticalAlignment = xlBottom .WrapText = False .Orientation = 0 .AddIndent = False .IndentLevel = 0 .ShrinkToFit = False .ReadingOrder = xlContext .MergeCells = False End With Selection.Merge With Selection .HorizontalAlignment = xlCenter .VerticalAlignment = xlCenter .WrapText = False .Orientation = 0 .AddIndent = False .IndentLevel = 0 .ShrinkToFit = False .ReadingOrder = xlContext End With Selection.Borders(xlDiagonalDown).LineStyle = xlNone Selection.Borders(xlDiagonalUp).LineStyle = xlNone With Selection.Borders(xlEdgeLeft) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlThin End With With Selection.Borders(xlEdgeTop) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlThin End With With Selection.Borders(xlEdgeBottom) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlThin End With With Selection.Borders(xlEdgeRight) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlThin End With Selection.Borders(xlInsideVertical).LineStyle = xlNone Selection.Borders(xlInsideHorizontal).LineStyle = xlNone Range("G73").Select End Sub 

现在,这是从E66开始的范围logging的,它基本上为所选单元格添加边界,并合并相邻列中的单元格行。 我想要做的就是添加一个条件,查看E列,并在第一个没有边框的非空单元格上启动macros,并在最后一个非空单元格上结束它。 在我logging的macros中,第一个非空的非空单元是E66(意思是E1:E65范围内的单元在至less一侧有所有边界),最后一个非空单元是E68(在第二行是E66:F68,因为我使用E66到F68单元格的矩形的外部边界,但只需要validation列E的条件)。

换句话说,我需要一些从E1到E x的循环,当它find一个既非空也不无边界的单元格时,它将该单元格号存储为起始单元格(比如E y )。 然后,当它find一个空单元格(如E z )时,循环停止,并且E z之前的单元格(所以E z-1 )被存储为最后一个单元格。 然后我logging的macros应该在E y :F z-1范围内运行。

我怎样才能做到这一点? 谢谢。

这可能工作。 您可以调整filter和格式以适合您的需求。 但是,要注意macros观录制。

 Sub FindAreas() TopRange = 1 LastRow = Cells.Find(What:="*", After:=[A1], SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row For A = 1 To LastRow If Range("A" & A).Value <> "" _ And Range("A" & A).Borders(xlEdgeLeft).LineStyle = xlNone _ And Range("A" & A).Borders(xlEdgeRight).LineStyle = xlNone _ And Range("A" & A).Borders(xlEdgeTop).LineStyle = xlNone _ And Range("A" & A).Borders(xlEdgeBottom).LineStyle = xlNone _ Then Contiguous = True Else Contiguous = False If A = LastRow Then Contiguous = False A = A + 1 End If Select Case Contiguous Case False Call ApplyFormattingtoArea("A" & TopRange & ":A" & A - 1) TopRange = A + 1 A = A + 1 End Select Next A End Sub Sub ApplyFormattingtoArea(AppliedArea) Application.DisplayAlerts = False Range(AppliedArea).Merge Range(AppliedArea).Borders(xlInsideVertical).LineStyle = xlNone Range(AppliedArea).Borders(xlInsideHorizontal).LineStyle = xlNone With Range(AppliedArea) .HorizontalAlignment = xlCenter .VerticalAlignment = xlCenter .WrapText = False .Orientation = 0 .AddIndent = False .IndentLevel = 0 .ShrinkToFit = False .ReadingOrder = xlContext End With Range(AppliedArea).Borders(xlDiagonalDown).LineStyle = xlNone Range(AppliedArea).Borders(xlDiagonalUp).LineStyle = xlNone With Range(AppliedArea).Borders(xlEdgeLeft) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlThin End With With Range(AppliedArea).Borders(xlEdgeTop) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlThin End With With Range(AppliedArea).Borders(xlEdgeBottom) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlThin End With With Range(AppliedArea).Borders(xlEdgeRight) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlThin End With Application.DisplayAlerts = True End Sub