+ Reply to Thread
Results 1 to 11 of 11

VB Sort with variables in the set range property

  1. #1
    Registered User
    Join Date
    01-27-2012
    Location
    Melbourne Australia
    MS-Off Ver
    Excel 2007
    Posts
    9

    VB Sort with variables in the set range property

    Excel 2007 VB 6.5
    I'm trying to use variables (defined) to set the range in the following example instead of actual cell (r,c) references. It does not work , I have tried various ways, it faults out on the line ".SetRange = ActiveSheet.Range(Cells(BrakeSheetPullRow, 2), Cells(BrakeSheetPullRow + 10, uCol)).Select 'ActiveSheet.Range(Cells(5, c1), Cells(20, c2))
    If I substitute .SetRange Range("B17:IQ20") the code works.


    However when the program gets to the line ; “ActiveWorkbook.Worksheets("Brakes").Range(Cells(BrakeSheetPullRow, 2), Cells(BrakeSheetPullRow + 10, uCol)).Select “
    It highlights the range demonstrating that it recognises it…..

    The entire relevant code is:

    Dim BrakeSheetPullRow ‘definition

    BrakeSheetPullRow=2

    ……………………………………………………………………………………………………….
    ActiveWorkbook.Worksheets("Brakes").Range(Cells(BrakeSheetPullRow, 2), Cells(BrakeSheetPullRow + 10, uCol)).Select 'ow+10,uCol)) ‘was Worksheets("Brakes").Range("B17:IQ20").Select
    'ActiveWorkbook.Worksheets("Brakes").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Brakes").Sort.SortFields.Add Key:=Range(Cells(BrakeSheetPullRow + 10, 2), Cells(BrakeSheetPullRow + 10, uCol)), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortTextAsNumbers
    With ActiveWorkbook.Worksheets("Brakes").Sort
    .SetRange = ActiveSheet.Range(Cells(BrakeSheetPullRow, 2), Cells(BrakeSheetPullRow + 10, uCol)).Select ' sample code; ActiveSheet.Range(Cells(5, c1), Cells(20, c2))
    .Header = xlGuess
    .MatchCase = False
    .Orientation = xlLeftToRight
    .SortMethod = xlPinYin
    .Apply
    End With

    I get run time error; 1004; Application defined or object defined error

    Can anybody shed light on why this does not work?
    I get run time error; 1004; Application defined or object defined error
    Last edited by Ian Crane; 02-04-2012 at 11:44 AM.

  2. #2
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: VB Sort with variables in the set range property

    You need to add code tags as per forum rules.

  3. #3
    Valued Forum Contributor smuzoen's Avatar
    Join Date
    10-28-2011
    Location
    Brisbane, Australia
    MS-Off Ver
    Excel 2003/2007/2010
    Posts
    610

    Re: VB Sort with variables in the set range property

    Try
    Please Login or Register  to view this content.
    Anthony
    “Confidence never comes from having all the answers; it comes from being open to all the questions.”
    PS: Remember to mark your questions as Solved once you are satisfied and rate the answer(s) questions.”

  4. #4
    Registered User
    Join Date
    01-27-2012
    Location
    Melbourne Australia
    MS-Off Ver
    Excel 2007
    Posts
    9

    Re: VB Sort with variables in the set range property

    Thank you Anthony.
    If I try
    .SetRange Range(Cells(BrakeSheetPullRow, 2), Cells(BrakeSheetPullRow + 10, uCol))
    I get error Type 11 "mismatch"
    If I try
    .SetRange =Range(Cells(BrakeSheetPullRow, 2), Cells(BrakeSheetPullRow + 10, uCol))
    I get error 438 "Object doesn't support this property or method

    ;(

  5. #5
    Valued Forum Contributor smuzoen's Avatar
    Join Date
    10-28-2011
    Location
    Brisbane, Australia
    MS-Off Ver
    Excel 2003/2007/2010
    Posts
    610

    Re: VB Sort with variables in the set range property

    You have dimensioned BrakeSheetPullRow as a Variant with the code
    Please Login or Register  to view this content.
    It should be dimensioned as Long. I also do not see uCol being set a value and again it should be dimensioned as Long. The type mismatch is due to the setrange requiring numbers not variants/arrays
    Anthony
    “Confidence never comes from having all the answers; it comes from being open to all the questions.”
    PS: Remember to mark your questions as Solved once you are satisfied and rate the answer(s) questions.”
    Last edited by smuzoen; 01-27-2012 at 07:44 AM.

  6. #6
    Registered User
    Join Date
    01-27-2012
    Location
    Melbourne Australia
    MS-Off Ver
    Excel 2007
    Posts
    9

    Re: VB Sort with variables in the set range property

    Anthony, I had uCol dimensioned as Integer and also for BrakeSheetPullRow
    with the line
    .SetRange = Range(Cells(BrakeSheetPullRow, 2), Cells(BrakeSheetPullRow + 10, uCol)).Select
    I still get error code 438 with both set to "Long"

  7. #7
    Registered User
    Join Date
    01-27-2012
    Location
    Melbourne Australia
    MS-Off Ver
    Excel 2007
    Posts
    9

    Re: VB Sort with variables in the set range property

    Anthony, I had uCol dimensioned as Integer and also for BrakeSheetPullRow
    with the line
    .SetRange = Range(Cells(BrakeSheetPullRow, 2), Cells(BrakeSheetPullRow + 10, uCol)).Select
    I still get error code 438 with both set to "Long"

  8. #8
    Valued Forum Contributor smuzoen's Avatar
    Join Date
    10-28-2011
    Location
    Brisbane, Australia
    MS-Off Ver
    Excel 2003/2007/2010
    Posts
    610

    Re: VB Sort with variables in the set range property

    Have you tried - oh and by the way can you have a read of the forum rules - all your code must be included in code tags - have a look at http://www.excelforum.com/misc.php?do=bbcode#code
    Please Login or Register  to view this content.
    If that does not work can you post a sample workbook with no sensitive data and include the code as well - your full code listing for this sort. There is no reason why you cannot use variables to set the range to sort.

    Anthony
    “Confidence never comes from having all the answers; it comes from being open to all the questions.”
    PS: Remember to mark your questions as Solved once you are satisfied and rate the answer(s) questions.”
    Last edited by smuzoen; 01-27-2012 at 09:38 AM.

  9. #9
    Registered User
    Join Date
    01-27-2012
    Location
    Melbourne Australia
    MS-Off Ver
    Excel 2007
    Posts
    9

    Re: VB Sort with variables in the set range property

    Yes Anthony I did try that too and it does not work..(suggestion 1)
    I cannot use Range("B2") because this code is part of a bigger module with a doloop that deals with succesive ranges of dats, so all is variable.

    I might seperate out the core code with some data into another workbook and post that, just so I don't make it too complicated.
    There is nothing too sensitive abourt a listing of all BMW brakes for every model though

  10. #10
    Registered User
    Join Date
    01-27-2012
    Location
    Melbourne Australia
    MS-Off Ver
    Excel 2007
    Posts
    9

    Re: VB Sort with variables in the set range property

    I have truncated my workbook to show the problem.
    You will see that there are two sets of data in the sheet "brakes"
    The code will in its final form sort successive sets of data and hence the need for variables in the sort codeBrakesSortExample.xlsm

  11. #11
    Registered User
    Join Date
    01-27-2012
    Location
    Melbourne Australia
    MS-Off Ver
    Excel 2007
    Posts
    9

    Re: VB Sort with variables in the set range property

    Anthony,
    It look like I have resolved the problem myself.
    The solution was to use relative references in the code by depressing the "Use Relative References" button prior to running the code.
    Thank you for your input I will report any further issues

    So the rule is when using variables in the set range property "use relative references" in the spreadsheet.
    One also need to set this property when opening the macro enables workbook for if you set it later the SetRange property will not work.

    Funny how the select and Key:=Range properties in the same piece of code do not need this setting
    Last edited by Ian Crane; 01-29-2012 at 05:41 AM. Reason: additional information

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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