Getting Data from Excel to Word Userform

rohanmalhotra

New Member
Joined
May 4, 2010
Messages
29
Hi all

i am preparing a template in word userform. In the template, i want the senders name to come up automatically, therefore, i have created an excel sheet which has the list of staff members with their employee id's (as they login to the system with there employee id's and that will be picked up). i have written a code but it is giving me run time error 13.
HTML:
Sub GetExcelData()
 
    Dim objExcel As Excel.Application
    Dim wbk As Excel.Workbook
    Dim ws As Excel.Worksheet
    Dim myRange As Range
    Dim UserName As String
    Dim rs As Variant
 
    Set objExcel = CreateObject("Excel.Application")
    Set wbk = objExcel.Workbooks.Open("M:\Phones\Management Information\Projects\Staff.xls")
    Set ws = wbk.Worksheets("Staff")
 
    UserName = Me.txtID.Value
 
    myRange = ws.Range("A1:B500")
     rs = wbk.Application.WorksheetFunction.VLookup("UserName", "myRange", 2, False)
 
End Sub

Can anyone of you please help.

Many Thanks
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Try this:
Code:
Sub GetExcelData()
 
    Dim objExcel As Excel.Application
    Dim wbk As Excel.Workbook
    Dim ws As Excel.Worksheet
    Dim myRange As Range
    Dim UserName As String
    Dim rs As Variant
 
    Set objExcel = CreateObject("Excel.Application")
    Set wbk = objExcel.Workbooks.Open("M:\Phones\Management Information\Projects\Staff.xls")
    Set ws = wbk.Worksheets("Staff")
 
    UserName = Me.txtID.Value
 
    Set myRange = ws.Range("A1:B500")
    rs = wbk.Application.WorksheetFunction.VLookup(UserName, myRange, 2, False)
 
End Sub

When describing a problem it is helpful to state the number and text of the error you see as well as where in your code the error is occurring.
 
Last edited:
Upvote 0
Hi

i am getting error on the following line :

rs = wbk.Application.WorksheetFunction.VLookup(UserName, myRange, 2, False)


its Runtime error '13'. Type mismatched.

Thanks
 
Last edited:
Upvote 0
This code is located in a MSWord userform code page.

When I changed the dimension statement for myRange to object, the code worked.
Code:
    Dim myRange As Object

If that does not work for you, put a STOP line immediately before the line that errors out and tell us the values of all variables in the code. VBE | View | Locals opens the 'Locals' pane which displays current info on code execution.
 
Upvote 0

Forum statistics

Threads
1,224,503
Messages
6,179,136
Members
452,890
Latest member
Nikhil Ramesh

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