Display 2 ranges in message box

MrFlyer

New Member
Joined
Aug 15, 2018
Messages
7
Hey need assistance with a vba. would like to have a message box show a specific cell and then a range determined by an input box.
what I'd like is display A1 then what is entered by the input box.

I was able to get a message box to show a cell.

Sub message()

Dim q As String, fnd As Range
q = InputBox("Find Person")

If q = "" Then Exit Sub

Set fnd = Range("B:B").Find(q, LookIn:=xlValues, lookat:=xlPart)
If Not fnd Is Nothing Then
Range(fnd, fnd.Offset(, -1)).Select
MsgBox (fnd.Offset(, -1))
End If

End Sub
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
The message box will display the value of the cell (column A) to the left of the cell (column B) where the persons name was found. But both cells will be selected though. Could you explain what is wrong with that? What would you like the code to do?
 
Upvote 0
Here are some solutions:
All solutions will show the name and the offset value first, and then the other way around.
The first 2 will not select.
The last 4 will select.
The last 3 will not select if you out-comment the line rng.Select.

VBA Code:
Option Explicit

' If you don't need to select:
Sub message1()

    Dim q As String, fnd As Range
    q = InputBox("Find Person")
   
    If q = "" Then Exit Sub
   
    Set fnd = Range("B:B").Find(What:=q, LookIn:=xlValues, LookAt:=xlPart)
    If Not fnd Is Nothing Then
        MsgBox q & " " & fnd.Offset(, -1).Value
        ' Switch if you want the name first.
        MsgBox fnd.Offset(, -1).Value & " " & q
    End If

End Sub

' If you don't need to select using with:
Sub message2()

    Dim q As String, fnd As Range
    q = InputBox("Find Person")
   
    If q = "" Then Exit Sub
   
    Set fnd = Range("B:B").Find(What:=q, LookIn:=xlValues, LookAt:=xlPart)
    If Not fnd Is Nothing Then
        With Range(fnd.Offset(, -1), fnd)
            MsgBox q & " " & .Cells(1).Value
            ' Switch if you want the name first.
            MsgBox .Cells(1).Value & " " & q
        End With
    End If

End Sub

' If you need to select:
Sub message3()

    Dim q As String, fnd As Range
    q = InputBox("Find Person")
   
    If q = "" Then Exit Sub
   
    Set fnd = Range("B:B").Find(What:=q, LookIn:=xlValues, LookAt:=xlPart)
    If Not fnd Is Nothing Then
        Range(fnd.Offset(, -1), fnd).Select
        With Selection
            MsgBox q & " " & .Cells(1).Value
            ' Switch if you want the name first.
            MsgBox .Cells(1).Value & " " & q
        End With
    End If

End Sub

' If you wanna use a variable:
Sub message4()

    Dim q As String, fnd As Range
    q = InputBox("Find Person")
   
    If q = "" Then Exit Sub
   
    Set fnd = Range("B:B").Find(What:=q, LookIn:=xlValues, LookAt:=xlPart)
    If Not fnd Is Nothing Then
        Dim rng As Range
        Set rng = Range(fnd.Offset(, -1), fnd)
        rng.Select ' if you need to select
        With rng
            MsgBox q & " " & .Cells(1).Value
            ' Switch if you want the name first.
            MsgBox .Cells(1).Value & " " & q
        End With
    End If

End Sub

' If you wanna use a variable, without 'With':
Sub message5()

    Dim q As String, fnd As Range
    q = InputBox("Find Person")
   
    If q = "" Then Exit Sub
   
    Set fnd = Range("B:B").Find(What:=q, LookIn:=xlValues, LookAt:=xlPart)
    If Not fnd Is Nothing Then
        Dim rng As Range
        Set rng = Range(fnd.Offset(, -1), fnd)
        rng.Select ' if you need to select
         MsgBox rng.Cells(1).Value & " " & q
        ' Switch if you want the name first.
         MsgBox q & " " & rng.Cells(1).Value
    End If

End Sub


' If you wanna use a variable, without `With` and shortened:
Sub message6()

    Dim q As String, fnd As Range
    q = InputBox("Find Person")
   
    If q = "" Then Exit Sub
   
    Set fnd = Range("B:B").Find(What:=q, LookIn:=xlValues, LookAt:=xlPart)
    If Not fnd Is Nothing Then
        Dim rng As Range
        Set rng = Range(fnd.Offset(, -1), fnd)
        rng.Select ' if you need to select
         MsgBox rng(1).Value & " " & q
        ' Switch if you want the name first.
         MsgBox q & " " & rng(1).Value
    End If

End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,390
Messages
6,119,235
Members
448,879
Latest member
VanGirl

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