select一个单元格区域,删除空单元格,然后在剩余单元格周围添加边框

我想要使​​用一个macros,它将查看一系列单元格,删除空行,然后在实际上包含内容的其余单元格周围添加边框。 这是我有两个macros:一个是删除空单元格,另一个是添加边框。 正如我所提到的,问题是,我不知道如何告诉Excel仅在删除macros完成后留下的单元格周围添加边框。 我将不胜感激任何帮助。

**Sub Remove()** ' ' Remove Macro ' ' Range("B80:B95").Select Selection.SpecialCells(xlCellTypeBlanks).Select Selection.EntireRow.Delete ActiveWindow.SmallScroll Down:=-12 Range("B61:B77").Select Selection.SpecialCells(xlCellTypeBlanks).Select Selection.EntireRow.Delete ActiveWindow.SmallScroll Down:=-21 Range("B39:B58").Select Selection.SpecialCells(xlCellTypeBlanks).Select Selection.EntireRow.Delete ActiveWindow.SmallScroll Down:=-27 Range("B10:B28").Select Selection.SpecialCells(xlCellTypeBlanks).Select Selection.EntireRow.Delete ActiveWindow.SmallScroll Down:=-6 End Sub 

 **Sub Border()** ' ' Border Macro ' ' Range("B7:K19").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 ActiveWindow.SmallScroll Down:=18 Range("B21:K74").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 ActiveWindow.SmallScroll Down:=54 Range("O76").Select ActiveWindow.SmallScroll Down:=-81 End Sub 

我假设你的B到K列总是保持不变。 你基本上只是想find总“使用”行…

 Dim cols As Integer, LastRow As Long, TestRow As Long LastRow = 0 For cols = 2 to 11 TestRow = Cells(Rows.Count, cols).End(xlUp).Row If TestRow > LastRow Then LastRow = TestRow Next cols Range("B7:K" & LastRow).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 ActiveWindow.SmallScroll Down:=18 Range("B21:K74").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 ActiveWindow.SmallScroll Down:=54 Range("O76").Select ActiveWindow.SmallScroll Down:=-81 

我真的讨厌使用。select,但我不重写所有其他的代码。