VBA VLook Up Type Mismatch

The_Gigster

New Member
Joined
Nov 4, 2016
Messages
5
Hi

I have a range on a worksheet that I wish to restrict to a number of users based on their application name but, based on the following if the User Name is not in the range, a type mismatch is returned:

Private Sub Workbook_Open()




Dim strUserName As String


strUserName = Application.UserName
Range("AZ1") = strUserName


If strUserName = (Application.VLookup(strUserName, Sheet1.Range("$BA$1:$BA$9"), 1, False)) Then
Columns("G:L").Select
Selection.EntireColumn.Hidden = False
Range("A1").Select
Else
Range("A1").Select

End If


End Sub

Any help will be greatly appreciated. Thanks in advance.
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Hello

Did you put strUserName in cel AZ1 on the correct sheet ?

Since you use cell AZ1 to store the username, why not have the VLOOKUP formula in say cell AZ2 ?
Then VBA-code is easier and can just read the result of the formula.

Code:
Private Sub Workbook_Open()

    Dim strUserName           As String


    strUserName = Application.UserName
    Sheet1.Range("AZ1").Value = strUserName


    If Sheet1.Range("AZ2").Value = strUserName Then
        Columns("G:L").Hidden = False
    End If
    
    Range("A1").Select


End Sub
 
Upvote 0
Hello

Thank you for taking the time to respond. I understand what you've done there - but when the user name is not in the range given, how do I create the "else" without an Type Mismatch 13 error.

The Gigster
 
Upvote 0
This will unhide columns G to L if the username is found in BA1:BA9 on Sheet1.
Code:
Private Sub Workbook_Open()
Dim strUserName As String
Dim Res As Variant

    strUserName = Application.UserName
    
    Range("AZ1") = strUserName
    
    Res = Application.Match(strUserName, Sheet1.Range("BA1:BA9"), 0)
    
    If Not IsError(Res) Then
        Columns("G:L").Hidden = False
    End If
    
    Range("A1").Select

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,945
Messages
6,122,393
Members
449,081
Latest member
JAMES KECULAH

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