vba - Vlookup Error handling

Mallesh23

Well-known Member
Joined
Feb 4, 2009
Messages
976
Office Version
  1. 2010
Platform
  1. Windows
Hi Team

I want to make my Vlookup dynamic here, if Closed workbooks name has space in it.then

.FormulaR1C1 = "=VLOOKUP(RC[-1],'[" & wbk.Name & "]Sheet1'!R1C1:R10C2,2,0)" 'Run this Code.
'.FormulaR1C1 = "=VLOOKUP(RC[-1],[" & wbk.Name & "]Sheet1!R1C1:R10C2,2,0)" if no space Run this Code.

how to check error and skip it.

Option Explicit
Sub Vlookup_with_space_and_without_Space()
Dim wbk As Workbook
Set wbk = Workbooks.Open(Mac.Range("b5").Value)

With Sheet2.Range("b2:b10")
.FormulaR1C1 = "=VLOOKUP(RC[-1],[" & wbk.Name & "]Sheet1!R1C1:R10C2,2,0)" 'If workbooks dont have space in it.
.FormulaR1C1 = "=VLOOKUP(RC[-1],'[" & wbk.Name & "]Sheet1'!R1C1:R10C2,2,0)" ' If workbooks name Contain space in it.
.Value = .Value
End With

Thanks
mg

End Sub
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Try this:
VBA Code:
With Sheet2.Range("b2:b10")
    If InStr(wBk.Name, " ") = 0 Then
    .FormulaR1C1 = "=VLOOKUP(RC[-1],[" & wBk.Name & "]Sheet1!R1C1:R10C2,2,0)" 'If workbooks dont have space in it.
    Else
    .FormulaR1C1 = "=VLOOKUP(RC[-1],'[" & wBk.Name & "]Sheet1'!R1C1:R10C2,2,0)" ' If workbooks name Contain space in it.
    End If
        .Value = .Value
End With
 
Upvote 0
HI Akuini,

Nice Tricks, it worked Thank you so much.?


Regards,
mg
 
Upvote 0
You're welcome, glad to help, & thanks for the feedback.:)
 
Upvote 0

Forum statistics

Threads
1,215,693
Messages
6,126,246
Members
449,304
Latest member
hagia_sofia

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