Excel VBA; 用户窗体,运行在不同的文本框值的一段代码

晚上好,stackoverflow社区。

UPD:

关于我的真实情况的一些信息。 这里是我使用的UserForm: 用户窗体

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

这个macros的主要目的是复制一个表格模板,并以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 (somacros产生的表2将接近macros产生的表1,而不覆盖它),对于填充的每个持续时间/ CED文本框。

如果有人可以提出解决scheme,我真的很感激!

先谢谢你

这个怎么样?

 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事件中的,但是你应该能够把它从事件中拿出来,让它运行得很好(你可能需要修改某些部分以适应你的情况,例如,如果你想要的值去不同的目的地); 基本上它会做的是循环遍历用户窗体中的所有控件,如果控件是一个文本框,它将把这个值放到相应的单元格中(这就是你的问题本来要求做的)。