Consulting

Results 1 to 6 of 6

Thread: quick help on using a variable to define a range

  1. #1
    VBAX Regular
    Joined
    Sep 2013
    Posts
    37
    Location

    quick help on using a variable to define a range

    Sub Macro3()
    
    Dim myrng As Range
     
    With ActiveSheet
        If .AutoFilterMode Then
            .ShowAllData
        End If
    End With
        
    ActiveSheet.AutoFilter.Sort.SortFields. _
            Clear
        ActiveSheet.AutoFilter.Sort.SortFields. _
            Add Key:=Range("M1:M51816"), SortOn:=xlSortOnValues, Order:=xlAscending, _
            DataOption:=xlSortNormal
        With ActiveSheet.AutoFilter.Sort
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
        
        x = 2
        Cells(x, 13).Select
        
        Do While Selection.Value <> ""
         Cells(x, 13).Select
        Selection.Value = Selection.Value
        
       
        
        
       x = x + 1
        
        Loop
        
        myrng = "Range(" & "A2" & ":R" & x - 2 & """)"
        Debug.Print myrng
       myrng.Select
       ' Range("A2:R19").Select
       'Cells(x - 2, 3).EntireRow.Select
        
        'Range(Selection, Selection.End(xlUp)).Select


    I am getting error 91 object variable or with block variable not set. I don't get it. can someone please help?
    Last edited by SamT; 12-09-2013 at 11:17 PM. Reason: Added Code Tags with # Icon

  2. #2
    VBAX Regular
    Joined
    Sep 2013
    Posts
    37
    Location
    I am getting error 91 object variable or with block variable not set. I don't get it. can someone please help?

  3. #3
    VBAX Regular
    Joined
    Sep 2013
    Posts
    37
    Location
    ha nvm didn't set my range my bad

  4. #4
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    not sure what you're trying to accomplish

    Sub Macro3()
    
    
        Dim MyRange As Range
        Dim LastRow As Long
        
        With ActiveSheet
            .Range("A1").CurrentRegion.Sort Key1:=.Range("M2"), Order1:=xlAscending, Header:=xlYes
            LastRow = .Cells(.Rows.Count, "M").End(xlUp).Row
            With .Range("M2:M" & LastRow)
                .Value = .Value
            End With
            Set MyRange = .Range("A2:R" & LastRow - 2)
            'codes related with MyRange
            '....
            '...
        End With
        
    End Sub
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  5. #5
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Set myRng = Range("A2:R" & CStr(x-2))
    'or
    Set myRng = Range(Range("A2"), Cells(18, x-2))
    'or several other ways
    You must SET a variable to an Object.

    You can format the code in your post by either selecting all the code and pressing the # icon, or by pressing the # icon and pasting your code in between the new CODE Tags. Finally, you can type the opening and closing tags before and after your code. "Edit" your post to see an example
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  6. #6
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    This is a merged version of two different threads. all the posts in both threads are displayed in chronological order in this thread, regardless of which original thread they were posted in.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

Posting Permissions

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