Why isn't this statement working? Seems simple enough.

pglufkin

Board Regular
Joined
Jun 19, 2005
Messages
127
I have

reg is defined as a range

Set reg = Worksheets("DataSet").Range(Cells(M, 9), Cells(NN, 9))

And it gives me back "Application-defined or object-defined error. The variables M and NN contain correct Values.

??? Weird, this should work in my mind.
 
Last edited:

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
I have

reg is defined as a range

Set reg = Worksheets("DataSet").Range(Cells(M, 9), Cells(NN, 9))

And it gives me back "Application-defined or object-defined error. The variables M and NN contain correct Values.

??? Weird, this should work in my mind.
Hi,

Can you share more of the code you have?
How are M and NN defined and what values do they hold when you get the error?
 
Upvote 0
I have

reg is defined as a range

Set reg = Worksheets("DataSet").Range(Cells(M, 9), Cells(NN, 9))

And it gives me back "Application-defined or object-defined error. The variables M and NN contain correct Values.

??? Weird, this should work in my mind.
The two Cells calls are pointing to the ActiveSheet, not the DataSet worksheet. Try it this way...
Code:
With Worksheets("DataSet")
  Set reg = .Range(.Cells(M, 9), .Cells(NN, 9))
End With
or, alternately, Dim a variable (I'll name it WS for this example) as as Worksheet data type to hold the reference to the worksheet and then do this...
Code:
Set WS = Worksheets("DataSet")
Set reg = WS.Range(WS.Cells(M, 9), WS.Cells(NN, 9))
 
Last edited:
Upvote 0
Still cannot get it to work even with implementing the above . . . . reg will not take on the range parameters.
 
Upvote 0
More of my code.

(code)
Declarations before any subs
Dim reg As Range
Dim tar As Range
Dim WS As Worksheet
Dim DataVar As Range



Sub rangeset()
Dim N As Long, NN As Long
Dim M As Long, col As Long
Dim target As Range, source As Range
Dim s As String
Application.Calculation = xlCalculationManual
Set tar = ActiveCell
col = 9
N = 30
M = Range("DataVar").Row + 1
For NN = M To N
s = Worksheets("DataSet").Cells(NN, col).Value
If Asc(Right(s, 1)) = 63 Then
Set WS = Worksheets("DataSet")
Set reg = WS.Range(WS.Cells(M, 9), WS.Cells(NN, 9))
Exit For
End If
Next NN
If IsNumeric(Left(ActiveCell.Text, 1)) = True Then
ActiveCell.Offset(1, 0).Select
End If
Set target = ActiveCell
Set source = reg
ConcatAndColorParts target, reg
End Sub
(\code)
 
Upvote 0
However source is fine, I fixed it . . . for some reason reg is not getting the range, but source is . . . weird.
 
Upvote 0
If M is greater than N your code will fail as you skip the For/Next loop then try to set a Range equivalent to an object that doesn't exist (reg is only set inside the loop). If you really need to loop between 2 variables of unknown sizes there are some methods you can try:


Code:
For NN =WorksheetFunction.Min(M, N) to WorksheetFunction.Max(M, N)

or

Code:
If M > N Then x = -1 Else x = 1
For NN = M To N Step x
 
Upvote 0

Forum statistics

Threads
1,214,959
Messages
6,122,476
Members
449,087
Latest member
RExcelSearch

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top