Excel VBA程序的用户窗体太大
我使用下面的代码在单独的窗口中显示计算结果,以跟踪实时时间的变化。这只是代码的一小部分,它将在下面的许多行中继续。我有很长的代码,我想让我的UserForm在选项卡式视图多页中显示摘要报告。我只能用以下代码创建一个选项卡。当我尝试添加第二个选项卡时,我得到的过程太大 我的代码:Excel VBA程序的用户窗体太大,excel,vba,Excel,Vba,我使用下面的代码在单独的窗口中显示计算结果,以跟踪实时时间的变化。这只是代码的一小部分,它将在下面的许多行中继续。我有很长的代码,我想让我的UserForm在选项卡式视图多页中显示摘要报告。我只能用以下代码创建一个选项卡。当我尝试添加第二个选项卡时,我得到的过程太大 我的代码: Controls("Label841").Caption = ThisWorkbook.Sheets("Price calculation").Range("A109").Value Controls("Label842
Controls("Label841").Caption = ThisWorkbook.Sheets("Price calculation").Range("A109").Value
Controls("Label842").Caption = ThisWorkbook.Sheets("Price calculation").Range("A110").Value
Controls("Label843").Caption = ThisWorkbook.Sheets("Price calculation").Range("A111").Value
Controls("Label844").Caption = ThisWorkbook.Sheets("Price calculation").Range("A112").Value
Controls("Label845").Caption = ThisWorkbook.Sheets("Price calculation").Range("A113").Value
Controls("Label846").Caption = ThisWorkbook.Sheets("Price calculation").Range("A114").Value
Controls("Label847").Caption = ThisWorkbook.Sheets("Price calculation").Range("A115").Value
Controls("Label848").Caption = ThisWorkbook.Sheets("Price calculation").Range("A116").Value
Controls("Label849").Caption = ThisWorkbook.Sheets("Price calculation").Range("A117").Value
Controls("Label850").Caption = ThisWorkbook.Sheets("Price calculation").Range("A118").Value
Controls("Label851").Caption = ThisWorkbook.Sheets("Price calculation").Range("A119").Value
Controls("Label852").Caption = ThisWorkbook.Sheets("Price calculation").Range("A120").Value
Controls("Label853").Caption = ThisWorkbook.Sheets("Price calculation").Range("A121").Value
Controls("Label854").Caption = ThisWorkbook.Sheets("Price calculation").Range("A122").Value
Controls("Label855").Caption = ThisWorkbook.Sheets("Price calculation").Range("A123").Value
Controls("Label856").Caption = ThisWorkbook.Sheets("Price calculation").Range("A124").Value
Controls("Label875").Caption = ThisWorkbook.Sheets("Price calculation").Range("D109").Value
Controls("Label876").Caption = ThisWorkbook.Sheets("Price calculation").Range("D110").Value
Controls("Label877").Caption = ThisWorkbook.Sheets("Price calculation").Range("D111").Value
Controls("Label878").Caption = ThisWorkbook.Sheets("Price calculation").Range("D112").Value
Controls("Label879").Caption = ThisWorkbook.Sheets("Price calculation").Range("D113").Value
Controls("Label880").Caption = ThisWorkbook.Sheets("Price calculation").Range("D114").Value
Controls("Label881").Caption = ThisWorkbook.Sheets("Price calculation").Range("D115").Value
Controls("Label882").Caption = ThisWorkbook.Sheets("Price calculation").Range("D116").Value
Controls("Label883").Caption = ThisWorkbook.Sheets("Price calculation").Range("D117").Value
Controls("Label884").Caption = ThisWorkbook.Sheets("Price calculation").Range("D118").Value
Controls("Label885").Caption = ThisWorkbook.Sheets("Price calculation").Range("D119").Value
Controls("Label886").Caption = ThisWorkbook.Sheets("Price calculation").Range("D120").Value
Controls("Label887").Caption = ThisWorkbook.Sheets("Price calculation").Range("D121").Value
Controls("Label888").Caption = ThisWorkbook.Sheets("Price calculation").Range("D122").Value
Controls("Label889").Caption = ThisWorkbook.Sheets("Price calculation").Range("D123").Value
Controls("Label890").Caption = ThisWorkbook.Sheets("Price calculation").Range("D124").Value
Controls("Label891").Caption = ThisWorkbook.Sheets("Price calculation").Range("D125").Value
Controls("Label911").Caption = ThisWorkbook.Sheets("Price calculation").Range("E109").Value
Controls("Label912").Caption = ThisWorkbook.Sheets("Price calculation").Range("E110").Value
Controls("Label913").Caption = ThisWorkbook.Sheets("Price calculation").Range("E111").Value
Controls("Label914").Caption = ThisWorkbook.Sheets("Price calculation").Range("E112").Value
Controls("Label915").Caption = ThisWorkbook.Sheets("Price calculation").Range("E113").Value
Controls("Label916").Caption = ThisWorkbook.Sheets("Price calculation").Range("E114").Value
Controls("Label917").Caption = ThisWorkbook.Sheets("Price calculation").Range("E115").Value
Controls("Label918").Caption = ThisWorkbook.Sheets("Price calculation").Range("E116").Value
Controls("Label919").Caption = ThisWorkbook.Sheets("Price calculation").Range("E117").Value
Controls("Label920").Caption = ThisWorkbook.Sheets("Price calculation").Range("E118").Value
Controls("Label921").Caption = ThisWorkbook.Sheets("Price calculation").Range("E119").Value
Controls("Label922").Caption = ThisWorkbook.Sheets("Price calculation").Range("E120").Value
Controls("Label923").Caption = ThisWorkbook.Sheets("Price calculation").Range("E121").Value
Controls("Label924").Caption = ThisWorkbook.Sheets("Price calculation").Range("E122").Value
Controls("Label925").Caption = ThisWorkbook.Sheets("Price calculation").Range("E123").Value
Controls("Label926").Caption = ThisWorkbook.Sheets("Price calculation").Range("E124").Value
Controls("Label927").Caption = ThisWorkbook.Sheets("Price calculation").Range("E125").Value
您可以通过执行以下操作使您的过程变得更小更快:
With ThisWorkbook.Sheets("Price calculation")
Controls("Label841").Caption = .Range("A109").Value
Controls("Label842").Caption = .Range("A110").Value
Controls("Label843").Caption = .Range("A111").Value
....
End With
除此之外,你还可以用一个for。。。下一个声明。例如,代码的前16行可以替换为以下例程:
With ThisWorkbook.Sheets("Price calculation")
For x = 841 to 856
Controls("Label" & x).Caption = .Range("A" & x - 732).Value
Next x
End With
或:
这种做法大大减少了代码大小,使过程更快。您可以通过执行以下操作使过程更小更快:
With ThisWorkbook.Sheets("Price calculation")
Controls("Label841").Caption = .Range("A109").Value
Controls("Label842").Caption = .Range("A110").Value
Controls("Label843").Caption = .Range("A111").Value
....
End With
除此之外,你还可以用一个for。。。下一个声明。例如,代码的前16行可以替换为以下例程:
With ThisWorkbook.Sheets("Price calculation")
For x = 841 to 856
Controls("Label" & x).Caption = .Range("A" & x - 732).Value
Next x
End With
或:
这种做法大大减少了代码大小,并使过程更快。从空白表单开始。添加具有单个页面的多页控件。将此代码放入UserForm Initialize事件中
Private Sub UserForm_Initialize()
Dim x As Integer: Dim y As Integer: Dim counter As Integer
Dim SourceRange As Range
Set SourceRange = ThisWorkbook.Sheets("Price calculation").Range("A109:A124")
Dim p As Control
Dim lab As Control
Const rowoffset = 20 'height of each row
Const startpoint = 60 'position of top row in tab
Const columnoffset = 3 'where next columns for captions are on spreadhseet
y = 12 'indent from left of form
Dim r As Range
For counter = 0 To 2
Set p = Me.MyMultiPage.Pages(counter)
x = startpoint
For Each r In SourceRange
Set lab = p.Controls.Add("Forms.Label.1")
lab.Left = y
lab.Top = x
lab.Width = 100
lab.Caption = r.Text
x = x + rowoffset
Next r
Set SourceRange = SourceRange.Offset(0, columnoffset)
If counter = Me.MyMultiPage.Pages.Count - 1 Then
Me.MyMultiPage.Pages.Add "Page" & counter + 1, "Page" & counter + 1, counter + 1
End If
Next counter
End Sub
玩这些常量直到它看起来很漂亮/适合这个表单从空白表单开始。添加具有单个页面的多页控件。将此代码放入UserForm Initialize事件中
Private Sub UserForm_Initialize()
Dim x As Integer: Dim y As Integer: Dim counter As Integer
Dim SourceRange As Range
Set SourceRange = ThisWorkbook.Sheets("Price calculation").Range("A109:A124")
Dim p As Control
Dim lab As Control
Const rowoffset = 20 'height of each row
Const startpoint = 60 'position of top row in tab
Const columnoffset = 3 'where next columns for captions are on spreadhseet
y = 12 'indent from left of form
Dim r As Range
For counter = 0 To 2
Set p = Me.MyMultiPage.Pages(counter)
x = startpoint
For Each r In SourceRange
Set lab = p.Controls.Add("Forms.Label.1")
lab.Left = y
lab.Top = x
lab.Width = 100
lab.Caption = r.Text
x = x + rowoffset
Next r
Set SourceRange = SourceRange.Offset(0, columnoffset)
If counter = Me.MyMultiPage.Pages.Count - 1 Then
Me.MyMultiPage.Pages.Add "Page" & counter + 1, "Page" & counter + 1, counter + 1
End If
Next counter
End Sub
玩这些常量,直到它看起来很漂亮/符合形式试试看
Dim vDB As Variant, a As Variant, c As Variant
Dim Ws As Worksheet
Dim i As Integer, j As Integer, n As Integer
Set Ws = ThisWorkbook.Sheets("Price calculation")
a = Array("a", "d", "e") 'column characters
c = Array(841, 875, 911) 'label numbers
For i = LBound(a) To UBound(a)
vDB = Ws.Range(a(i) & 109).Resize(16)
n = 0
For j = c(i) To c(i) + 15
n = n + 1
Me.Controls("Label" & j).Caption = vDB(n, 1)
Next j
Next i
如果数据大小不同,将给出一个变量k
Dim vDB As Variant, a As Variant, c As Variant
Dim Ws As Worksheet
Dim i As Integer, j As Integer, n As Integer
Dim k As Integer
Set Ws = ThisWorkbook.Sheets("Price calculation")
a = Array("a", "d", "e") 'column characters
c = Array(841, 875, 911) 'label numbers
For i = LBound(a) To UBound(a)
If i = 0 Then
k = 16
Else
k = 17
End If
vDB = Ws.Range(a(i) & 109).Resize(k)
n = 0
For j = c(i) To c(i) + k - 1
n = n + 1
Me.Controls("Label" & j).Caption = vDB(n, 1)
Next j
Next i
试一试
如果数据大小不同,将给出一个变量k
Dim vDB As Variant, a As Variant, c As Variant
Dim Ws As Worksheet
Dim i As Integer, j As Integer, n As Integer
Dim k As Integer
Set Ws = ThisWorkbook.Sheets("Price calculation")
a = Array("a", "d", "e") 'column characters
c = Array(841, 875, 911) 'label numbers
For i = LBound(a) To UBound(a)
If i = 0 Then
k = 16
Else
k = 17
End If
vDB = Ws.Range(a(i) & 109).Resize(k)
n = 0
For j = c(i) To c(i) + k - 1
n = n + 1
Me.Controls("Label" & j).Caption = vDB(n, 1)
Next j
Next i
使用循环?您的标签号和范围似乎是按顺序排列的。对于我的情况,有没有最有效的循环示例?+您可以重复使用标签,而不是在多页的不同选项卡上使用大量标签。我想到了@pspl的答案。请参阅一些减少KB限制的提示,使用循环?您的标签号和范围似乎是按顺序排列的。对于我的情况,有没有最有效的循环示例?+您可以重复使用标签,而不是在多页的不同选项卡上使用许多标签。我想到了@pspl的答案。请参阅一些减少KB限制的提示谢谢您的回答!能否为Label841-Label856和Label875-Label891等两个模块添加示例?非常感谢。谢谢你的回答!能否为Label841-Label856和Label875-Label891等两个模块添加示例?非常感谢。非常感谢。但是请注意,在第一列A中有16项,其余17项都添加了答案。@user7202022。您的代码看起来是最有趣的实现。由于某种原因,我不能让它工作。另外,在您上次编辑的代码标签外有一个i。我已经在我的宏中添加了它,但由于某些原因它仍然不起作用。一直都在出错time@user7202022,我打错了a&I&109。这是ai&109,谢谢!但是请注意,在第一列A中有16项,其余17项都添加了答案。@user7202022。您的代码看起来是最有趣的实现。由于某种原因,我不能让它工作。另外,在您上次编辑的代码标签外有一个i。我已经在我的宏中添加了它,但由于某些原因它仍然不起作用。一直都在出错time@user7202022,我打错了a&I&109。这应该是ai&109。