+ Reply to Thread
Results 1 to 7 of 7

named ranges in a For Loop

  1. #1

    named ranges in a For Loop

    I have named ranges in a worksheet (ques1, ques2,etc.). I need a loop
    to clear the contents of these ranges, something like:

    For i = 1 To 60
    Range("ques & i").Select
    Selection.ClearContents
    Next i

    Any suggestions on how to make this work?


  2. #2
    Gary Keramidas
    Guest

    Re: named ranges in a For Loop

    i think jim gave you a suggestion in your original post 2 days ago. did you
    try it?

    --


    Gary


    <[email protected]> wrote in message
    news:[email protected]...
    >I have named ranges in a worksheet (ques1, ques2,etc.). I need a loop
    > to clear the contents of these ranges, something like:
    >
    > For i = 1 To 60
    > Range("ques & i").Select
    > Selection.ClearContents
    > Next i
    >
    > Any suggestions on how to make this work?
    >




  3. #3
    Chip Pearson
    Guest

    Re: named ranges in a For Loop

    Try

    For i = 1 To 60
    Range("ques" & i ).ClearContents
    Next i


    --
    Cordially,
    Chip Pearson
    Microsoft MVP - Excel
    Pearson Software Consulting, LLC
    www.cpearson.com


    <[email protected]> wrote in message
    news:[email protected]...
    >I have named ranges in a worksheet (ques1, ques2,etc.). I need a
    >loop
    > to clear the contents of these ranges, something like:
    >
    > For i = 1 To 60
    > Range("ques & i").Select
    > Selection.ClearContents
    > Next i
    >
    > Any suggestions on how to make this work?
    >




  4. #4
    chijanzen
    Guest

    RE: named ranges in a For Loop


    TRY,

    Dim n As Name
    For Each n In ThisWorkbook.Names
    If Left(n.Name, 4) = "ques" Then
    Range(n).ClearContents
    End If
    Next

    --
    天行健,君*以自強不息
    地勢坤,君*以厚德載物

    http://www.vba.com.tw/plog/


    "[email protected]" wrote:

    > I have named ranges in a worksheet (ques1, ques2,etc.). I need a loop
    > to clear the contents of these ranges, something like:
    >
    > For i = 1 To 60
    > Range("ques & i").Select
    > Selection.ClearContents
    > Next i
    >
    > Any suggestions on how to make this work?
    >
    >


  5. #5

    Re: named ranges in a For Loop

    I'M SORRY! I had forgotton I had already posted this. Jim's solution
    gives a Method 'Range' of object '_Global' failed. Chip's solution is
    almost the same as Jim's. Gary's earlier solution works wonderfully:

    Sub Clear_rnge()
    For Each nm In ThisWorkbook.Names
    Range(nm).ClearContents
    Next
    End Sub

    Please accept my appologies for double posting and thanks for the help.


  6. #6
    Chip Pearson
    Guest

    Re: named ranges in a For Loop

    > Sub Clear_rnge()
    > For Each nm In ThisWorkbook.Names
    > Range(nm).ClearContents
    > Next
    > End Sub


    This will clear the contents of ALL names, not just those
    beginning with "ques", as the original poster wanted.


    --
    Cordially,
    Chip Pearson
    Microsoft MVP - Excel
    Pearson Software Consulting, LLC
    www.cpearson.com



    <[email protected]> wrote in message
    news:[email protected]...
    > I'M SORRY! I had forgotton I had already posted this. Jim's
    > solution
    > gives a Method 'Range' of object '_Global' failed. Chip's
    > solution is
    > almost the same as Jim's. Gary's earlier solution works
    > wonderfully:
    >
    > Sub Clear_rnge()
    > For Each nm In ThisWorkbook.Names
    > Range(nm).ClearContents
    > Next
    > End Sub
    >
    > Please accept my appologies for double posting and thanks for
    > the help.
    >




  7. #7
    Gary Keramidas
    Guest

    Re: named ranges in a For Loop

    yep, clearly stated in my original post.

    --


    Gary


    "Chip Pearson" <[email protected]> wrote in message
    news:%[email protected]...
    >> Sub Clear_rnge()
    >> For Each nm In ThisWorkbook.Names
    >> Range(nm).ClearContents
    >> Next
    >> End Sub

    >
    > This will clear the contents of ALL names, not just those beginning with
    > "ques", as the original poster wanted.
    >
    >
    > --
    > Cordially,
    > Chip Pearson
    > Microsoft MVP - Excel
    > Pearson Software Consulting, LLC
    > www.cpearson.com
    >
    >
    >
    > <[email protected]> wrote in message
    > news:[email protected]...
    >> I'M SORRY! I had forgotton I had already posted this. Jim's solution
    >> gives a Method 'Range' of object '_Global' failed. Chip's solution is
    >> almost the same as Jim's. Gary's earlier solution works wonderfully:
    >>
    >> Sub Clear_rnge()
    >> For Each nm In ThisWorkbook.Names
    >> Range(nm).ClearContents
    >> Next
    >> End Sub
    >>
    >> Please accept my appologies for double posting and thanks for the help.
    >>

    >
    >




+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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