Results 1 to 3 of 3

Thread: Selecting cell in VBA AND sorting

  1. #1

    Thread Starter
    Member
    Join Date
    May 2011
    Posts
    42

    Selecting cell in VBA AND sorting

    Hello all, I have 2 questions, which are both probably extremely easy for you experienced users. Any help would be much appreciated.

    1. Why does something like
    Code:
    Worksheets("Sheet1").Range("B1").Select
    not work? Is the only way to accomplish this to do
    Code:
    Worksheets("Sheet1").Select
    Range("B1").Select
    ???

    2. As far as sorting, starting in Excel 2007, you can sort with up to 255 sort fields, right? I noticed that selecting some range and doing .Sort and then typing in Key1:= and Key2:= and Key3:= works, but only up to 3 sort fields can be used in this way. Why is that? I have found an alternative (from recording a macro). Is this the only way? It would be doing something like

    Code:
    With ActiveWorkbook.Worksheets("sheet1").Sort
      .SortFields.Clear
      .SortFields.Add Key:=Range("B1") 
      and add more keys as you need, then later
      .SetRange Range("A1:X30")             ' or whatever
      .Apply
    End With
    That is, using that method, you can add more than 3 keys and it works just fine.

    Also, regarding this question, my number of rows might change from time to time and I don't want to have to redo the code each time. Can I use

    .SetRange UsedRange

    to get all my data in there?

    Thanks!

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Selecting cell in VBA AND sorting

    .SetRange UsedRange
    did you try it?
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    Member
    Join Date
    May 2011
    Posts
    42

    Re: Selecting cell in VBA AND sorting

    Quote Originally Posted by westconn1 View Post
    did you try it?
    No, I didn't. My code is all at work. I'll probably try it when I get there. But, there was nothing wrong with asking just in case. For example, if it doesn't work and you happen to know that, then you could tell me a way that would work, if you happen to know that.

Tags for this Thread

Posting Permissions

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



Click Here to Expand Forum to Full Width