Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 如果保持为空,则使用userform文本框_Excel_Vba - Fatal编程技术网

Excel 如果保持为空,则使用userform文本框

Excel 如果保持为空,则使用userform文本框,excel,vba,Excel,Vba,我创建了一个useform,它可以帮助我根据textbox1和textbox2中输入的文本,从B列或C列(基于Frame2中的选择选项)更改活动工作表或所有工作表(基于Frame3中的选择选项)的列宽和行高。 我已附上用户表格供您参考。 这段代码运行良好,但是如果我只需要更改列宽而不需要更改行高,那么当我输入列宽并将行高保留为空时,下面提到的行会出现错误: Selection.Cells.RowHeight=Me.TextBox2.Value 请让我知道如何修改我的代码,即使任何文本框值留空(

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

这段代码运行良好,但是如果我只需要更改列宽而不需要更改行高,那么当我输入列宽并将行高保留为空时,下面提到的行会出现错误: Selection.Cells.RowHeight=Me.TextBox2.Value

请让我知道如何修改我的代码,即使任何文本框值留空(即,如果我只想更改列宽并保持行高不变),它也不会给我错误信息

用户表单代码:

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


在if函数中添加selection.cells.rowheight,检查文本框的值,如果不是vbnullstring,则更改行高。如果您愿意,您也可以对列宽执行类似的操作,而@SivaprasathV是正确的,这将是解决此问题的首选方法。我认为这不是您想要的。看看你最近的发帖记录,看看你是如何忽视了从这个网站上这么多天才程序员那里得到的所有伟大建议的,我建议你做这里大多数人都不赞成的事情,然后简单地在错误恢复时执行这一行
。基本上,这告诉VBA:如果一行代码由于导致错误而无法执行,那么只需跳过该行代码,然后继续下一行并从那里开始。所有错误都将被忽略。我添加了这一行,它有帮助:If Me.TextBox1.Value“”然后Selection.Cells.ColumnWidth=Me.TextBox1.Value结束If Me.TextBox2.Value“”然后选择Selection.Cells.RowHeight=Me.TextBox2.Value结束If@Ralph我真的很感谢你对我上一篇和现在这篇文章的建议,它对我帮助很大
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