简体   繁体   English

Excel:在VBA中插入包含excel公式的新行[更新]

[英]Excel: Insert new row that contains excel formula in VBA[Updated]

I have there the sample data of my project. 我有项目的样本数据。 As you can see there are 4 colums: deadline, submitted, description, & days_delayed. 如您所见,共有4个栏位:截止日期,提交,说明和days_delayed。 In addition, in the same sheet I have the insertion form of data. 另外,在同一张纸上,我有数据的插入形式。 Where Range G1 is the deadline and the G3 is the actual date when the user submits its document. 其中,范围G1是截止日期,而范围G3是用户提交其文档的实际日期。

在此处输入图片说明

In this code you'll the conditional statement written in formula way inside of vba. 在此代码中,您将在vba内以公式方式编写条件语句。 So that every time adds new row the formula will follows and copied to its corresponding cell range. 这样,每次添加新行时,公式都会遵循并复制到其相应的单元格范围。

    Private Sub CommandButton1_Click()

    Dim Deadline, Submitted, Description, Days_Delayed As String
    Worksheets("Sheet3").Select
    Deadline = Range("G1")
    Submitted = Range("G3")
    Description = "=IF(AND(D2>0,ISBLANK(B2)),"NO- 
    DOCUMENT",IF(AND(D2<=0,NOT(ISBLANK(B2))),"ON-TIME", IF(AND(D2 > 0,NOT(ISBLANK(B2))),"DELAYED")))"
    Days_Delayed = "=IF(COUNT(A2:B2)=2,B2-A2,IF(B2="","0"))"


    Worksheets("Sheet3").Select
    Worksheets("Sheet3").Range("A2").Select
    If Worksheets("Sheet3").Range("A2").Offset(1, 0) <> "" Then
    Worksheets("Sheet3").Range("A2").End(xlDown).Select
    End If



    ActiveCell.Offset(1, 0).Select
    ActiveCell.Value = Deadline
    ActiveCell.Offset(0, 1).Select
    ActiveCell.Value = Submitted
    ActiveCell.Offset(0, 1).Select
    ActiveCell.Formula = Description
    ActiveCell.Offset(0, 1).Select
    ActiveCell.Formula = Days_Delayed

    End Sub

This code is working fine but when I tried to implement the Formula inside of the vba codes. 这段代码工作正常,但是当我尝试在vba代码内部实现公式时。 The error say "End Of Statement" 错误说“声明结束”


Fixed By Peh The Insertion of formula to VBA should be like this 由Peh修复将公式插入VBA应该像这样

Days_Delayed = "=IF(COUNT(N36:O36)=2,O36-N36,IF(O36="""",TODAY()-N36))"

Another Question is: 另一个问题是:

Since the add new row is a new data and the formula should not placed a defined range inside of it. 由于添加新行是新数据,因此公式不应在其中放置定义的范围。

Days_Delayed = "=IF(COUNT(N36:O36)=2,O36-N36,IF(O36="""",TODAY()-N36))"

In this formula you can see that the formula ranges are defined. 在此公式中,您可以看到已定义公式范围。 What I'm trying to fix is to do something like this: 我要解决的是做这样的事情:

Days_Delayed = "=IF(COUNT(N(i):O(i))=2,O(i)-N(i),IF(O(i)="""",TODAY()-(i)))"

You can see that there is variable i so when the new row comes in. The formula will automatically knows what cells needed to be calculated. 您会看到有变量i,所以当有新行进入时。该公式将自动知道需要计算哪些单元格。 I guess, looping is the appropriate way for this. 我猜想,循环是实现此目的的合适方法。 Because If I remain the same formula with defined ranges. 因为如果我保持定义范围内的相同公式。 The output will be same. 输出将是相同的。 Is that possible? 那可能吗?

The main issue is that you use .Select . 主要问题是您使用.Select Instead determine the next free row number with 而是使用以下命令确定下一个空闲行号

nRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row + 1 

and use it to access the cells directly ws.Cells(nRow, "A").Value . 并使用它直接访问ws.Cells(nRow, "A").Value

Also don't declare your variables as String instead use Dim Deadline As Range otherwise your dates will be converted to strings and you cannot calculate anymore with the dates. 另外,请勿将变量声明为String而应使用Dim Deadline As Range否则您的日期将被转换为字符串,并且您将无法再使用该日期进行计算。

In the formulas the row numbers need to be substituted by " & nRow & " to insert the row number of the next free row instead of a fixed row number. 在公式中,行号需要用" & nRow & "代替,以插入下一个空闲行的行号,而不是固定的行号。

Option Explicit

Public Sub AddData()
    Dim ws As Worksheet 'define worksheet
    Set ws = ThisWorkbook.Worksheets("Sheet3")

    Dim Deadline As Range 'define deadline range
    Set Deadline = ws.Range("G1")

    Dim Submitted As Range 'define submitted range
    Set Submitted = ws.Range("G3")

    Dim nRow As Long 'find next free row = last used used row +1
    nRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row + 1


    ws.Cells(nRow, "A").Value = Deadline.Value
    ws.Cells(nRow, "B").Value = Submitted.Value

    ws.Cells(nRow, "C").Formula = "=IF(AND(D" & nRow & ">0,ISBLANK(B" & nRow & ")),""NO-DOCUMENT"",IF(AND(D" & nRow & "<=0,NOT(ISBLANK(B" & nRow & "))),""ON-TIME"", IF(AND(D" & nRow & " > 0,NOT(ISBLANK(B" & nRow & "))),""DELAYED"")))"
    ws.Cells(nRow, "D").Formula = "=IF(COUNT(A" & nRow & ":B" & nRow & ")=2,B" & nRow & "-A" & nRow & ",IF(B" & nRow & "="""",""0""))"
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM