Range is set to nothing but cell references within range are valid - excel

I have some VBA in an Excel 2010 document which returns "nothing" when I try to assign a range from a different worksheet. I can reference the value of a cell within the range with no problems.
Why does the code below leave the rng variable set to Nothing, when the value of s3 correctly contains the value of the first cell in that range??
Dim s3 As String
Dim rng As Range
Set SourceFile = Application.Workbooks.Open("c:\finance\inv.xls")
Set SourceSheet = SourceFile.Worksheets(Source)
s3 = SourceSheet.Range("A3").Value
rng = SourceSheet.Range("A3:A30")

Use
Set rng = SourceSheet.Range("A3:A30")
I do not understand why you do not get the error message
Object variable or With block variable not set
Are you using On Error Resume Next to suppress errors?

Related

VBA set range to a cell in a table (error object variable not set)

I have a sheet called DB.
On that sheet I have a table called Details.
In the table there is column called AccountID.
Now I want to set a range to be the first cell on that column.
I am trying:
Dim importRange As Range
Set importRange = DB.ListObjects("Details").ListColumns("AccountID").DataBodyRange.Cells(1, 1)
I get the following error:
Object variable or with block variable not set
Why?
Thanks
If your worksheet is called DB, try the following...
Dim importRange As Range
Set importRange = Worksheets("DB").ListObjects("Details").ListColumns("AccountID").DataBodyRange.Cells(1, 1)
If DB is a variable, you'll need to assign it your worksheet. So, for example, if your worksheet is called Sheet1, try...
Dim DB As Worksheet
Set DB = Worksheets("Sheet1")
Dim importRange As Range
Set importRange = DB.ListObjects("Details").ListColumns("AccountID").DataBodyRange.Cells(1, 1)
Something's not right. You could try this to figure out what.
Dim lo As ListObjects
Dim lc As ListColumns
Dim dbr As Range
Dim importRange As Range
Set lo = DB.ListObjects("Details")
Set lc = lo.ListColumns("AccountID")
Set dbr = lc.DataBodyRange
Set importRange = dbr.Cells(1, 1)
Step through the code with the debugger and check the values of the variable on each line.

How to get range assigned to Named Range in excel vba?

I have already defined the named ranges in my workbook. I want to use the range for placing my pie chart. I am trying to write a code which sets range to variable and move the chart to the specific location.
Dim Rng As Range
Dim ChtObj As ChartObject
Set Rng = ThisWorkbook.Name("BT_GATE1").RefersTo
Set ChtObj = ActiveChart.Parent
ChtObj.Top = Rng.Top
I think I am missing something or using a worng method. Can some one help me assigning a range to variable 'Rng'?
A named range is either one cell or a collection of more cells which have been given a name. Ultimately it is a range. So in your case you can directly set it to a range as shown below
Dim Rng As Range
Set Rng = Range("BT_GATE1")
Debug.Print Rng.Address
Debug.Print Rng.Top
Debug.Print Rng.Parent.Name

how to find a value in a range in worksheet code

I need to find a value that the user inputs in one cell, in a range of values in the same workbook. In the worksheet, the user selects a value and then I need to write a code that finds that value in the range or delivers an error message. I was directed to put this in the worksheet code.
I tried specifying the worksheet of the criteria value but that hasn't seemed to make a difference as I am still getting the same error.
Range("Dcust").Offset(2, 0).End(xlDown).End(xlToRight).ClearContents
Dim shuttleNum As Range
Set shuttleNum = Range("Dsched").Offset(2, 0).End(xlDown).End(xlToRight)
Set driverSheet = ActiveWorkbook.Sheets("Driver")
Dim DSnumView As Integer
DSnumView = driverSheet.Range("DSnumView").Value
'''''Here is where I get the error'''
If shuttleNum.Columns(2).Find(DSnumView, lookat:=xlWhole) Is Nothing Then
MsgBox "You are not scheduled to drive this shuttle", vbCritical, "Error"
Exit Sub
Else
Dim ctable As Range
Set ctable = Range("CTableStart").Offset(1,0).End(xlDown).Range("CTableStart").Offset(0, 3)
End If
This line of code:
Range("Dsched").Offset(2, 0).End(xlDown).End(xlToRight)
returns a single cell.
So basically shuttleNum is set to a single cell and consequently this: shuttleNum.Columns(2) will give you an error because there is only one column in this range.
If I understand correctly what you're trying to do, the folowing should help.
Let's assume you have some data which starts from cell A1 but you don't know where it ends. For example:
To get the range that contains the data, which in this case is A1:C5, you would need to do the following:
Dim sht As Worksheet
Dim rng As Range
Set sht = ThisWorkbook.Worksheets("Worksheet Name")
Set rng = sht.Range("A1")
Set rng = sht.Range(rng, rng.End(xlToRight)) 'A1:C1
Set rng = sht.Range(rng, rng.End(xlDown)) 'A1:C5
Alternatively you can merge the last two steps into one:
Set rng = sht.Range(rng, rng.End(xlToRight).End(xlDown))

Selecting named range from dropdown list in a cell

I have several ranges that may need sorting. I want to use a range name selected from a dropdown list to select and sort that range:
Sub Sorts()
Dim Wb As Workbook
Dim Ws1 As Worksheet
Dim rng As String
Set Wb = ThisWorkbook
Set Ws1 = ThisWorkbook.Sheets("Products")
Set rng = Ws1.Range("G76").Value
Ws1.Range("rng").Sort Range("rng").Cells(1, 1)
End Sub
macro stops at St rng .......object required
Your code is ok, apart for a little detail.
The right line of code should be this one:
Ws1.Range(rng).Sort Range(rng).Cells(1, 1)
This is due to the difference between variables and strings. Your rng is a variable of type string; it means that it's a string, but it contains a specific value that you have "almost" correctly referenced here (you don't need the Set keyword for a string:
rng = Ws1.Range("G76").Value
However, if you pass the variable rng with quotes "", VBA will understand that has to look for a range named exactly rng; which, apparently, doesn't exist so you get an object not-set error.

VBA storing a range in a cell

I have a problem in VBA where I store a cell address as a range contained in a global variable. Problem is, any error and it stops working as it loses the variable. I'm trying to catch as many errors as I can, but I'd prefer to store the range in a cell if possible.
Does anyone have any suggestions?
Thanks
James
I would store the range in a Defined Name:
Assuming the name is called CellAddr and it already exists
Names("CellAddr").RefersTo = "=" & Worksheets("Sheet2").Range("A3:B8").Address(True, True, xlA1, True)
Then you can retrieve the range values or range address using the defined name.
You can also hide the name if you want, and it will persist in a saved workbook
I think something like this is what you are looking for, the Address property of Range returns the string of the Range
Edit: have ammended to show the name of the Worksheet
Public oRng As Range
Sub storeRange()
Dim oWst1, oWst2 As Worksheet
Dim oCell As Range
Set oWst1 = Application.Worksheets("Sheet1")
Set oWst2 = Application.Worksheets("Sheet2")
Set oCell = oWst1.Range("A1")
Set oRng = oWst2.Range("B2")
oCell.Value = oWst2.Name & "!" & oRng.Address
End Sub

Resources