I've been searching how to assign a row to a variable and manipulate cells through the variable but I can't seem to find how to do this.
x = Sheet5.Range("A1").EntireRow
MsgBox x(1, 1)
The above code will get me the row into 'x', but is there any way that I can change a cells value using the variable 'x'? x(1,1) = "foo" will not work, and since it's not an object I can't access .Value.
Here's some sample code:
Sub Ranging()
Dim rng As Excel.Range
Dim ws As Excel.Worksheet
Set ws = ActiveSheet
Set rng = ws.Range("A1").EntireRow
With rng
Debug.Print .Cells(1).Value
Debug.Print .Cells(5).Address
.Cells(43).Value = "SurfN'Turf"
End With
End Sub
Debug.Print prints to the VBE's Immediate Window (access with Ctrl-G)
Related
I'm trying to use both worksheets and ranges as variables, but I'm having some problems.
If I declare a worksheet as a variable and then use the range property it works just fine.
However, when I declare a variable Range and try to use it reference it, it throws me the error 438, object doesn't have property or method.
Sub try()
Dim ws As Worksheet
Set ws = Worksheets("Code")
ws.Range("A3", "B6").Value = "sheets"
Dim r As Range
Set r = Range("D1", "F3")
Worksheets("DATOS").r.Value = "ranges"
End Sub
My end goal would be to have both the Worksheet and the range as variables, so I could reference it such as
ws.r.Value = "123"
Thanks in advance, I hope my question isn't too basic and you can help me.
When you set a Range object, it is not a universal cell address to be used like what you did, each Range refers to a specific Worksheet that you can see under its Worksheet property. (documentation)
You did not specify the Worksheet in Set r = Range("D1", "F3") so VBA assumes that you are referring to the ActiveSheet which can be anything. (which is also why you are recommended to always fully qualify your range reference)
As mentioned in your comment - Since your objective is to use the same range for multiple worksheets, you can define the range address in a String variable and use that variable as shown below:
Sub try()
Const r As String = "D1:F3"
Worksheets("DATOS").Range(r).Value = "ranges"
Worksheets("Code").Range(r).Value = "ranges"
End Sub
You can't use range variable in this way. Rather qualify range mentioning sheet name. Try below codes.
Try below codes.
Sub try()
Dim ws As Worksheet
Set ws = Worksheets("Code")
ws.Range("A3", "B6").Value = "sheets"
Dim r As Range
Set r = Worksheets("DATOS").Range("D1", "F3")
r = "ranges"
End Sub
It is not possible to refer to the range in the way you want.
If you want to use a VBA variable to refer to ranges, you can do this:
' Get a range
Set Sht1 = ThisWorkbook.Worksheets("Sheet1")
Set Rng1 = Sht.Range("A2:B2")
' Set the contents of another range to the same value
Set Sht2 = ThisWorkbook.Worksheets("Sheet2")
Set Rng2 = Sht2.Range("C2:D2")
Rng2.value = Rng1.Value
You already seem to have a grasp of doing it this way.
If you want to refer to a range by a name, here is a method that creates a named range:
' Delete the named range if it exists and create it again.
Sub CreateNamedRange(Wbk As Workbook, Txt As String, Rng As Range)
On Error Resume Next
Wbk.Names(Txt).Delete
If Err.Number <> 0 Then Err.Clear
On Error GoTo 0
Wbk.Names.Add Txt, Rng
End Sub
Here we create a named range Name1 and retrieve it using Sht.Range("Name1"):
Sub CreateNamedRangeAndUseIt()
Dim Sht As Worksheet
Dim Rng As Range
' Set the value of the range to 42
Set Sht = ThisWorkbook.Worksheets("Sheet2")
Set Rng = Sht.Range("A2")
Rng.Value = 42
' Create a name for the range
CreateNamedRange ThisWorkbook, "Name1", Rng
' Activate some other sheet to make sure it works when the
' sheet with the named range is not active.
ThisWorkbook.Worksheets("Sheet1").Activate
' Get the named range and output the value of the range to the
' immediate window.
Set Rng = Sht.Range("Name1")
' This would also work, even though the named range does not
' exist on Sheet1:
' Set Rng = ThisWorkbook.Worksheets("Sheet1").Range("Name1")
' Or this (provided you don't have several workbooks open and
' another workbook is selected)
' Set Rng = Range("Name1")
Debug.Print Rng.Value
' Then select the range.
' We must activate the sheet first to select ranges in it.
Rng.Worksheet.Activate
Rng.Select
End Sub
The named range will still exist if you close and reopen the workbook, provided you save the workbook before closing it. So you only need to name the range once.
I am new to VBA, I need help applying the below VBA code to two specific work sheets by Region and by Model. The code simply finds the last column which has the name total for the year and copies the previous months values into a new column. But I get an error object required at ws.
Sub Insert_New_Col()
Dim Found As Range, BeforeR As Long
Dim xSheets As Variant
Dim ws As Worksheet
Dim i As Long
xSheets = Array("By Region", "By Model") '
For i = LBound(xSheets) To UBound(xSheets)
Set ws = xSheets(i)
Set Found = ws.Rows(3).Find(What:="Total for the Year", Lookat:=xlWhole)
BeforeR = R.Column - 1
If Found Is Nothing Then
MsgBox ("The word 'Totals' was not found in Row 5 on Sheet: " & ws.Name)
Else
Columns(BeforeR).Copy
ws.Columns(R.Column).Insert Shift:=xlRight
End If
Next i
End Sub
You want an actual member of the Worksheets collection:
Set ws = ThisWorkbook.Worksheets(xSheets(i))
BeforeR = R.Column - 1
What is R? Add Option Explicit to the top of the module and declare all variables.
Trying to write a Union of Ranges Function
I get `Object variable or with block not set"
I am not getting this right (I think):
With Rng
UnionRange = Intersect(ws.UsedRange, Rng.EntireColumn)
End With
Sub iUnionRange()
Dim R As Range
'Check to see if the Function is working
Set R = UnionRange("Elements", Range("A1:D1, G1:G1, I1:K1"))
R.Select
End Sub
The Function
Function UnionRange(shtName As String, Rng As Range) As Range
Set ws = ThisWorkbook.Sheets(shtName)
If Rng Is Nothing Then Exit Function
With ws.Rng
UnionRange = Intersect(ws.UsedRange, .EntireColumn)
End With
End Function
First of all, use Set keyword to assign an object to a variable, so UnionRange = should be Set UnionRange =. Specify a sheet object when you are retrieving a range, doing so it's not necessary to pass sheet name to the function since Rng.Parent returns the sheet object.
There is example below:
Sub test()
Dim Q As Range
Dim R As Range
Set Q = Sheets("Elements").Range("A1:D1, G1:G1, I1:K1")
Q.Select
Set R = UnionRange(Q)
R.Select
End Sub
Function UnionRange(Rng As Range) As Range
If Rng Is Nothing Then Exit Function
Set UnionRange = Intersect(Rng.Parent.UsedRange, Rng.EntireColumn)
End Function
Your function is returning an object so you need to use 'Set', ie:
Set UnionRange = Intersect(ws.UsedRange, .EntireColumn)
I think your code might also also throw an error if 'R.Select' is called when the "Elements" worksheet isn't active (ie the user has activated another sheet). I wonder too if you're using the Range parameter as simply an address of cells when it can do a lot more for you.
If it were me, I'd change the code to the following:
Sub iUnionRange()
Dim ws As Worksheet
Dim r As Range
' Define the worksheet
Set ws = ThisWorkbook.Worksheets("Elements")
' Call the cell selection function
Set r = UnionRange(ws.Range("A1:D1, G1:G1, I1:K1"))
' Note, if you go to the properties of the "Elements"
' worksheet, you can change its name property to,
' say, ElementsSht and simply refer to the object by that name.
' As well as being easier to code, it does protect you
' from an error if a user changes the sheet name in
' Excel.
' So you could just uncomment the following line:
'Set r = UnionRange(ElementSht.Range("A1:D1, G1:G1, I1:K1"))
' Select the target range
SafeSelect r
End Sub
Function UnionRange(target As Range) As Range
If target Is Nothing Then Exit Function
Set UnionRange = Intersect(target.Worksheet.UsedRange, target.EntireColumn)
End Function
Sub SafeSelect(target As Range)
' Check that the range object is not nothing
' and the worksheet to be selected is active
If Not target Is Nothing Then
target.Worksheet.Activate
target.Select
End If
End Sub
If you're intending to call this routine a lot then perhaps define the UsedRange outside the scope of the function as you only need to process that command once to have the range defined. And finally, be aware that you could have some empty cells selected, especially at the bottom of your used range if some columns are shorter than others.
Good luck with your project.
I have hundreds of Columns in excel that I don't need. I have a range that I want to keep.
At the minute I have
Sub DeleteClms ()
Range("A:G,L:O").Delete
End Sub
Is there anyway to make this an opposite, in other languages I would simply put a =!.
I have tried putting <> in but I dont know where/how to put it into my code?
Thanks
There is no Excel or VBA function for the Symetric Difference of the columns that I know of.
Here is a quick VBA function to get there. Usage would be DeleteAllBut Range("A:C,H:Q")
Sub DeleteAllBut(rngToKeep As Range)
Dim ws As Worksheet
Dim rngToDelete As Range
Dim rngColi As Range
'Number of columns used in worksheet
Set ws = rngToKeep.Parent
iCols = ws.UsedRange.Columns.Count
FirstOne = True
For i = 1 To iCols
Set rngColi = Range("A:A").Offset(0, i - 1)
If Intersect(rngToKeep, rngColi) Is Nothing Then
If FirstOne Then
Set rngToDelete = rngColi
FirstOne = False
Else
Set rngToDelete = Union(rngColi, rngToDelete)
End If
End If
Next i
Debug.Print rngToDelete.Address & " was deleted from " & ws.Name
rngToDelete.Delete
End Sub
I am new in VBA coding. Lets say I am retrieving value from Sheet3.Cell(23, 4) for a value, is there any way in the VBA code which let me set this as a variable?
For example, I have changed the interface and let the value stay at Sheet4.Cell(20,1), everywhere in my code which refer to Sheet3.Cell(23, 4) need to be changed to Sheet4.Cell(20, 1). I am thinking is there any best practice for coding VBA for situation like this?
Yes. For that ensure that you declare the worksheet
For example
Previous Code
Sub Sample()
Dim ws As Worksheet
Set ws = Sheets("Sheet3")
Debug.Print ws.Cells(23, 4).Value
End Sub
New Code
Sub Sample()
Dim ws As Worksheet
Set ws = Sheets("Sheet4")
Debug.Print ws.Cells(23, 4).Value
End Sub
Yes, set the cell as a RANGE object one time and then use that RANGE object in your code:
Sub RangeExample()
Dim MyRNG As Range
Set MyRNG = Sheets("Sheet1").Cells(23, 4)
Debug.Print MyRNG.Value
End Sub
Alternately you can simply store the value of that cell in memory and reference the actual value, if that's all you really need. That variable can be Long or Double or Single if numeric, or String:
Sub ValueExample()
Dim MyVal As String
MyVal = Sheets("Sheet1").Cells(23, 4).Value
Debug.Print MyVal
End Sub