Add the current selection to the Edit Range with VBA

Lade

New Member
Joined
Jun 7, 2023
Messages
13
Office Version
  1. 365
Platform
  1. Windows
Hello,

I have a protected worksheet that has a certain range of cells set as an Edit Range. I wrote a macro to be able to insert a new line in that edit range, but I can't figure out how to update the Edit Range to include the inserted line. After some research it sounds like maybe the edit range can't be updated but just has to be deleted and then another made with the total range. Is that correct?

VBA Code:
Dim Tstamp As Date
Tstamp = Now()

ActiveSheet.Protection.AllowEditRanges.Add _
Title:=Tstamp, _
Range:=Selection   'Selection is the new row that the macro just added
One thing I tried was naming the new ranges by the date/time that the macro was run, that way each one will be unique, but I couldn't figure out how to set the title of the range with the timestamp. Am I missing something in here?

Basically, I want to run the insert line macro as many times as I need and have the edit range expand accordingly.
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
When you insert a row in a Range object, the Range object expands automatically to include the new row:

VBA Code:
Public Sub Expand_Range()

    Dim r As Range
   
    Set r = Worksheets("Sheet2").Range("A1:D4")
    r.Rows(2).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
    Debug.Print r.Address 'now A1:D5
   
End Sub

The same thing happens with an AllowEditRange object - you don't have to delete the existing object and add a new one with the new row included. This macro shows how to get an existing Allow Edit Range (the first one on Sheet1), unprotect the sheet and insert a new row in the Allow Edit Range:
VBA Code:
Public Sub Expand_Edit_Range()

    Dim ws As Worksheet
    Dim er As AllowEditRange
   
    Set ws = Worksheets("Sheet1")
    Set er = ws.Protection.AllowEditRanges(1)

    MsgBox "Allow Edit Range before expanding: " & er.Range.Address
   
    ws.Unprotect
    er.Range.Rows(2).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
    ws.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
   
    MsgBox "Allow Edit Range after expanding: " & er.Range.Address
   
End Sub
 
Upvote 1
Solution
That makes sense.
Maybe why it wouldn't work for me was I was running the macro on the first row and it wasn't expanding the edit range because it was copying the new row from the row above which is not in the edit range? Maybe I need to adjust the range to include the header row.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,184
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top