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 10:44 AM.
You need to add code tags as per forum rules.
Try
Anthony.SetRange Range(Cells(BrakeSheetPullRow, 2), Cells(BrakeSheetPullRow + 10, uCol))
“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.”
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
;(
You have dimensioned BrakeSheetPullRow as a Variant with the code
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/arraysDim BrakeSheetPullRow 'definition
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 06:44 AM.
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"
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"
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
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..SetRange Range(Cells(BrakeSheetPullRow, 2), Cells(BrakeSheetPullRow + 10, uCol)) 'or you could try .SetRange Range("B2").Resize(10, ucol - 2)
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 08:38 AM.
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
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
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 04:41 AM. Reason: additional information
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks