+ Reply to Thread
Results 1 to 14 of 14

Range selection problem

Hybrid View

  1. #1
    Registered User
    Join Date
    03-27-2014
    Location
    Custer Park, IL
    MS-Off Ver
    Excel 2010
    Posts
    13

    Range selection problem

    Can somebody please check this code out and let me know what's wrong with the "Range" selection. When I run the below listed code, it errors out.

    Worksheets("RDH1").Activate
    Sheets("RDH1").Unprotect "xyz"
    eRow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
    Range(Cells(9, 1), Cells(eRow, 28)).Select
    Selection.ClearContents
    Sheets("RDH1").Protect "xyz"
    I have check throughout the net and I still can't figure out the problem. Thanks in advance for your HELP.

  2. #2
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,794

    Re: Range selection problem

    Perhap you need to "Dim" eRow? I.e.

    Sub test()
    Dim eRow As Integer
    Worksheets("RDH1").Activate
    Sheets("RDH1").Unprotect "xyz"
    eRow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
    Range(Cells(9, 1), Cells(eRow, 28)).Select
    Selection.ClearContents
    Sheets("RDH1").Protect "xyz"
    End Sub
    You don't need to use "Select" and "Selection" try using only the commands.

    Range(Cells(9, 1), Cells(eRow, 28)).ClearContents
    Alf

  3. #3
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,199

    Re: Range selection problem

    Hi, Alf,

    IŽd use Long instead of Integer (for the scope of the new worksheets anyhow ) - from what I remember from Excel2002 on any Integer value is internally transferred to a Long anyhow.

    Sub test()
    Dim eRow As Long
    With Worksheets("RDH1")
      .Unprotect "xyz"
      eRow = .Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
      .Range(.Cells(9, 1), .Cells(eRow, 28)).ClearContents
      .Protect "xyz"
    End With
    End Sub
    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  4. #4
    Registered User
    Join Date
    03-27-2014
    Location
    Custer Park, IL
    MS-Off Ver
    Excel 2010
    Posts
    13

    Re: Range selection problem

    Thanks for the reply. I tried to Dim eRow but I still receive this error:

    Run Time Error '1004'
    Select Method of Range Class Failed

    This one has me baffled!!!!

  5. #5
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,199

    Re: Range selection problem

    Hi, Riverridgefarm,

    with RTE 1004 IŽd ask you to attach your workbook (or my favourite suggestion: check for merged cells or comments).

    Ciao,
    Holger

  6. #6
    Forum Guru sktneer's Avatar
    Join Date
    04-30-2011
    Location
    Kanpur, India
    MS-Off Ver
    Office 365
    Posts
    9,655

    Re: Range selection problem

    If you start a sub-routine code with Option Explicit, you need to declare all the variables used in the code otherwise you will get an error "variable not defined". Were you getting the same error? If so declare the variable eRow As Long, otherwise your code looks ok.
    Regards
    sktneer


    Treat people the way you want to be treated. Talk to people the way you want to be talked to.
    Respect is earned NOT given.

  7. #7
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,719

    Re: Range selection problem

    The Range looks OK to me.
    Try this.
    Sub Try()
        Dim eRow As Long
        Worksheets("RDH1").Activate
        ActiveSheet.Unprotect Password:="xyz"
        eRow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
        Range(Cells(9, 1), Cells(eRow, 28)).ClearContents
        ActiveSheet.Protect Password:="xyz"
    End Sub

  8. #8
    Registered User
    Join Date
    03-27-2014
    Location
    Custer Park, IL
    MS-Off Ver
    Excel 2010
    Posts
    13

    Re: Range selection problem

    I tried the latest suggestion. Now I get that the sheet is Protected but the Debug highlights the Range Code again. I looked at Sheet("RDH1") and it is Unprotected. So I Protected it again, reset the Debug, and ran the program again and I still get the same error: Sheet is Protected and the Range Code is highlighted.

  9. #9
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,719

    Re: Range selection problem

    Do you have the right password in the code? Capitals maybe

  10. #10
    Registered User
    Join Date
    03-27-2014
    Location
    Custer Park, IL
    MS-Off Ver
    Excel 2010
    Posts
    13

    Re: Range selection problem

    Absolutely no merged cells or comments in the whole sheet.

  11. #11
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,794

    Re: Range selection problem

    Hi Holger,

    Yes you are right but old habits die hard i.e. I'm used to small ranges so "Integer" with a limit of 32 767 is ok but for an OP question one should use "Long" to be on the safe side.

    Alf

  12. #12
    Registered User
    Join Date
    03-27-2014
    Location
    Custer Park, IL
    MS-Off Ver
    Excel 2010
    Posts
    13

    Re: Range selection problem

    Password is correct. Went to the sheet and Protected / Unprotected a couple of times. All lower case.

  13. #13
    Registered User
    Join Date
    03-27-2014
    Location
    Custer Park, IL
    MS-Off Ver
    Excel 2010
    Posts
    13

    Re: Range selection problem

    Just took out the Unprotect code. Went to Sheet "RDH1" and ensured that it was Unprotected. Put a MsgBox in after eRow and it came up with 42 which is correct. Still get the Range Code highlighted and it's still coming up with the error saying that the sheet is protected. WHAT A PROBLEM.......

  14. #14
    Forum Guru sktneer's Avatar
    Join Date
    04-30-2011
    Location
    Kanpur, India
    MS-Off Ver
    Office 365
    Posts
    9,655

    Re: Range selection problem

    Upload the sample workbook so that the problem can be solved.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Problem with Range,Selection, Copy and Paste
    By ljunggrenus in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-20-2007, 07:19 AM
  2. Range Selection problem
    By Coolboy55 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-03-2007, 12:58 PM
  3. Range Selection Problem
    By boylejob in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-02-2007, 12:09 AM
  4. [SOLVED] VBA : Excel Range Selection Problem
    By Learner in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 02-28-2005, 02:06 PM
  5. [SOLVED] [SOLVED] Range selection problem
    By Brian in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-04-2005, 10:06 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1