userform文本框如果保持空白则起作用

时间:2017-04-18 05:40:31

标签: excel-vba vba excel

我创建了一个useform,它帮助我从B列或C列更改活动工作表或所有工作表的列宽和行高(基于Frame3中的选择选项)(基于Frame2中的选择选项)基于textbox1和textbox2中输入的文本。 我已经使用了userform供您参考。 Userform

此代码工作正常,但是如果我只需要更改列宽而不是行高,那么当我键入列宽并将行高保持为空时,它会在下面提到的行上给出错误:  Selection.Cells.RowHeight = Me.TextBox2.Value

请告诉我如何修改我的代码,即使任何文本框值留空(即如果我只想更改列宽并按原样保持行高)那么它不应该给我错误。

Userform代码:

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

Private Sub CommandButton1_Click()

If Me.OptionButton3.Value = True Then
    If Me.OptionButton1.Value = True Then
        Call rowcolactivesheetb
        Selection.Cells.RowHeight = Me.TextBox2.Value
        Selection.Cells.ColumnWidth = Me.TextBox1.Value
    ElseIf Me.OptionButton2.Value = True Then
        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(ShtNames(Z), "_Index") = 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 = Me.TextBox2.Value
                Selection.Cells.ColumnWidth = Me.TextBox1.Value
            End If
        Next Z
    End If
End If

If Me.OptionButton4.Value = True Then
    If Me.OptionButton1.Value = True Then
        Call rowcolactivesheetc
        Selection.Cells.RowHeight = Me.TextBox2.Value
        Selection.Cells.ColumnWidth = Me.TextBox1.Value
    ElseIf Me.OptionButton2.Value = True Then
        ReDim ShtNames(1 To ActiveWorkbook.Sheets.Count)
        For Z = 1 To Sheets.Count
            ShtNames(Z) = Sheets(Z).Name
            Sheets(Z).Select
            If ShtNames(Z) <> "Trans_Letter" And ShtNames(Z) <> "Cover" And ShtNames(Z) <> "Abbreviations" And InStr(ShtNames(Z), "_Index") = 0 Then
                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, 3), Sheets(Z).Cells(lastrow1, lastcolumn1)).Select
                Selection.Cells.RowHeight = Me.TextBox2.Value
                Selection.Cells.ColumnWidth = Me.TextBox1.Value
            End If
        Next Z
    End If
End If

End Sub

Private Sub CommandButton2_Click()

Unload Me

End Sub

模块代码:

Sub rowcolactivesheetb()

Dim exworkb As Workbook
Dim xlwksht As Worksheet
Dim lastrow1 As Long
Dim lastcolumn1 As Long
Dim firstrowDB As Long

With ActiveSheet
    lastrow1 = .Cells(Rows.Count, "A").End(xlUp).Row
    lastcolumn1 = .Cells(1, Columns.Count).End(xlToLeft).Column
    .Range(.Cells(1, 2), .Cells(lastrow1, lastcolumn1)).Select
End With

End Sub
Sub rowcolactivesheetc()

Dim exworkb As Workbook
Dim xlwksht As Worksheet
Dim lastrow1 As Long
Dim lastcolumn1 As Long
Dim firstrowDB As Long

With ActiveSheet
    lastrow1 = .Cells(Rows.Count, "A").End(xlUp).Row
    lastcolumn1 = .Cells(1, Columns.Count).End(xlToLeft).Column
    .Range(.Cells(1, 3), .Cells(lastrow1, lastcolumn1)).Select
End With

End Sub

0 个答案:

没有答案