简体   繁体   中英

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. 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. I would prefer not to change the range to an Excel table but could if absolutely necessary.

    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). The second table (columns AO:BF) is used as a default price table. 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. 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.

Do not use On Error Resume Next , this is a bad practice. 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...":

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.

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") .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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