vba用户表单,如果框架中的任何checkbox为真,则不应将macros应用于checkbox中提到的工作表名称

我已经创build了一个用户窗体(用来更改活动工作表或所有工作表的列和行宽),它有三个框架。 在第一帧我给了两个选项框。 首先选项框: – 要更改列B向前和其他选项框中的行和列宽度,以更改列c以后的行列宽度。 用户将select其中的任何一个,然后移动到第二个框架:它有两个选项,一个用于在活动工作表和第二个选项框中进行更改,以在所有工作表中进行更改。 因此,如果第一个表格中的用户将select第一个选项(从B开始更改行和列宽度,而在第二个框架中将select活动工作表,则列和行宽将从活动工作表中的列B开始改变,依此类推。 ..

现在我想要创build第三个fram,其中有3个checkbox,其名称为3张(Sheet1,Sheet2和Sheet3)。我希望当用户在第一帧和第二帧中select了他的选项时,如果第三帧中的用户selectcheckbox或所有checkbox,那么这些更改不应该应用于他select的3个checkbox中的任何一个checkbox中提到的表单名称。

我已经成功地执行了第一帧和第二帧,但是正在努力为第三帧创build一个代码,该代码将有3个checkbox(其中包含3个表单的名称),这个checkbox将被排除以进行任何行和列宽度的改变。

请在下面find我的代码在模块中:

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 Selection.Cells.RowHeight = 9.14 Selection.Cells.ColumnWidth = 7.14 End With End Sub Sub rowcolallsheetb() 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 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 = 9.14 Selection.Cells.ColumnWidth = 7.14 Next Z 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 Selection.Cells.RowHeight = 9.14 Selection.Cells.ColumnWidth = 7.14 End With End Sub Sub rowcolallsheetc() 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 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, 3), Sheets(Z).Cells(lastrow1, lastcolumn1)).Select Selection.Cells.RowHeight = 9.14 Selection.Cells.ColumnWidth = 7.14 Next Z End Sub 

用户表单代码:

 Private Sub CommandButton1_Click() If Me.OptionButton5.Value = True Then If Me.OptionButton7.Value = True Then Call rowcolactivesheetb ElseIf Me.OptionButton8.Value = True Then rowcolallsheetb End If End If If Me.OptionButton6.Value = True Then If Me.OptionButton7.Value = True Then Call rowcolactivesheetc ElseIf Me.OptionButton8.Value = True Then rowcolallsheetc End If End If End Sub 

首先,我不认为我会使用OptionButtons 。 从你的描述看来,好像ListBoxes会更适合你。

其次,将值传递到实际设置列和行的单个例程可能会更优雅,而不是创build单独但几乎相同的例程。

我坚持你的OptionButton结构,并假设你提到的三个额外的OptionButtons将被称为OptionButton9,10和11。

所以模块代码可以是这样的:

 Public Sub SizeRowsAndCols(fromB As Boolean, _ fromC As Boolean, _ targetActive As Boolean, _ targetAll As Boolean, _ excSheets As Variant) Dim fromCol As Long Dim sh As Worksheet Dim nameString As Variant 'Define the column value Select Case True Case fromB: fromCol = 2 Case fromC: fromCol = 3 Case Else: MsgBox "Column selection error" End Select 'Run routine on single or multiple sheets Select Case True Case targetActive SetValuesOnSheet ThisWorkbook.ActiveSheet, fromCol Case targetAll For Each sh In ThisWorkbook.Worksheets If IsEmpty(excSheets) Then 'If no sheets are to be excluded SetValuesOnSheet sh, fromCol Else 'Exclude the sheets in the list For Each nameString In excSheets If sh.Name <> nameString Then SetValuesOnSheet sh, fromCol End If Next End If Next Case Else MsgBox "Sheet selection error" End Select End Sub Private Sub SetValuesOnSheet(sh As Worksheet, fromCol As Long) Dim lastR As Long, lastC As Long Dim rng As Range With sh lastR = .Cells(.Rows.Count, "A").End(xlUp).Row lastC = .Cells(1, .Columns.Count).End(xlToLeft).Column Set rng = .Range(.Cells(1, fromCol), .Cells(lastR, lastC)) rng.RowHeight = 9.14 rng.ColumnWidth = 7.14 End With End Sub 

UserForm代码可能是:

 Private Sub CommandButton1_Click() Dim c As Long Dim sheetNames As String Dim list As Variant 'Build the list of excluded sheets If OptionButton9.Value Then sheetNames = "Sheet1" If OptionButton10.Value Then sheetNames = IIf(sheetNames <> "", "|", "") & "Sheet2" If OptionButton11.Value Then sheetNames = IIf(sheetNames <> "", "|", "") & "Sheet3" list = IIf(sheetNames <> "", Split(sheetNames, "|"), Empty) 'Call the generic routine SizeRowsAndCols OptionButton5.Value, _ OptionButton6.Value, _ OptionButton7.Value, _ OptionButton8.Value, _ list End Sub