use a cell value as a range in vba

bpoese

New Member
Joined
Oct 2, 2006
Messages
18
This may be a silly question, but I'm stumped and can't find an answer while searching.
Maybe I'm going about it the wrong way, so feel free to propose a different solution.

I have this formula cell u22

=VLOOKUP(T22,LineErrors,2,FALSE)


I am now trying to reference the result of that formula as a range in vba in this snippet.
Code:
        If u22 <> "okay" Then
            strPart = MsgBox(msg1 & [parterror].Value & msg2, vbExclamation, "Required Information Missing")
            [u22.value].select
            Exit Sub
        End If


Any help you can give is greatly appreciated... =)
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
I have this formula cell u22

=VLOOKUP(T22,LineErrors,2,FALSE)

I am now trying to reference the result of that formula as a range in vba in this snippet.
Code:
If u22 <> "okay" Then
You cannot simply use a cell name in VBA because VBA needs to know which workbook, which worksheet, etc. Instead, you need to reference cells in VBA code by specifying the complete "path" to the cell. Here is one set of code that should work:

Code:
Sub Test()
With Workbooks(ThisWorkbook.Name).Worksheets("Template")
        If .Range("B1").Value = "okay" Then
              MsgBox ("hello")
        Else
              MsgBox ("goodby")
        End If
    End With
End Sub
Substitute the name of the worksheet that contains the code for the worksheet name "Template" in the above code
 
Upvote 0
I knew that didn't work but I was substituting actual cell values instead of the range names I was using for clarity.

Either way, alphafrog's basic idea worked... here's my code now as it properly functions the way I wanted as copied directly from vba.

Code:
        If [parterror] <> "okay" Then
            Dim errorlocation As Range
            Set errorlocation = Range(Range("errorlocation").Value)
            strPart = MsgBox(msg1 & [parterror] & msg2, vbExclamation, "Required Information Missing")
            errorlocation.Select
            Exit Sub
        End If
 
Upvote 0

Forum statistics

Threads
1,214,583
Messages
6,120,383
Members
448,956
Latest member
JPav

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top