我正在为工作创建项目跟踪器,需要帮助缩短代码。每个制造商都有自己的纸张。我有一个Userform,项目经理输入数据。每个制造表都是相同的,与用户表单链接的偏移也是相同的。
请参阅下面的代码。我是编码的新手,可以使用一些帮助。
'A&R 5X9N
If Me.CB7.Value = "A&R" And Me.CB23.Value = "5x9N" Then
rowcount = Worksheets("A&R").Range("c3").CurrentRegion.Rows.Count
With Worksheets("A&R").Range("c1")
'PROJECT
.Offset(rowcount, 0) = Me.CB1.Value
.Offset(rowcount, 1) = Me.TB2.Value
.Offset(rowcount, 2) = Me.TB3.Value
.Offset(rowcount, 3) = Me.CB4.Value
.Offset(rowcount, 4) = Me.CB7.Value
.Offset(rowcount, 5) = Me.TB1.Value
'FINISH DATES
.Offset(rowcount, 7) = Me.TB23.Value
.Offset(rowcount, 8) = Me.TB24.Value
.Offset(rowcount, 9) = Me.TB25.Value
.Offset(rowcount, 10) = Me.TB26.Value
.Offset(rowcount, 11) = Me.TB27.Value
.Offset(rowcount, 12) = Me.TB28.Value
.Offset(rowcount, 13) = Me.TB29.Value
'PRODUCTION
.Offset(rowcount, 14) = Me.TB8.Value
.Offset(rowcount, 15) = Me.TB9.Value
.Offset(rowcount, 16) = Me.TB10.Value
.Offset(rowcount, 17) = Me.TB11.Value
.Offset(rowcount, 18) = Me.TB12.Value
.Offset(rowcount, 19) = Me.TB13.Value
.Offset(rowcount, 20) = Me.TB14.Value
'ASSETS
.Offset(rowcount, 21) = Me.TB16.Value
.Offset(rowcount, 22) = Me.TB17.Value
.Offset(rowcount, 23) = Me.TB18.Value
.Offset(rowcount, 24) = Me.TB19.Value
.Offset(rowcount, 25) = Me.TB20.Value
.Offset(rowcount, 26) = Me.TB21.Value
.Offset(rowcount, 27) = Me.TB22.Value
.Offset(rowcount, 28) = Me.CB23.Value
.Offset(rowcount, 29) = Me.CB24.Value
.Offset(rowcount, 30) = Me.CB25.Value
.Offset(rowcount, 31) = Me.CB26.Value
.Offset(rowcount, 32) = Me.CB27.Value
End With
End If
'A&R 5X10N
If Me.CB7.Value = "A&R" And Me.CB23.Value = "5x10N" Then
rowcount = Worksheets("A&R").Range("AM3").CurrentRegion.Rows.Count
With Worksheets("A&R").Range("AM1")
'PROJECT
.Offset(rowcount, 0) = Me.CB1.Value
.Offset(rowcount, 1) = Me.TB2.Value
.Offset(rowcount, 2) = Me.TB3.Value
.Offset(rowcount, 3) = Me.CB4.Value
.Offset(rowcount, 4) = Me.CB7.Value
.Offset(rowcount, 5) = Me.TB1.Value
'FINISH DATES
.Offset(rowcount, 7) = Me.TB23.Value
.Offset(rowcount, 8) = Me.TB24.Value
.Offset(rowcount, 9) = Me.TB25.Value
.Offset(rowcount, 10) = Me.TB26.Value
.Offset(rowcount, 11) = Me.TB27.Value
.Offset(rowcount, 12) = Me.TB28.Value
.Offset(rowcount, 13) = Me.TB29.Value
'PRODUCTION
.Offset(rowcount, 14) = Me.TB8.Value
.Offset(rowcount, 15) = Me.TB9.Value
.Offset(rowcount, 16) = Me.TB10.Value
.Offset(rowcount, 17) = Me.TB11.Value
.Offset(rowcount, 18) = Me.TB12.Value
.Offset(rowcount, 19) = Me.TB13.Value
.Offset(rowcount, 20) = Me.TB14.Value
'ASSETS
.Offset(rowcount, 21) = Me.TB16.Value
.Offset(rowcount, 22) = Me.TB17.Value
.Offset(rowcount, 23) = Me.TB18.Value
.Offset(rowcount, 24) = Me.TB19.Value
.Offset(rowcount, 25) = Me.TB20.Value
.Offset(rowcount, 26) = Me.TB21.Value
.Offset(rowcount, 27) = Me.TB22.Value
.Offset(rowcount, 28) = Me.CB23.Value
.Offset(rowcount, 29) = Me.CB24.Value
.Offset(rowcount, 30) = Me.CB25.Value
.Offset(rowcount, 31) = Me.CB26.Value
.Offset(rowcount, 32) = Me.CB27.Value
End With
End If
答案 0 :(得分:0)
我建议去2个潜艇。一个驱动船(ProcessRecord)和一个执行咕噜声工作(WriteRecord)。这至少有助于避免重复所有这些偏移。
UIView's subclass
答案 1 :(得分:0)
以下是如何缩短代码的示例。这是您的代码中的一部分:
.Offset(rowcount, 7) = Me.TB23.Value
.Offset(rowcount, 8) = Me.TB24.Value
.Offset(rowcount, 9) = Me.TB25.Value
.Offset(rowcount, 10) = Me.TB26.Value
.Offset(rowcount, 11) = Me.TB27.Value
.Offset(rowcount, 12) = Me.TB28.Value
.Offset(rowcount, 13) = Me.TB29.Value
'PRODUCTION
.Offset(rowcount, 14) = Me.TB8.Value
.Offset(rowcount, 15) = Me.TB9.Value
.Offset(rowcount, 16) = Me.TB10.Value
.Offset(rowcount, 17) = Me.TB11.Value
.Offset(rowcount, 18) = Me.TB12.Value
.Offset(rowcount, 19) = Me.TB13.Value
.Offset(rowcount, 20) = Me.TB14.Value
'ASSETS
.Offset(rowcount, 21) = Me.TB16.Value
.Offset(rowcount, 22) = Me.TB17.Value
.Offset(rowcount, 23) = Me.TB18.Value
.Offset(rowcount, 24) = Me.TB19.Value
.Offset(rowcount, 25) = Me.TB20.Value
.Offset(rowcount, 26) = Me.TB21.Value
.Offset(rowcount, 27) = Me.TB22.Value
由于所有值都是以数字顺序排列并且都以TB开头,因此您可以使用循环来缩短代码:
For x = 7 To 27
If x = 7 Then
t = 23
ElseIf x = 14 Then
t = 8
ElseIf x = 21 Then
t = 16
End If
.Offset(rowcount, x) = Me.Controls("TB" & t).Value
t = t + 1
Next x
答案 2 :(得分:0)
以下是这个想法:
project = Array(CB1.value, TB2.value, TB3.value, CB4.value, CB7.value, TB1.value)
.Offset(rowcount, 0).resize(1, Ubound(project)-1).Value = project
finishDates= Array(TB23.value, TB24.value, TB25.value, TB26.value, TB27.value, TB28.value, TB29.Value)
.Offset(rowcount, 7).resize(1, Ubound(finishDates)-1).Value = finishDates
等等......
P.S。如果您的控件按顺序编号,您可以编写一个利用其名称顺序的循环,但这不是您的情况(并非总是AFAICS),而且不建议依赖它,因为它很难维护。
答案 3 :(得分:0)
您可以按如下方式重构代码:
If Me.CB7.Value = "A&R" Then 'A&R
With Worksheets("A&R")
Select Case Me.CB23.Value
Case "5x9N" '5X9N
Write_em .Range("C1"), .Range("C3").CurrentRegion.Rows.Count
Case "5x10N" '5X1N
Write_em .Range("AM1"), .Range("AM3").CurrentRegion.Rows.Count
End Select
End With
End If
利用以下Write_em()
子:
Sub Write_em(rng As Range, RowCount As Long)
With rng
'PROJECT
.Offset(RowCount, 0) = Me.CB1.Value
.Offset(RowCount, 1) = Me.TB2.Value
.Offset(RowCount, 2) = Me.TB3.Value
.Offset(RowCount, 3) = Me.CB4.Value
.Offset(RowCount, 4) = Me.CB7.Value
.Offset(RowCount, 5) = Me.TB1.Value
'FINISH DATES
.Offset(RowCount, 7) = Me.TB23.Value
.Offset(RowCount, 8) = Me.TB24.Value
.Offset(RowCount, 9) = Me.TB25.Value
.Offset(RowCount, 10) = Me.TB26.Value
.Offset(RowCount, 11) = Me.TB27.Value
.Offset(RowCount, 12) = Me.TB28.Value
.Offset(RowCount, 13) = Me.TB29.Value
'PRODUCTION
.Offset(RowCount, 14) = Me.TB8.Value
.Offset(RowCount, 15) = Me.TB9.Value
.Offset(RowCount, 16) = Me.TB10.Value
.Offset(RowCount, 17) = Me.TB11.Value
.Offset(RowCount, 18) = Me.TB12.Value
.Offset(RowCount, 19) = Me.TB13.Value
.Offset(RowCount, 20) = Me.TB14.Value
'ASSETS
.Offset(RowCount, 21) = Me.TB16.Value
.Offset(RowCount, 22) = Me.TB17.Value
.Offset(RowCount, 23) = Me.TB18.Value
.Offset(RowCount, 24) = Me.TB19.Value
.Offset(RowCount, 25) = Me.TB20.Value
.Offset(RowCount, 26) = Me.TB21.Value
.Offset(RowCount, 27) = Me.TB22.Value
.Offset(RowCount, 28) = Me.CB23.Value
.Offset(RowCount, 29) = Me.CB24.Value
.Offset(RowCount, 30) = Me.CB25.Value
.Offset(RowCount, 31) = Me.CB26.Value
.Offset(RowCount, 32) = Me.CB27.Value
End With
End Sub
此外,为了缩短代码,您可以按如下方式重构后者:
Sub Write_em(rng As Range, RowCount As Long)
With rng
'PROJECT
.Offset(RowCount, 0).Resize(, 6).Value = Array(Me.CB1.Value, Me.TB2.Value, Me.TB3.Value, Me.CB4.Value, Me.CB7.Value, Me.TB1.Value)
'FINISH DATES
WriteTBs .Offset(RowCount, 7), 23, 29
'PRODUCTION
WriteTBs .Offset(RowCount, 14), 8, 14
'ASSETS
WriteTBs .Offset(RowCount, 21), 16 27
End With
End Sub
Sub WriteTBs(rng As Range, iniTB As Integer, endTB As Integer)
Dim iTb As Integer, colOffset As Integer
With rng
For iTb = iniTB To endTB
.Offset(, colOffset) = Me.Controls("TB" & iTb).Value
colOffset = colOffset + 1
Next
End With
End Sub