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.
Related
I'm trying to write a code that makes a graph from different parts of the data I have, the issue is that I need a for loop to go through all my lines of data and when the row changes also the range for the next graph
Basically I need the code to check when i is 1 to graph using the range A2:A12 from a different sheet (The table below exists on Sheet1, while the range is from Sheet2)
Cycle
Starting range
Ending range
1
A2
A12
2
A22
A32
The issue I'm facing is that when I try to define Range("Variable 1:Variable 2") the code doesn't seem to work.
I'm sure I'm thinking about this wrong but I haven't found any solutions online.
Never use Range or Cells (as well as Columns, Rows) without specifying in which sheet you expect them to be. Otherwise your code depends on which workbook/worksheet is active the moment it runs and your code is not reliable.
Use something like
ThisWorkbook.Worksheets("Sheet2").Range(…)
or better define a variable so you don't have to repeate it:
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Sheet2")
'and then always use
ws.Range(…)
To use variable ranges you either need to concatenate your variables to a valid address you can use:
ws.Range(Variable1 & ":" & Variable2)
or simly use
ws.Range(Variable1, Variable2)
where for example Variable1 = "A2" and Variable2 = "A12"
Reference Ranges Using a List of Cell Addresses
Option Explicit
Sub ReferenceRanges()
Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
Dim sws As Worksheet: Set sws = wb.Worksheets("Sheet1")
Dim dws As Worksheet: Set dws = wb.Worksheets("Sheet2")
Dim drg As Range
Dim r As Long
For r = 2 To sws.Cells(sws.Rows.Count, "B").End(xlUp).Row
' Note the importance of using '.Value' (try without it).
Set drg = dws.Range(sws.Cells(r, "B").Value, sws.Cells(r, "C").Value)
' Continue using 'drg', e.g.:
Debug.Print drg.Address(0, 0)
Next r
End Sub
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
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))
I have two separate excel workbooks. One is an order form, the other is a master inventory file.
I have a column where I input the order amount for each individual items (let's say it spans from cell C2:C130, each row is a different item). My optimal solution is to develop a macro that takes that order and adds it to an existing master inventory to keep track of total orders.
I wrote some code that I thought would work:
Private Sub CommandButton1_Click()
Dim wsCopy As Worksheet
Dim wsDest As Worksheet
Dim lCopyLastRow As Variant
Dim lDestLastRow As Variant
Workbooks.Open "C:\Users\Dave\Desktop\Designs Work\Master_Inventory.xlsm"
Set wsCopy = Workbooks("Production file_Dave Edits").Worksheets("Order")
Set wsDest = Workbooks("Master_Inventory").Worksheets("Sheet1")
Set lCopyLastRow = wsCopy.Range("E2:E130")
Set lDestLastRow = wsDest.Range("E2:E130")
lDestLastRow.Value = lDestLastRow.Value + lCopyLastRow.Value
End Sub
when I run this code, I get a mismatch error (type 13?).
So I went into the watch window to see the type of each expression and the combined right side of the equation is a "variant/integer" type, whereas each individual expression is a "variant/variant" type. Moreover, when I run the code and call only one cell instead of a matrix, the code works; it doesn't run when multiple cells are called.
Can anyone help? Or have a more elegant code? Thank you
Set lCopyLastRow = wsCopy.Range("E2:E130")
Set lDestLastRow = wsDest.Range("E2:E130")
This makes both variables Variant/Range, because the Set keyword says the right-hand side of the assignment operator is yielding an object reference: the two variables might as well be declared As Range.
Now, the Value of a Range object that refers to multiple cells, is a 2D Variant array.
lDestLastRow.Value = lDestLastRow.Value + lCopyLastRow.Value
That's where you're getting the type mismatch error, because you can't use the + operator with array operands.
when I run the code and call only one cell instead of a matrix, the code works
That's because a single-cell Range.Value returns that numeric value, and + will work with that - although, if the cell contains an error value (e.g. #REF! or #VALUE!), you'll still get a type mismatch error, because a Variant/Error can't be an operand.
I wish I could help beyond that, but I have no idea what this + intends to be doing.
As for a more elegant solution, I'd recommend indenting the procedure body, moving declarations closer to their assignment, and keeping a reference to the destination "inventory" workbook:
Private Sub CommandButton1_Click()
Dim sourceSheet As Worksheet
Set sourceSheet = Workbooks("Production file_Dave Edits").Worksheets("Order")
Dim inventoryBook As Workbook
Set inventoryBook = Workbooks.Open("C:\Users\Dave\Desktop\Designs Work\Master_Inventory.xlsm")
Dim destSheet As Worksheet
Set destSheet = inventoryBook.Worksheets("Sheet1")
Dim sourceRange As Range
Set sourceRange = sourceSheet.Range("E2:E130")
Dim destRange As Range
Set destRange = destSheet.Range("E2:E130")
'todo: figure out intent
'lDestLastRow.Value = lDestLastRow.Value + lCopyLastRow.Value
End Sub
If that + intends to add everything up in both ranges, you could use Application.WorksheetFunction.Sum(sourceRange) + Application.WorksheetFunction.Sum(destRange), although I doubt that's really want you're looking for.
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?