添加字体颜色和不填充到子例程

希望快速解决这个问题。 我有一个子例程,我正在使用它来修改工作簿中的所有工作表的列大小,这个工作簿非常好,但是我想添加到这个子工作区,以便它将改变我范围内所有单元格的黑色字体,并删除从他们的任何填充颜色也。 我已经写了下面的代码,但它似乎没有执行我想要的方式。 任何想法,我怎样才能纠正这将是太棒了!

Sub forEachWs() Dim ws As Worksheet 'Opens new workbook for formatting Workbooks.Open "C:\Users\XNEID\Desktop\Test MPAN Destination Folder\Shell_MPANs_Test1.xlsx" For Each ws In ActiveWorkbook.Worksheets Call resizingColumns(ws) Next End Sub Sub resizingColumns(ws As Worksheet) With ws .Range("A1:BB1").EntireColumn.AutoFit Range("A2:BB2", Range("A2:BB2").End(xlDown)).Select Selection.Font.Color = vbBlack Range("A2:BB2", Range("A2:BB2").End(xlDown)).Select Selection.Interior.ColorIndex = xlNone End With End Sub 

我会build议定义最后一行(从底部开始),并省略了你的With语句所需的点

 Sub resizingColumns(ws As Worksheet) Dim n As Long With ws n = .Range("A" & .Rows.Count).End(xlUp).Row .Range("A1:BB1").EntireColumn.AutoFit .Range("A2:BB" & n).Font.Color = vbBlack .Range("A2:BB" & n).Interior.ColorIndex = xlNone End With End Sub 
  1. 你需要完全限定细胞的对象
  2. 使用variables。 看看我如何使用它来find最后一行
  3. 避免使用。select

这是你正在尝试?

 With ws .Columns("A:BB").EntireColumn.AutoFit lrow = .Range("A" & .Rows.Count).End(xlUp).Row With .Range("A2:BB" & lrow) .Font.Color = vbBlack .Interior.ColorIndex = xlNone End With End Withh 

避免使用SelectActiveWorkbook 。 相反,只需直接引用范围即可。 尝试这个:

 Sub forEachWs() Dim ws As Worksheet Dim wb As Workbook 'Opens new workbook for formatting Set wb = Workbooks.Open "C:\Users\XNEID\Desktop\Test MPAN Destination Folder\Shell_MPANs_Test1.xlsx" For Each ws In wb.Worksheets Call resizingColumns(ws) Next End Sub Sub resizingColumns(ws As Worksheet) With ws .Range("A1:BB1").EntireColumn.AutoFit .Range("A2:BB" & .Cells(.Rows.Count, 1).End(xlUp).Row).Font.Color = vbBlack .Range("A2:BB" & .Cells(.Rows.Count, 1).End(xlUp).Row).Interior.ColorIndex = xlNone End With End Sub