IF在Excel中循环使用多个THEN语句

时间:2018-04-24 09:40:37

标签: excel vba excel-vba

我正在为我的工作拼凑一些工作,因为我不是一个贸易程序员,我正在努力与下面 - 任何帮助将不胜感激。

我有一个名为'Data'的工作表,其信息如下:

enter image description here

列E和F对从外部源粘贴的列B到D中的数据执行操作。

红色数字是新数据粘贴方式的一个示例。下面的函数检查列E中没有值的第一行和列B中值的最后一行。然后循环粘贴公式E2和F2到该范围内的所有行。

我还试图添加一个输入框,询问用户收到信息的日期,并将该日期输入相同范围的A列。

我将下面的内容放在一起,但它会在'ActiveCell.Offset(0,38)行上产生编译错误:语法错误。

再次,非常感谢任何帮助。

由于

Sub Paste_formula()

'Declare variables and set their types
Dim r As Range
Dim CurrRow As Long
Dim LastRow As Long
Dim StatementDate As Date

'Execute a series of statements relating to the 'Data' Worksheet
With Sheets("Data")
    'Set variable's values
    CurrRow = ActiveCell.Row
    LastRow = .Cells(.Rows.Count, "B").End(xlUp).Row
    StatementDate = InputBox("Please enter the date of the statement in the following format: dd/mm/yyyy", "Statement Date")

    'For Loop which establishes the range for r being between the last row with Data in column E and the last row with Data in column B
        For Each r In .Range("B" & CurrRow & ":B" & LastRow)
        'If there is no value in
         If r.Value <> vbNullString Then r.Offset(0, 0) = r
         'Select Column A for any row between
         r.Offset(0, 0).Select
         'Enter the user's input into Cell A of row
         StatementDate = ActiveCell.Value
         'Then move to last column of row
         ActiveCell.Offset(0, 38)
         ' ...select the last column of the row...
         ActiveCell.Offset(0, 38).Select
         ' ...and paste the current selection
         ActiveSheet.Paste

         Next r
End With
End Sub

1 个答案:

答案 0 :(得分:0)

根据数字我猜测应用的公式 - 这不能回答你的问题,但尝试这个版本填充一个只有一行公式的范围(没有循环,复制或粘贴)

Sub Fill_formula()

    'Declare variables and set their types
    Dim LastRowA As Long
    Dim LastRowB As Long
    Dim StatementDate As Date

    'Execute a series of statements relating to the 'Data' Worksheet
    With Sheets("Sheet1")
        'Set variable's values
        LastRowA = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
        LastRowB = .Cells(.Rows.Count, "B").End(xlUp).Row
        StatementDate = InputBox("Please enter the date of the statement in the following format: dd/mm/yyyy", "Statement Date")

        .Range("E" & LastRowA & ":E" & LastRowB).Formula = "=SUM(B" & LastRowA & ":D" & LastRowA & ")"
        .Range("F" & LastRowA & ":F" & LastRowB).Formula = "=B" & LastRowA & "-C" & LastRowA & "-D" & LastRowA

    End With

End Sub
相关问题