Excel VBA; UserForm,运行 一段关于不同 TextBox 值的代码

Excel VBA; UserForm, Run one piece of code on different TextBox values

更新:

关于我的真实案例的一些信息。这是我正在使用的用户窗体:

在我的用户表单中,Duration/CED 文本框被命名为 Dur1-Dur6,S/C 频率文本框被命名为 sc1-sc6。

宏的主要目的是复制一个 table 模板,然后根据 S/C 频率将其粘贴到带有所需公式的主要 table 旁边,如下所示。

我有的完整代码:

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 开始(以便宏生成 Table 2 将接近生成的宏 Table 1,而不是覆盖它),对于每个 Duration/CED 文本框填充。

如果有人能提出解决方案,我将不胜感激!

这个怎么样?

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

编辑: 看起来您已经完全重写了问题,现在想要为您编写一些代码。堆栈溢出不是代码编写服务,仅供参考。我上面提供的代码在 userform_initialize 事件中,但你应该能够将它从事件中取出并让它 运行 就好了(你可能需要修改某些部分以适应你的情况,例如,如果您希望值到达不同的目的地);基本上它会做的是遍历用户窗体中的所有控件,如果控件是文本框,它将将该值放入相应的单元格中(这是你的问题最初要求做的)。