VBA: How to display a cell value by its defined name? - vba

I have already a defined name for a particular column in my worksheet.
How to display a cell value by its defined name?
I've tried these:
Public Sub Test()
Dim R As Range
Set R = ThisWorkbook.Names("SomeName").RefersToRange
MsgBox CStr(R.Value)
End Sub
but run-time error occured "Type Mismatch" (error code: 13).
What's wrong with the code?
What is the actual data type of RefersToRange.Value?
The documentation says that RefersToRange returns the Range object, but it seems differ with the Range object returned by ActiveCell, because I've no problem with the following code:
MsgBox CStr(ActiveCell.Value)
I've been using Excel 2003

RefersToRange does return a Range object. I assume you're getting your Type Mismatch on the Cstr line. If the range has multiple cells, the Value property returns a Variant array. Do this in the Immediate Window (Control+G in the VBE).
?typename(thisworkbook.Names("SomeRange").RefersTorange)
Range
?typename(thisworkbook.Names("SomeRange").RefersToRange.Value)
Variant()
The CStr function can't handle an array argument and that's why you're getting that error. You can get to a particular cell in the range, like the first cell, like this
ThisWorkbook.Names("SomeRange").RefersToRange(1).Value
Or loop through them as Fink suggests.

You Need to iterate through the cells within that range and get the value of each cell separately if the range spans multiple cells. Otherwise it throws an error.
Sub Test()
Dim r As Range
Dim cell As Range
Set r = Range("Something")
If r.Cells.Count > 1 Then
For Each cell In r.Cells
MsgBox cell.Value
Next cell
Else
MsgBox r.Value
End If
End Sub
However, you can set the value to all of the cells defined in the range by setting the value of a multi-cell range like this:
Sub Test()
Dim r As Range
Set r = Range("Something")
r.Value = "Test"
End Sub

You should be able to just supply the name to the Range (or even the Cells) property:
Set R = ThisWorkbook.Range("SomeName")

You can get cell value by its name as follows:-
Workbook.Range("SomeName").Value

Related

Using Find method to search for a string from another worksheet

Perhaps I just haven't used the right search terms, I am still new to VBA, but I just can't find the solution to my problem:
I am trying to find a value (format 'yyyy-ww') from one worksheet in the row of another worksheet, and then select the cell (the next step would then be to then select and copy the respective column, and then paste the values).
I have the following code:
Private Sub Button5_Click()
'Define previous week and the search range
Dim prevwk As Object
Dim SrchRng As Range
Set prevwk = ActiveWorkbook.Worksheets("Values").Range("B1")
Set SrchRng = ActiveWorkbook.Worksheets("DE").Rows(1)
'If previous week is found, select the cell
With SrchRng
Dim prevwkf As Range
Set prevwkf = SrchRng.Find(What:=prevwk)
prevwkf.Select '<----- Error is here
End Sub
I keep receiving the error message:
'Run-time error '91': Object variable or With block variable not set'.
I have tried many changes but it keeps coming down to this error message.
Many thanks for your help!
before selecting a cell you have to activate the sheet, just as you would do manually:
SrchRng.parent.Activate
prevwkf.Select
BTW you don't need that With SrchRng, and you could check for actual match found
Private Sub Button5_Click()
'Define previous week and the search range
Dim prevwk As Object
Dim SrchRng As Range
Set prevwk = ActiveWorkbook.Worksheets("Values").Range("B1")
Set SrchRng = ActiveWorkbook.Worksheets("DE").Rows(1)
'If previous week is found, select the cell
Dim prevwkf As Range
Set prevwkf = SrchRng.Find(What:=prevwk)
If Not prevwkf Is Nothing Then ' check you actually found something
SrchRng.Parent.Activate
prevwkf.Select '<----- Error is here
End If
End Sub

VBA Excel select a range of cells within a named range

I have a Named Range col_9395 it is an entire column. I want to set a range within this Named range. I want the range to start at row 3 to row 200 of same column. Whats the best way to do this?
Original working line without Named Range:
Set rngBlnk = Sheet108.Range("T3:T200").SpecialCells(xlCellTypeBlanks)
This is the code I tried with no luck:
Set rngBlnk = Range("col_9395)(3,1):Range("col_9395)(200,1).SpecialCells (xlCellTypeBlanks)
Might be wrong, but I find this the easiest one:
Private Sub Test()
Dim rngBlnk As Range
Set rngBlnk = Range("col_9395").Rows("3:200").SpecialCells(xlCellTypeBlanks)
End Sub
You can see the sort of logic with
Option Explicit
Sub test()
Dim colToUse As Long
colToUse = ThisWorkbook.Worksheets("Sheet1").Range("ol_9395").Column
With ThisWorkbook.Worksheets("Sheet1")
Debug.Print .Range(.Cells(3, colToUse), .Cells(200, colToUse)).Address
End With
End Sub
Sub t()
Dim rng As Range
Set rng = Range("col_9395") ' for easier use
Dim blnkRng As Range
Set blnkRng = Range(Cells(rng.Rows(3).Row, rng.Column), Cells(rng.Rows(200).Row, rng.Column)).SpecialCells(xlCellTypeBlanks)
blnkRng.Select
End Sub
What I did was assign your named range to a variable (just for easier referencing). Then using the Range() property, I used the 3rd and 200th row of your named range to set the range to look for blank cells.
The idea is that this will help you in the event your named range isn't simply an entire column. It will get the relative 3rd and 200th row from your named range.
Option Explicit
Public Sub TestMe()
Dim rngBlnk As Range
Dim firstCell As Range
Dim lastCell As Range
Set firstCell = [col_9395].Cells(3, 1)
Set lastCell = [col_9395].Cells(200, 1)
If WorksheetFunction.CountBlank(Range(firstCell, lastCell)) > 0 Then
Set rngBlnk = Range(firstCell, lastCell).SpecialCells(xlCellTypeBlanks)
End If
End Sub
A kind of a problem with SpecialCells and assigning to them is that if there are no cells from the specific type, it throws an error.
Thus, there is a check with WorksheetFunction.CountBlank()>0, before the setting of rngBlnk to the special cells.
I'm late to the party, I know, but maybe this will help someone. I've stumbled on a technique that I don't see presented as an option in any of the handful of "range-in-a-range" questions here.
I discovered you can ask VBA for a range of a range directly. I have formatted some data as a Table, but it could be simply a Named Range, or even an unnamed range I suppose. My code that is working looks like this:
With Workbooks(Filename)
.Worksheets(tabName).Activate
.Worksheets(tabName).Range("SummaryBand").Range("B2:R2").Copy
End With
My Table is named SummaryBand, which due to a previous step was not necessarily always in the same absolute position on the spreadsheet, but I wanted an absolute Range within SummaryBand. In this example, "B2:R2" is the absolute position within the Table, with the top left cell of the Table being A1.

return absolute reference number

I am using follow code to find maximum value in a column. I need to know absolute reference number of where that value is found. I am hoping to use that absolute reference number in FOR loop and check what is in adjacent cells of where that value is found.
rng = Application.WorksheetFunction.Max(Columns("H"))
I have tried using match but I am getting error 2042.
adrs = Application.Match(rng, Range("H:H"), 0)
Is there a way to find out where that maximum value resides?
Please note I am searching for time stamps so wrapping rng in CLNG is not an option.
Or simply this?
Dim rng As Range
Set rng = Columns("H").Find(Application.Max(Columns("H")))
If Not rng Is Nothing Then
MsgBox rng.Address(0, 0)
End If
Try this:
Dim rng As Range
Dim maxValue As Integer
'you set your area that you get mxaimum value from
Set rng = Range("H:H")
'determine the maximum value
maxValue = Application.WorksheetFunction.Max(rng)
'select cell which contains found value
'(Find returns Range objects, so you can use it as you like)
rang.Find(maxValue).Select
You need to make sure that you are using the correct references.
For example, if the code is running off Sheet1 and the data is in Sheet2 then it is possible you will get errors. Trying this as an example I got the Error 2042 for the adrs variable when the code and the data were on different WorkSheets.
Also, always remember to Option Explicit this will force you to delcare your variables.
Try the following:
Option Explicit
Sub FindMaxValueAndReturnRowNum()
Dim SomeWorkSheet As Worksheet
'Here you can change the Sheet1 to that of you data sheet name
Set SomeWorkSheet = ThisWorkbook.Sheets("Sheet1")
Dim MaxNum As Long
Dim adrs As Long
'Notice the reference to where the code needs to evaluate
'if there is no reference to a specific sheet then it will use the active sheet.
MaxNum = Application.WorksheetFunction.Max(SomeWorkSheet.Columns("H"))
'adrs will return the row number
adrs = Application.Match(MaxNum , SomeWorkSheet.Range("H:H"), 0)
End Sub

Function to check if a range is contained in another

I am looking to create a function that checks if a cell (Range1) is contained within Range2. The function would be:
Function IsWithin(Range1 as Range, Range2 as Range) as Boolean
This is meant to go in a Before_DoubleClick event to check that the cell clicked belongs to a range.
Examples of expected input/output (using addresses directly only to make it easier to imagine):
IsWithin("A2", "A1:B3") = True
IsWithin("B1","B1:B2") = True
IsWithin("A3", "A4:C10") = False
IsWithin("A3", "A3") = True
Off the top of my head I can think of a simple way to do this:
Function IsWithin(Range1 as Range, Range2 as Range) as Boolean
Dim cell2 as range
For each cell2 in Range2
If cell2.address = Range1.Address then
IsWithin = True
Exit Function
End if
Next
End function
Now for the harder part, and the question. If I am selecting a Merged cell that protrudes inside Range2, I'd like it to count as being part of the range (even if some of the merged cell sticks out). What would I need to write to get that done?
Example considering A1:B3 is a merged cell (Still sending addresses instead of range objects as a way to represent it easier):
IsWithin("A1:B3", "A2:D7") = True
Is there a reason why you're not using Intersect()?
Dim r As Range
Set r = Intersect(Range("A2"), Range("A1:B3"))
If r Is Nothing Then
Debug.Print "Not in range"
ElseIf r.Address = Range("A2").Address Then
Debug.Print "Completely within range"
Else
Debug.Print "Partially within range"
End If
Edit:
As #Bacon mentioned in the comments, this doesn't work with merged cells. But you can use the MergeArea property to test for that. Assuming A1:B1 is a merged range, this should work:
Set r = Intersect(Range("A1").MergeArea, Range("B1:B3"))
If r Is Nothing Then
Debug.Print "Not in range"
Else
Debug.Print "Ranges intersect"
End If
MergeArea returns the merged range, if it's part of a merged area. If not, it just returns the single cell. So you should be safe always using MergeArea for the source when you test the intersection, as shown above in the edit.

Does there exist a VBA command which does not change the formula of a cell, but its value?

Suppose in a worksheet the formula of R4 cell is =B1+B2, and its current value is 10.
A VBA command Range("R4").Value = 5 will change both its formula and its value to 5.
Does anyone know if there exists a VBA command which changes the value of R4 to 5, but does not change its formula, such that its formula is still =B1+B2?
PS: we can also achieve the same state in another way: 1) do a Range("R4").Value = 5 2) change the formula of R4 to =B1+B2 but without evaluating it. In this case, does there exist a VBA command which change the formula of a cell without evaluating it?
Edit: What I want to do is...
I would like to write a function, which takes a worksheet where some cells may be out of date (the formula does not match its value), and generates automatically a VBA Sub, this VBA Sub can reproduce this worksheet. The VBA Sub may look like:
Sub Initiate()
Cells(2,3).Value = 5
Cells(4,5).Value = 10
...
Cells(2,3).Formula = "=2+3"
Cells(4,5).Formula = "=C2+C2"
...
End Sub
Such that running Initiate() builds one worksheet with same values and formulas.
Without the VBA command I am asking, this Initiate() will be hard to generated.
You cannot change the value of a cell to something different than what the cell formula computes to.
Regarding your p.s.: You can probably change the formula of a cell without re-evaluation by changing the calculation mode to manual. But that would of course apply to the entire workbook, not just this one cell
EDIT: maybe a solution would be to temporarily save the formula of the cell in either a tag of that cell, or a hidden worksheet?
It is quite simple to change the result of a formula without changing the formula itself:
Change the value of of its argument(s). This is a Solver-type approach:
Sub ForceDesiredResult()
Dim r As Range
Set r = Range("B2")
With r
If r.HasFormula Then
.Formula = .Formula & "-5"
Else
.Value = .Value - 5
End If
End With
End Sub
Here is some very dirty code that will save all values of all formulas on the active sheet as custom properties of the sheet, and a 2nd sub that will mark red all cells where the value has changed from it's original value, while preserving all formulas. It will need some error-checking routines (property already exists, property doesn't exist,...) but should give you something to work with. Since I don't really understand your problem it's a bit hard to say ;)
Sub AddCustomProperty()
Dim mysheet As Worksheet
Dim mycell2 As Range
Dim myProperty As CustomProperty
Set mysheet = ActiveWorkbook.ActiveSheet
For Each objcell In mysheet.UsedRange.Cells
Debug.Print objcell.Address
If objcell.HasFormula Then Set myProperty = mysheet.CustomProperties.Add(objcell.Address, objcell.Value)
Next objcell
End Sub
Sub CompareTags()
Dim mysheet As Worksheet
Dim mycell2 As Range
Dim myProperty As CustomProperty
Set mysheet = ActiveWorkbook.ActiveSheet
For Each objcell In mysheet.UsedRange.Cells
Debug.Print objcell.Address
If objcell.HasFormula Then
On Error Resume Next
If mysheet.CustomProperties(objcell.Address).Value <> objcell.Value Then
objcell.Font.ColorIndex = 3
On Error GoTo 0
End If
End If
Next objcell
End Sub