Value in cell as variable in select formula - vba

I have programmed the following VBA code:
Sub test()
Dim Variable As Variant
Variable = "R2:R11"
Sheet1.Select
Range(Variable).Select
End Sub
This formula works perfectly. Now I want to replace the "R2:R11" part of the variable and instead referring to a cell (W12) in the spreadsheet. In this cell I have written "R2:R11" and I changed the formula to the following:
Variable = Sheet1.Range("W12")
which leads to the following code:
Sub test()
Dim Variable As Variant
Variable = Sheet1.Range("W12")
Sheet1.Select
Range(Variable).Select
End Sub
However, with this formula I now get the ERROR 1004.
Do you guys have any solution how I can make the variable referring to the cell in the spreadsheet and then use it in the select formula?
Thanks for any help :-)

You need to declare your variables properly and access the actual .Value property of the Range object.
Sub test()
Dim Variable As String
Variable = Sheet1.Range("W12").Value
Sheet1.Select
Range(Variable).Select
End Sub
Which can also be written as:
Sub test()
Sheet1.Activate
Range([W12]).Activate
End Sub
A note on the .Value property - if you omit this, the value will usually be assigned anyway because it's the default property of the range object. That being said there are scenarios where this won't be the case and therefore it's always best practice to explicitly state that you want the value.

you most probably typed "R2:R11" in W12 cell, i.e with double quotes too
in that cell you only have to type R1:R12
moreover you can simply code
Sub test()
Sheet1.Range(Sheet1.Range("W12")).Select
End Sub

Related

Excel VBA .Find Function changing values in selection

Heya this is probably simple but I cannot figure out what is wrong.
I am trying to do .find for a specific date and change that selection to a user input date.
I have a userform to select a date from a combobox (date1_cbo). The combobox source is linked to dates on a worksheet (Backend). There is a textbox below for writing the new date to change it to (date1_txt). I keep getting an error
object variable or with block variable not set.
I have tried a few options without any luck here is my code:
Dim selection As Range
Dim check As Boolean
'input box validation
check = IsDate(date1_txt.Value)
If check = True Then
'find cell matching combobox
With Worksheets("Backend").Range("A1:A500")
Set selection = .Find(date1_cbo.Value) 'this is the problem
selection.Value = date1_txt.Value
End With
Else
End If
Interestingly .Find returns the range or Nothing. however because the combobox is linked to the cells I am searching through this should never return nothing... I dont understand why the error is occurring.
a variable named as 'selection' is bad coding practice but totally legal. Don't use such names for the sake of clarity.
Error 91 is caused when you are trying to read a property( .value) from null object. your selection variable is null cause date formats on the sheet and combobox are different.
Just convert to date before attempting to find it in sheet.
Set selection = .Find(CDate(date1_cbo.Value)) '/ once again, selection is valid but bad name for variable.
You are using a variable named Selection. VBA uses it as well. Rename your variable to anything else, rewrite your code and it should work. Even Selection1 is quite ok:
Public Sub TestMe()
Dim selection1 As Range
With Worksheets(1).Range("A1:A500")
Set selection1 = .Find("vi")
End With
If Not selection Is Nothing Then
selection1.Value = "some other value"
End If
End Sub
To change multiple values with Find() as here - A1:A10, then some possibility is to do it like this:
Public Sub TestMe()
Dim myRng As Range
With Worksheets(1).Range("A1:A500")
.Range("A1:A10") = "vi"
Set myRng = .Find("vi")
If Not myRng Is Nothing Then
Do Until myRng Is Nothing
myRng.Value = "New value"
Set myRng = .Find("vi")
Loop
End If
End With
End Sub
It is a bit slow, as far as it loops every time and it can be improved, if the range is united and replaced at once.

Call Subroutine from a string value VBA

trying to call this subroutine using a string. I have tried Application.Run like I have read online but that doesn't seem to be working.
The variable element will loop through and represents different state codes. So I have subs called "CA_Config", "GA_Config" "AZ_Config" etc.
Dim strSubToCall As String
strSubToCall = element & "_Config()"
Application.Run strSubToCall
State subs are very different therefore need to be different subroutines. The other subs and the main sub calling the other ones are all public.
Example for CA sub below
Public Sub CA_Config()
Dim rngLastHeader As Range
Dim intLastRow As Integer
Dim i As Integer
intLastRow = Sheet1.currWS.UsedRange.Rows.Count
Set rngLastHeader = Sheet1.currWS.Range("A1").End(xlToRight)
rngLastHeader.Offset(, 1).Value = "Use Tax Reversal Needed"
Sheet1.currWS.Range("X:X").EntireColumn.Copy
Sheet1.currWS.Range("Y:Y").PasteSpecial xlPasteFormats
Sheet1.currWS.Range("Y:Y").Columns.AutoFit
End Sub
Remove parenthesis and prepend your Sub name with module name. For instance, Application.Run "Module1.MySub".

How to extended the range variable with CurrentRegion property (vba)?

The range variable extends with the following code:
Option Explicit
Function ESTENDISELEZIONE(Area As Range) As Range
Set ESTENDISELEZIONE = Range(Area, Area.End(xlDown))
End Function
But it doesn't extends with:
Option Explicit
Function ESTENDISELEZIONE(Area As Range) As Range
Set ESTENDISELEZIONE = Range(Area, Area.CurrentRegion)
End Function
And with:
Option Explicit
Function ESTENDISELEZIONE(Area As Range) As Range
Set ESTENDISELEZIONE = Area.CurrentRegion
End Function
The procedure extends the range!
Sub EXTENDSELECTION()
Selection.CurrentRegion.Select
End Sub
Can someone help me?
I've added an example to the question.
In the cell:
=CONTA.SE(ESTENDISELEZIONE(B2);"Orange")
In the formula:
Option Explicit
Function ESTENDISELEZIONE(Area As Range) As
Range
MsgBox(Area.Address)
Set ESTENDISELEZIONE = Area.CurrentRegion
MsgBox(ESTENDISELEZIONE.Address)
End Function
The CurrentRegion property of a Range object won't work in a UDF.
And it doesn't make much sense to try, as it will cause untold problems. Imagine that you changed cell C6 in your spreadsheet - the cell containing =CONTA.SE(ESTENDISELEZIONE(B2);"Orange") will not be recalculated (because it isn't dependent on cell C6) which means you start getting inconsistent data in your worksheet.
Note: Your code works fine if just being used as a normal Function. It is only when used as a UDF that it will have problems.

Find Range Name Using Active Cell

I'm a beginner when it comes to programming in VBA.
I have one cell that is part of a named range. Using that active cell, I want to be able to find what range that cell is a part of and pass it into a VBA function or subroutine as a Range object.
Can anyone provide me with guidance as to how to proceed, or is this not possible?
Thanks in advance!
Here is a simple example.
The code checks if the selected cell is part of a named range. If so, the named range is passed to a function:
Sub Main()
Dim nm As Integer
For nm = 1 To ActiveWorkbook.Names.Count
If Not Intersect(Selection, Range(ActiveWorkbook.Names(nm).Name)) Is Nothing Then
Debug.Print MyFunc(Range(ActiveWorkbook.Names(nm).Name)) // Prints TRUE or FALSE
End If
Next nm
End Sub
Function MyFunc(Named_Range As Range) As Boolean
MyFunc = Named_Range.Cells.Count > 2 ~~>Courtesy of `Thomas Inzina`
End Function

Inserting Formula in Formula Bar

Anyone knows what's the problem with my code?
Sub reFormat()
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Admin")
ws.Range("C21").Formula = "=""S4&""AA5&""AA6&""AA7&""AA8&""AA9&""AA10" 'returns applica-
tion defined or object-defined error
End Sub
And I want the output of this code to be: =S4&(AA5&AA6&AA7&AA8&AA9&AA10)
Thanks for the help!
Your formula is fundamentally invalid. The string evaluates to this:
="S4&"AA5&"AA6&"AA7&"AA8&"AA9&"AA10
Which isn't a valid Excel formula.
You have too many quotes. If these are cell references and your formula intends to concatenate them, and you want your string to evaluate to this:
=S4&(AA5&AA6&AA7&AA8&AA9&AA10)
Then you could just do this:
.Formula = "=S4&(AA5&AA6&AA7&AA8&AA9&AA10)"