VBA vlookup reference in different sheet

后端 未结 4 1757
悲哀的现实
悲哀的现实 2020-12-17 06:29

In Excel 2007, I am looping through the values of column 4 in Sheet 2. Still in Sheet 2, I want to output the result of my vlookup formula into column 5. The vlookup formula

4条回答
  •  醉酒成梦
    2020-12-17 06:37

    It's been many functions, macros and objects since I posted this question. The way I handled it, which is mentioned in one of the answers here, is by creating a string function that handles the errors that get generate by the vlookup function, and returns either nothing or the vlookup result if any.

    Function fsVlookup(ByVal pSearch As Range, ByVal pMatrix As Range, ByVal pMatColNum As Integer) As String
        Dim s As String
        On Error Resume Next
        s = Application.WorksheetFunction.VLookup(pSearch, pMatrix, pMatColNum, False)
        If IsError(s) Then
            fsVlookup = ""
        Else
            fsVlookup = s
        End If
    End Function
    

    One could argue about the position of the error handling or by shortening this code, but it works in all cases for me, and as they say, "if it ain't broke, don't try and fix it".

提交回复
热议问题