UPD:
有关我的真实案例的一些信息。这是我正在使用的UserForm:
在我的Userforms中,持续时间/ CED文本框命名为Dur1-Dur6,而S / C频率文本框命名为sc1-sc6。
宏的主要目的是复制表格模板并将其与主表一起粘贴所需的公式,基于S / C频率,如下所示。
我的完整代码:
Private Sub OkButton_Click()
TheStart:
Dim FirstRow2 As Integer: FirstRow2 = 18 'set the number value of first row with formulas
Dim LastRow2 As Integer: LastRow2 = Range("L1000").End(xlUp).Row
Dim AQCol As Integer: AQCol = 11 'set the number value of AQ column in the Main Table (to calculate relative reference for formulas)
If Supplier_Data.SuppName = "" Then
MsgBox "Please enter supplier's name"
Exit Sub
End If
Dim LastCol1 As Integer: LastCol1 = Range("IV18").End(xlToLeft).Column
If Supplier_Data.Dur1 = "" Or Supplier_Data.sc1 = "" Then
MsgBox "Please enter at least one duration and s/c frequency"
Exit Sub
'copy TEST table
ElseIf Supplier_Data.Dur1 = "TEST" Then
Sheet5.Range("A7:C10").Copy
Sheet6.Cells(15, LastCol1 + 1).PasteSpecial (xlPasteAll)
Sheet6.Cells(15, LastCol1 + 1).Value = Supplier_Data.SuppName.Value & " " & Supplier_Data.Dur1.Value & " " & "offer"
Sheet6.Cells(17, LastCol1 + 1).Value = Supplier_Data.sc1.Value
Else
Sheet5.Range("A2:C5").Copy
Sheet6.Cells(15, LastCol1 + 1).PasteSpecial (xlPasteAll)
Sheet6.Cells(15, LastCol1 + 1).Value = Supplier_Data.SuppName.Value & " " & Supplier_Data.Dur1.Value & " " & "offer"
Sheet6.Cells(17, LastCol1 + 1).Value = Supplier_Data.sc1.Value
End If
'Calculate AS for each line
For i = FirstRow2 To LastRow2 - 1
If Supplier_Data.sc1.Value = "ppd" Then
ASFormula = "= (r[0]c[-2] * 365/100) + (r[0]c[" & AQCol - (LastCol1 + 3) & "] * r[0]c[-1])/100"
ElseIf Supplier_Data.sc1.Value = "PD" Then
ASFormula = "= (r[0]c[-2] * 365) + (r[0]c[" & AQCol - (LastCol1 + 3) & "] * r[0]c[-1])/100"
ElseIf Supplier_Data.sc1.Value = "PM" Then
ASFormula = "= (r[0]c[-2] * 12) + (r[0]c[" & AQCol - (LastCol1 + 3) & "] * r[0]c[-1])/100"
ElseIf Supplier_Data.sc1.Value = "PQ" Then
ASFormula = "= (r[0]c[-2] * 4) + (r[0]c[" & AQCol - (LastCol1 + 3) & "] * r[0]c[-1])/100"
End If
Sheet6.Cells(i, LastCol1 + 3).FormulaR1C1 = ASFormula
Sheet6.Range(Cells(FirstRow2, LastCol1 + 1), Cells(FirstRow2, LastCol1 + 3)).Copy
Sheet6.Range(Cells(i, LastCol1 + 1), Cells(i, LastCol1 + 3)).PasteSpecial (xlPasteFormats)
Next i
'Total Estimated AS
Sheet6.Cells(LastRow2, LastCol1 + 3).FormulaR1C1 = "=SUM(r" & FirstRow2 & "c" & LastCol1 + 3 & ":r" & LastRow2 - 1 & "c" & LastCol1 + 3 & " )"
Sheet6.Range(Cells(LastRow2, LastCol1 + 1), Cells(LastRow2, LastCol1 + 3)).Borders.LineStyle = xlContinuous
Sheet6.Range(Cells(LastRow2, LastCol1 + 1), Cells(LastRow2, LastCol1 + 3)).Font.Bold = True
Supplier_Data.Hide
End Sub
因此,为了不为所有持续时间提供相同的代码段,我正在寻找一种从Dim LastCol1 As Integer: LastCol1 = Range("IV18").End(xlToLeft).Column
开始运行代码的方法(以便宏生成表2将接近宏生成表1填写每个持续时间/ CED文本框,而不是覆盖它。
如果有人可以提出解决方案,我真的很感激!
答案 0 :(得分:0)
这个怎么样?
Private Sub UserForm_Initialize()
Dim c As Control
Dim cnt As Integer: cnt = 1
For Each c In userform1.Controls
If TypeName(c) = "TextBox" Then
Cells(cnt, 1).Value = c.Value * 2 * 3
cnt = cnt + 1
End If
Next c
End Sub
编辑: 看起来你已经完全重写了这个问题,现在想要为你编写一些代码。堆栈溢出不是代码编写服务FYI。我在上面提供的代码是在userform_initialize事件中,但您应该能够将其从事件中取出并使其运行正常(您可能必须修改某些部分以适应您的情况,例如,如果您想要值去另一个目的地);基本上它将做的是循环遍历userform中的所有控件,如果控件是一个文本框,它会将该值放在相应的单元格中(这是你最初要求的问题)。