繁体   English   中英

创建工作表后立即将内容添加到单元格

[英]Add content to cell immediately after creating the sheet

我正在编码一个VBA脚本,该脚本是:1.在一张纸上创建一些列2.创建新纸页3.将内容添加到此新纸页

问题是,当我运行脚本时,一切正常,但内容未添加到新表中。 如果我重新运行脚本,则内容将添加到先前创建的工作表中。 从第一轮开始如何运作? 这是脚本:

Sub AddColumn()
    'Declare variables
    Dim lastUsedRow As String
    Dim release As String


    'Get the number of current release
    release = ActiveSheet.Name

    'Get the number of the current Sheet

    'Affect values to variables
    lastUsedRow = CStr(last_Used_Row())

    'Write the column header
    Range("G1").Value = "Position"
    Range("H1").Value = "Requested"
    Range("I1").Value = "Planned"
    Range("J1").Value = "Delivered"
    Range("K1").Value = "Tested"
    Range("L1").Value = "Validated"

    'Formula for POSITION
    Range("G2:G" & lastUsedRow).Formula = "=LOOKUP(A:A,'Raw Data'!B:B,'Raw Data'!D:D)"

    'Formula for REQUESTED
    Range("H2:H" & lastUsedRow).FormulaR1C1 = "=IF(ISBLANK(R[0]C[-4]), ""NO"", ""YES"")"

    'Formula for PLANNED
    Range("I2:I" & lastUsedRow).FormulaR1C1 = "=IF(ISBLANK(R[0]C[-4]), ""NO"", ""YES"")"

    'Formula for DELIVERED
    Range("J2:J" & lastUsedRow).FormulaR1C1 = "=IF(ISBLANK(R[0]C[-4]), ""NO"", ""YES"")"

    'Formula for TESTED
    Range("K2:K" & lastUsedRow).FormulaR1C1 = _
    "=IF(OR(R[0]C[-1]=""NO"",AND(R[0]C[-1]=""YES"",OR(R[0]C[-4]=""40-To be tested"", R[0]C[-4]=""41-Pending retest"",R[0]C[-4]=""30-Fixed""))),""NO"",""YES"")"

    'Formula for VALIDATED
    Range("L2:L" & lastUsedRow).FormulaR1C1 = "=IF(AND(R[0]C[-2]=""YES"",R[0]C[-4]=""99-Closed""),""YES"",""NO"")"


    'Create the new sheet
        'Step 1: Tell Excel what to do if error
         On Error GoTo MyError
        'Step 2:  Add a sheet and name it
        Sheets.Add
        ActiveSheet.Name = "Stats " & release
        Exit Sub
        'Step 3: If here, an error happened; tell the user
MyError:
        MsgBox "There is already a sheet called that."

    'Write on the sheet the row and column headers

    With Worksheets("Stats " & release)
        .Range("B7").Value = "S1-Blocking"
        .Range("B8").Value = "S2-Major"
        .Range("B9").Value = "S3-Medium"
        .Range("B10").Value = "S4-Minor"
        .Range("B11").Value = "Total"

        .Range("J7").Value = "S1-Blocking"
        .Range("J8").Value = "S2-Major"
        .Range("J9").Value = "S3-Medium"
        .Range("J10").Value = "S4-Minor"
        .Range("J11").Value = "Total"

        .Range("B13").Value = "S1-Blocking"
        .Range("B14").Value = "S2-Major"
        .Range("B15").Value = "S3-Medium"
        .Range("B16").Value = "S4-Minor"
        .Range("B17").Value = "Total"

        .Range("J13").Value = "S1-Blocking"
        .Range("J14").Value = "S2-Major"
        .Range("J15").Value = "S3-Medium"
        .Range("J16").Value = "S4-Minor"
        .Range("J17").Value = "Total"

        .Range("C6").Value = "Requested"
        .Range("D6").Value = "Planned"
        .Range("E6").Value = "Delivered"
        .Range("F6").Value = "Tested"
        .Range("G6").Value = "Working"

        .Range("K6").Value = "Requested"
        .Range("L6").Value = "Planned"
        .Range("M6").Value = "Delivered"
        .Range("N6").Value = "Tested"
        .Range("O6").Value = "Working"

        'Merge cells where needed
        .Range("A7:A11").Merge

        .Range("A13:A17").Merge
        '.Range("A13:A17").Value = "Other"
        .Range("I7:I11").Merge

        .Range("I13:I17").Merge
        '.Range("I13:I17").Value = "Other"
    End With

End Sub

Workbook_NewSheet是所需的事件。 它应该在VBE中的ThisWorkbook类中:

在此处输入图片说明

这将在范围A1:A5写入“新内容”:

Option Explicit

Private Sub Workbook_NewSheet(ByVal Sh As Object)

    With Sh
        .Range("A1:A5") = "new content"
    End With

End Sub

这部分在这里:

'Create the new sheet
'Step 1: Tell Excel what to do if error
On Error GoTo MyError
'Step 2:  Add a sheet and name it
Sheets.Add
ActiveSheet.Name = "Stats " & release
Exit Sub

除非有错误,否则将始终退出子程序。 如果删除Exit Sub它将可以正常工作。

如果单步执行代码,您将看到正在发生的情况,工作表已经制作完成,它会跳过exit子项;如果正在创建工作表,那么它将退出。 您可能还需要在添加新工作表之前检查名称,以免创建不必要的工作表。

暂无
暂无

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

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