简体   繁体   English

VBA Excel-自动运行宏以在上面的单元格输入值后插入新的空白行

[英]VBA Excel - Auto run macro to insert new blank row after cell above has value entered

I am trying to get a macro to auto run to: 我正在尝试让宏自动运行到:

  1. insert a blank row in each section eg Architectural when the data validation row above (in column A) has a value entered into it. 在上面的数据验证行(在A列中)输入值的情况下,在每个部分(例如建筑)中插入空白行。 I entered the code as a sub in the worksheet, when I click run in the developer tab in excel, it inserts a line once, but I would like it to run automatically (after the workbook is opened) every time something is entered into column A. 我在工作表中的子代码中输入了代码,当我在excel的“开发人员”选项卡中单击“运行”时,它会插入一行,但是我希望每次在列中输入内容时它会自动运行(在打开工作簿之后)一种。

Excel的打印屏幕

Sub BlankLine()
    'Updateby20150203
    Dim Rng As Range
    Dim WorkRng As Range

    xTitleId = "KutoolsforExcel"
    Set WorkRng = Application.Selection
    Set WorkRng = Application.InputBox("Range", xTitleId, WorkRng.Address, Type:=8)
    Set WorkRng = WorkRng.Columns(1)

    xLastRow = WorkRng.Rows.count
    Application.ScreenUpdating = False

    For xRowIndex = xLastRow To 1 Step -1
        Set Rng = Range("B" & xRowIndex)
        If Rng.Value = "" = False Then
            Rng.Offset(1, 0).EntireRow.Insert Shift:=xlDown
        End If
    Next

    Application.ScreenUpdating = True
End Sub

I think I can help you with your first question. 我想我可以为您解决第一个问题。

You can automatically start a macro when a cell changes with a Sub Worksheet_Change(ByVal Target As Range) Sub inside the worksheet. 当单元格随着Sub Worksheet_Change(ByVal Target As Range)Sub Worksheet_Change(ByVal Target As Range) Sub发生变化时,您可以自动启动宏。

Here is description: https://support.microsoft.com/en-us/help/213612/how-to-run-a-macro-when-certain-cells-change-in-excel 这是描述: https : //support.microsoft.com/en-us/help/213612/how-to-run-a-macro-when-certain-cells-change-in-excel

You can insert a new row with the following code: 您可以使用以下代码插入新行:

Application.Selection.EntireRow.Insert shift:=xlDown

When you do just that, you will encounter that the new line will again trigger the event to start the macro, hence again inserting a new line. 当您这样做时,您会遇到新行将再次触发事件以启动宏,因此再次插入新行。 This leads to an infinity loop. 这导致无限循环。 To stop this from happening, we need to disable events for the time of the change. 为了阻止这种情况的发生,我们需要在更改期间禁用事件。

Application.EnableEvents = False
Call new_line_below_selection_macro
Application.EnableEvents = True

Here is a question with a similar problem: How to end infinite "change" loop in VBA 这是一个带有类似问题的问题: 如何结束VBA中的无限“更改”循环

I hope this helps. 我希望这有帮助。

Here is the code which should go into the sheet: 这是应该放在工作表中的代码:

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim KeyCells As Range
    Set KeyCells = Range("A1:C10") 'Area this should apply

    If Not Application.Intersect(KeyCells, Range(Target.Address)) _
           Is Nothing Then

        Application.EnableEvents = False
        'either you put your code here
        'Application.Selection.EntireRow.Insert shift:=xlDown
        'or you call it from a module
        Call Module1.BlankLine
        Application.EnableEvents = True

    End If
End Sub

暂无
暂无

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

相关问题 Excel宏,如果在同一行的另一个单元格中输入了一个值,则可以更改单元格的对齐方式,边框和自动换行 - Excel Macro to change cell alignment, borders and wrap text if a value has been entered into another cell in the same row 插入Excel VBA以在新行中运行宏 - Excel VBA to run Macro in new row is Inserted 在Excel VBA中的某个字符串上方插入空白行 - insert a blank row above a certain string in excel vba VBA - Excel 在每个包含特定文本的单元格上方插入行 - VBA - Excel insert row above for each cell containing certain text Microsoft Excel 2013-VBA宏-插入值并选择同一列(下一行)中的下一个单元格 - Microsoft Excel 2013 - VBA Macro - Insert value and select next cell in same column (row underneath) excel宏:对于在一行中具有值的每个单元格,在该值下方插入一行 - excel macro: for each cell with a value in a row, insert a row below with that value Excel VBA:创建宏,该宏删除两列中具有相等值单元格的行 - Excel VBA: create macro that deletes a row that has a cell of equal value in two columns Excel VBA宏以在A列的值为1时添加空白行 - Excel VBA Macro to add a blank row when the value in column A is 1 Excel宏-如果单元格具有特定值,则使接下来的2个单元格为空白 - Excel Macro - making next 2 cells blank if cell has a certain value Excel VBA如果行中有数据,则单元格不能为空 - Excel VBA If row has data, cell cannot be blank
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM