简体   繁体   English

如何处理 VBA 代码以包含添加的行和列?

[英]How do I handle VBA code to include added rows and columns?

Still trying to get this to work.仍在尝试使其正常工作。 Excel is crashing when I insert a row after changing the code to include a named range as shown below. Excel 在更改代码以包含命名范围后插入一行时崩溃,如下所示。 Maybe this is due to there being no data in the offset cells?也许这是由于偏移单元格中没有数据?

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)


    Dim xSht As Worksheet
    Set xSht = ActiveWorkbook.ActiveSheet
    Dim xRg As Range, xCell As Range
    Set xRg = Range("DevTable")
    If Not Intersect(Target, xRg) Is Nothing Then
        For Each xCell In xRg
            If xCell = "" Then
                xCell.Value = xCell.Offset(0, 35).Value
                End If
        Next xCell
    End If
End Sub

I would like to modify the code below so that if the range (F6:F42) changes the added rows/columns would be included.我想修改下面的代码,以便如果范围 (F6:F42) 更改,添加的行/列将被包括在内。 I would prefer not to change the range to an Excel table but could if absolutely necessary.我不希望将范围更改为 Excel 表,但如果绝对必要的话可以。

    Private Sub Worksheet_Change(ByVal Target As Range)

    On Error Resume Next
    Dim xSht As Worksheet
    Set xSht = ActiveWorkbook.ActiveSheet
    Dim xRg As Range, xCell As Range
    Set xRg = xSht.Range("F6:W42")
    If Not Intersect(Target, Range("F6:W42")) Is Nothing Then
        For Each xCell In xRg
            If xCell = "" Then
                xCell.Value = xCell.Offset(0, 35).Value
            End If
        Next xCell
    End If
End Sub>

For those of you interested, I have two identical price tables (not official excel tables).对于那些感兴趣的人,我有两个相同的价格表(不是官方的 excel 表)。 The second table (columns AO:BF) is used as a default price table.第二个表(AO:BF 列)用作默认价格表。 The first table just pulls the values from the second table via simple formulas, for example the first cell has the formula =AO6, etc. If the user decides to lower the price for a couple months he can enter the new price in the first table.第一个表只是通过简单的公式从第二个表中提取值,例如第一个单元格的公式 =AO6 等。如果用户决定在几个月内降低价格,他可以在第一个表中输入新价格. The cell is highlighted through conditional formatting to remind him that the price has been changed.该单元格通过条件格式突出显示,以提醒他价格已更改。 However, after the sale is over, he can later just hit delete and go back to the original price.但是,在销售结束后,他可以稍后只需点击删除并将 go 恢复到原价。

Do not use On Error Resume Next , this is a bad practice.不要使用On Error Resume Next ,这是一种不好的做法。 There is a way to catch the error if it exists and handle it correspondingly . 如果错误存在,有一种方法可以捕获并相应地处理它 Anyway, concerning the question - table is the only possible option to do it, if you want to stay "clean".无论如何,关于这个问题 - 如果你想保持“干净”,桌子是唯一可能的选择。

Without a table, it should write somewhere the added columns and rows.如果没有表格,它应该在某处写入添加的列和行。 Eg in an additional "Log" sheet.例如,在附加的“日志”表中。 Then calculate the needed range.然后计算所需的范围。 But it is too much of calculation and the implementation would be a nightmare.但计算量太大,实施将是一场噩梦。

Here is the table implementation, without "On Error...":这是表实现,没有“On Error...”:

Private Sub Worksheet_Change(ByVal Target As Range)

    Dim xRg As Range
    Dim xCell As Range
    Set xRg = Range("myTable")

    If Not Intersect(Target, xRg) Is Nothing Then
        For Each xCell In xRg
            If xCell = "" Then
                xCell.Value = "Something blue"
            End If
        Next xCell
    End If

End Sub

Furthermore, I am not sure why it is needed to loop through xRg and not through the "Target", but I guess there is a good reason about it.此外,我不确定为什么需要循环通过xRg而不是通过“目标”,但我想这是有充分理由的。

A trick is to use " Named Ranges " in your sheets.一个技巧是在您的工作表中使用“命名范围”。

Set xRg = xSht.Range("F6:W42")

can become可以变成

Set xRg = xSht.Range("UsuablePrices")  ' <-- I made the name up

Now, whenever anyone adds to the cells (in a controlled and proper fashion) the named range changes, but you don't have to modify your code.现在,每当有人(以受控和适当的方式)添加到单元格时,命名范围都会更改,但您不必修改代码。

For example, a line is inserted at Row 41. Your named range now refers to "F6:W43", but your code remains as Set xRg = xSht.Range("UsuablePrices") .例如,在第 41 行插入一行。您的命名范围现在引用“F6:W43”,但您的代码仍为Set xRg = xSht.Range("UsuablePrices")

If the practice is to add items at the end of the range, you can include a blank line at the end and get everyone to add items above the line.如果做法是在范围的末尾添加项目,您可以在末尾包含一个空行,并让每个人都在该行上方添加项目。

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

相关问题 VBA:如何将多列合并为 1 ? 我当前的代码有效,但只有 &lt;450 行 - VBA: How do I merge several columns into 1 ? My current code works but only <450 rows 如何在VBA中跨范围的行和列循环? - How do I loop across both rows and columns of a range in VBA? 如果满足条件,我如何从 excel 编辑我的 VBA 代码以在主题中包含特定行? - How do i edit my VBA Code from excel to include a specific line in the subject if conditions are met? 如何仅将可见列和行打印/保存到VBA中的XLSX文件? - How do I print/save only the visible columns and rows to an XLSX file in VBA? 如何抽象我的 VBA 代码以顺利处理 nxn 矩阵? - How do I abstract my VBA code to handle n x n matrix smoothly? 如何删除 VBA 中的重复行? - How do i delete duplicated rows in VBA? 我需要为第 5-200 行重复 VBA 代码,但不知道该怎么做 - I need to repeat a VBA code for rows 5-200 and have no idea how what to do 如何在 VBA 中编写一个宏来删除 excel 中未出现在数组中的列? - How do I code a macro in VBA that deletes columns in excel that don't appear in an array? 如何在总和中包括两列数据,直到达到某个值,然后对剩余的行仅累加一列? - How do I include two columns of data in a sum until a certain value is reached, then sum just one column for the remaining rows? VBA-如何调用一段代码? - VBA - How do I call a section of code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM