Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9260425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:53:19+00:00 2026-06-18T12:53:19+00:00

I wonder whether someone may be able to help me please. For a few

  • 0

I wonder whether someone may be able to help me please.

For a few weeks now I’ve been trying to find a solution whereby users can do the following:

  • Delete rows with and without data,
  • Shift all rows containing data aso that they sit one under another,
  • Whilst maintaining a defined ‘Input Range’

I’ve put together the following script which clears the cell contents and hence doesn’t alter the ‘Input Range’.

Sub DelRow()

      Dim msg

          Sheets("Input").Protect "handsoff", userinterfaceonly:=True
          Application.EnableCancelKey = xlDisabled
          Application.EnableEvents = False
          msg = MsgBox("Are you sure you want to delete this row?", vbYesNo)
          If msg = vbNo Then Exit Sub
          With Selection
              Application.Intersect(.Parent.Range("A:S"), .EntireRow).Interior.ColorIndex = xlNone
              Application.Intersect(.Parent.Range("T:AE"), .EntireRow).Interior.ColorIndex = 42
              Selection.SpecialCells(xlCellTypeConstants).ClearContents
              Application.Intersect(.Parent.Range("C:AE"), .EntireRow).Locked = True
              Application.Intersect(.Parent.Range("AG:AG"), .EntireRow).Locked = True
          End With
              Application.EnableEvents = True
      End Sub

Updated Code

Sub DelRow()
Dim RangeToClear As Range
Dim msg As VbMsgBoxResult

'Sheets("Input").Protect "handsoff", userinterfaceonly:=True
Application.EnableCancelKey = xlDisabled
Application.EnableEvents = False
msg = MsgBox("Are you sure you want to delete this row?", vbYesNo)
If msg = vbNo Then Exit Sub
With Selection
    Application.Intersect(.Parent.Range("A:S"), .EntireRow).Interior.ColorIndex = xlNone
    Application.Intersect(.Parent.Range("T:AE"), .EntireRow).Interior.ColorIndex = 42
    On Error Resume Next
    Set RangeToClear = Selection.SpecialCells(xlCellTypeConstants)
    On Error GoTo 0    ' or previously defined error handler
    If Not RangeToClear Is Nothing Then
        RangeToClear.ClearContents
    Else
    Selection.Sort Key1:=Range("B7"), Order1:=xlAscending, Header:=xlNo, _
        OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
        DataOption1:=xlSortNormal
    End If
    Application.Intersect(.Parent.Range("C:AE"), .EntireRow).Locked = True
    Application.Intersect(.Parent.Range("AG:AG"), .EntireRow).Locked = True
End With
Application.EnableEvents = True
End Sub

The problem with this though, is that if a user selects a blank row they receive a ‘Error 400’ message and it doesn’t shift the rows up to sit underneath each other.

As I said, I’ve spent so much time on this trying to find a solution without any success.

I really would be so grateful if someone could look at this please and offer some guidance on how I may achieve this.

Many thanks and kind regards

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T12:53:20+00:00Added an answer on June 18, 2026 at 12:53 pm

    If the selection is blank, the line Selection.SpecialCells(xlCellTypeConstants).ClearContents
    will fail because there are no xlCellTypeConstants. You need to test this and only clear the content if there are any:

    EDIT: To try to answer Sorting question

    I think you just want to sort no matter what, so I just moved the Sort to after the ClearContents. I sorted the UsedRange though, which I don’t think is what you want. You need to define the range to be sorted, either as a named range using the Name Manager in Excel, or in your code.

    Sub DelRow()
    Dim RangeToClear As Range
    Dim msg As VbMsgBoxResult
    
    Sheets("Input").Protect "handsoff", userinterfaceonly:=True
    Application.EnableCancelKey = xlDisabled
    Application.EnableEvents = False
    msg = MsgBox("Are you sure you want to delete this row?", vbYesNo)
    If msg = vbNo Then Exit Sub
    With Selection
        Application.Intersect(.Parent.Range("A:S"), .EntireRow).Interior.ColorIndex = xlNone
        Application.Intersect(.Parent.Range("T:AE"), .EntireRow).Interior.ColorIndex = 42
        On Error Resume Next
        Set RangeToClear = Selection.SpecialCells(xlCellTypeConstants)
        On Error GoTo 0    ' or previously defined error handler
        If Not RangeToClear Is Nothing Then
            RangeToClear.ClearContents
        End If
        'You need to define a range that you want sorted
        'here I've used UsedRange
        ActiveSheet.UsedRange.Sort Key1:=Range("B7"), Order1:=xlAscending, Header:=xlNo, _
                       OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
                       DataOption1:=xlSortNormal
    
        Application.Intersect(.Parent.Range("C:AE"), .EntireRow).Locked = True
        Application.Intersect(.Parent.Range("AG:AG"), .EntireRow).Locked = True
    End With
    Application.EnableEvents = True
    End Sub
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wonder whether someone may be able to help me please. I'm trying to
I wonder whether someone may be able to help me please. I've been working
I wonder whether someone may be able to help me please. I'm trying to
I wonder whether someone may be able to help please. I'm trying to put
I wonder whether someone could help me please. I've been trying to find a
I wonder whether someone may be able to help me please. I'm trying to
I wonder whether someone may be able to help me please. Firstly apologies as
I wonder whether someone may be able to help me please. I'm using the
I wonder whether someone may be able to help me please Firstly, my apologies
I wonder whether someone may be able to help me please. Firstly, this is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.