更改列宽和行高

时间:2017-04-18 03:57:37

标签: excel vba excel-vba

我有一个包含100-150个工作表的工作簿我创建了一个代码,该代码将更改除了具有以下名称的工作表之外的所有工作表的行高和列宽:"封面" " Trans_Letter","缩写"和表格名称以_Index结尾。

我的代码工作正常并且更改了行高和列宽,但是它也改变了以_Index结尾的工作表名称的列宽和行高。

请建议我应该如何修改代码,以便在循环时它应该跳过以_Index结尾的工作表名称。

我想以下行需要修改: -

If ShtNames(Z) <> "Trans_Letter" And ShtNames(Z) <> "Cover" And ShtNames(Z) <> "Abbreviations" And InStr("_Index", ShtNames(Z)) = 0 Then

请在下面找到代码: -

Sub rowcolallsheetbtransletter()

    Dim exworkb As Workbook
    Dim xlwksht As Worksheet
    Dim lastrow1 As Long
    Dim lastcolumn1 As Long
    Dim firstrowDB As Long
    Dim Z As Integer
    Dim ShtNames() As String

    ReDim ShtNames(1 To ActiveWorkbook.Sheets.Count)

    For Z = 1 To Sheets.Count

        ShtNames(Z) = Sheets(Z).Name

        If ShtNames(Z) <> "Trans_Letter" And ShtNames(Z) <> "Cover" And ShtNames(Z) <> "Abbreviations" And InStr("_Index", ShtNames(Z)) = 0 Then

            Sheets(Z).Select

            lastrow1 = Sheets(Z).Cells(Rows.Count, "A").End(xlUp).Row
            lastcolumn1 = Sheets(Z).Cells(1, Columns.Count).End(xlToLeft).Column

            ActiveWorkbook.Sheets(Z).Range(Sheets(Z).Cells(1, 2), Sheets(Z).Cells(lastrow1, lastcolumn1)).Select

            Selection.Cells.RowHeight = 67
            Selection.Cells.ColumnWidth = 30

        End If
    Next Z
End Sub

1 个答案:

答案 0 :(得分:1)

而不是检查字符串&#34; _Index&#34;中是否存在工作表名称,而不是检查是否&#34; _Index&#34;存在于工作表名称中:

If ShtNames(Z) <> "Trans_Letter" And _
   ShtNames(Z) <> "Cover" And _
   ShtNames(Z) <> "Abbreviations" And _
   InStr(ShtNames(Z), "_Index") = 0 Then