UserForm提交按钮以计算动态公式+插入行

时间:2019-01-10 15:45:55

标签: vba dynamic formula

我使用以下代码来传输我的用户表单数据(从文本框到A2行中的各个单元格值),这对我来说效果很好,因为我希望添加时将数据下推:

class LoginUer(AuthenticationForm):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        # custom class attributes for all the fields, for example: bootstrap themes.
        for field in self.fields:
            self.fields[field].widget.attrs.update({
                'class':'form-control'
                })

        # or you can modify the field attribute one by one.
        self.fields['username'].widget.attrs.update({
                 'class': 'whatever-class', 
                 'placeholder': 'your placeholder here'})
        # same for the password fields

在同一张纸上,有L列,我想计算当前日期与E2列中提到的日期之间的时差(以天数为单位),我希望它是动态的,因此当数据添加到行A2,此公式将应用于L2,同时将公式L保留在列L的其余部分中,以保留将被下推的数据。

我尝试了特殊的复制/粘贴操作,但是它不起作用。...

我希望这是有道理的。

非常感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

我会先对您的代码进行一些组织更改

Private Sub CommandButton1_Click()

Dim emptyRow As Long

'Validation
If WorksheetFunction.CountIf(Sheets("RawData").Range("A:A"), 
Me.TextBox1.Value) = False Then
    MsgBox "Ticket Does Not Exist", vbCritical
End If

'Transfer information
With ThisWorkbook.Sheets("WOTracker")
    .Cells(2, 1).EntireRow.Insert
    'Determine emptyRow
    emptyRow = .Range("L" & Rows.Count).End(xlUp).Row 
    'changed from WorksheetFunction.CountA(Range("A:A")) + 1 so we can retrieve last  
    'row in column L
    .Cells(2, 1).Value = TextBox1.Value
    .Cells(2, 5).Value = TextBox2.Value
    .Cells(2, 2).Value = TextBox3.Value
    .Cells(2, 3).Value = TextBox4.Value
    .Cells(2, 6).Value = TextBox5.Value
    .Cells(2, 7).Value = ComboBox1.Value
    .Cells(2, 8).Value = ComboBox2.Value
    .Cells(2, 9).Value = TextBox8.Value
    .Cells(2, 4).Value = TextBox9.Value
    .Range("L2:L" & emptyRow) = "=DAYS(TODAY(),$E$2)"
End With

'Formatting <- Might consider deleting the lines containing dDate since it's never 
               'used
Dim dDate As Date
dDate = DateSerial(Month(Date), Day(Date), Year(Date))
TextBox2.Value = Format(TextBox2.Value, "mm/dd/yy")
dDate = TextBox2.Value

With ThisWorkbook.Sheets("WOTracker")
    Sheets("WOTracker").Range("A2:Z2").Font.Bold = False
    Sheets("WOTracker").Range("A2:Z2").Font.Underline = xlUnderlineStyleNone
    End With
End Sub

.Range("L2:L" & emptyRow) = "=DAYS(TODAY(),$E$2)"行将为L列中的每个单元格计算当前日期与单元格E2中的日期之间的天差。

相反,如果您想要的是每行从今天开始就行号减去,只需更改

.Range("L2:L" & emptyRow) = "=DAYS(TODAY(),$E$2)"

收件人

.Range("L2") = "=DAYS(TODAY(),$E$2)"

插入操作将自动更新公式。