+ Reply to Thread
Results 1 to 5 of 5

VBA VLookup Problem: Run-Time error '1004'

  1. #1
    hurlbut777
    Guest

    VBA VLookup Problem: Run-Time error '1004'

    Pasted below is code that when ran is resulting in a "Run-Time error '1004'
    Unable to get the VLookup property of the WorksheetFunction Class." The
    problem is in the string of code where I am trying to apply a password to a
    file based on a result from a vlookup. Can someone help me get this
    corrected, I believe I am very close...thanks.


    Sub Protect_File_Labor()

    Dim sCurrFName As String
    Dim X As Integer
    Dim Y As Range
    Set Y = Range("d17")


    With Application
    .ScreenUpdating = False
    .DisplayAlerts = False
    sCurrFName = Dir(Y.Value & "*.xls")
    Do While sCurrFName <> ""
    Workbooks.Open (Y.Value & sCurrFName)
    Workbooks(sCurrFName).SaveAs (Y.Value & sCurrFName),
    Password:=WorksheetFunction.VLookup(sCurrFName, Range("k1:l3"), 2, 0)
    Workbooks(sCurrFName).Close
    sCurrFName = Dir
    Loop
    .ScreenUpdating = True
    .DisplayAlerts = True
    End With

    End Sub


  2. #2
    ben
    Guest

    RE: VBA VLookup Problem: Run-Time error '1004'

    HURLBUT
    Workbooks(sCurrFName).SaveAs (Y.Value & sCurrFName),
    Password:=application.WorksheetFunction.VLookup(sCurrFName, Range("k1:l3"),
    2, 0)


    "hurlbut777" wrote:

    > Pasted below is code that when ran is resulting in a "Run-Time error '1004'
    > Unable to get the VLookup property of the WorksheetFunction Class." The
    > problem is in the string of code where I am trying to apply a password to a
    > file based on a result from a vlookup. Can someone help me get this
    > corrected, I believe I am very close...thanks.
    >
    >
    > Sub Protect_File_Labor()
    >
    > Dim sCurrFName As String
    > Dim X As Integer
    > Dim Y As Range
    > Set Y = Range("d17")
    >
    >
    > With Application
    > .ScreenUpdating = False
    > .DisplayAlerts = False
    > sCurrFName = Dir(Y.Value & "*.xls")
    > Do While sCurrFName <> ""
    > Workbooks.Open (Y.Value & sCurrFName)
    > Workbooks(sCurrFName).SaveAs (Y.Value & sCurrFName),
    > Password:=WorksheetFunction.VLookup(sCurrFName, Range("k1:l3"), 2, 0)
    > Workbooks(sCurrFName).Close
    > sCurrFName = Dir
    > Loop
    > .ScreenUpdating = True
    > .DisplayAlerts = True
    > End With
    >
    > End Sub
    >


  3. #3
    hurlbut777
    Guest

    RE: VBA VLookup Problem: Run-Time error '1004'

    I appreciate the effort, but that didn't work...any other suggestions?

    "ben" wrote:

    > HURLBUT
    > Workbooks(sCurrFName).SaveAs (Y.Value & sCurrFName),
    > Password:=application.WorksheetFunction.VLookup(sCurrFName, Range("k1:l3"),
    > 2, 0)
    >
    >
    > "hurlbut777" wrote:
    >
    > > Pasted below is code that when ran is resulting in a "Run-Time error '1004'
    > > Unable to get the VLookup property of the WorksheetFunction Class." The
    > > problem is in the string of code where I am trying to apply a password to a
    > > file based on a result from a vlookup. Can someone help me get this
    > > corrected, I believe I am very close...thanks.
    > >
    > >
    > > Sub Protect_File_Labor()
    > >
    > > Dim sCurrFName As String
    > > Dim X As Integer
    > > Dim Y As Range
    > > Set Y = Range("d17")
    > >
    > >
    > > With Application
    > > .ScreenUpdating = False
    > > .DisplayAlerts = False
    > > sCurrFName = Dir(Y.Value & "*.xls")
    > > Do While sCurrFName <> ""
    > > Workbooks.Open (Y.Value & sCurrFName)
    > > Workbooks(sCurrFName).SaveAs (Y.Value & sCurrFName),
    > > Password:=WorksheetFunction.VLookup(sCurrFName, Range("k1:l3"), 2, 0)
    > > Workbooks(sCurrFName).Close
    > > sCurrFName = Dir
    > > Loop
    > > .ScreenUpdating = True
    > > .DisplayAlerts = True
    > > End With
    > >
    > > End Sub
    > >


  4. #4
    ben
    Guest

    RE: VBA VLookup Problem: Run-Time error '1004'

    hmm maybe try to put that worksheetfuntion or the formula equivalent into a
    cell, pull the value from that cell and then delete the formula?

    "hurlbut777" wrote:

    > I appreciate the effort, but that didn't work...any other suggestions?
    >
    > "ben" wrote:
    >
    > > HURLBUT
    > > Workbooks(sCurrFName).SaveAs (Y.Value & sCurrFName),
    > > Password:=application.WorksheetFunction.VLookup(sCurrFName, Range("k1:l3"),
    > > 2, 0)
    > >
    > >
    > > "hurlbut777" wrote:
    > >
    > > > Pasted below is code that when ran is resulting in a "Run-Time error '1004'
    > > > Unable to get the VLookup property of the WorksheetFunction Class." The
    > > > problem is in the string of code where I am trying to apply a password to a
    > > > file based on a result from a vlookup. Can someone help me get this
    > > > corrected, I believe I am very close...thanks.
    > > >
    > > >
    > > > Sub Protect_File_Labor()
    > > >
    > > > Dim sCurrFName As String
    > > > Dim X As Integer
    > > > Dim Y As Range
    > > > Set Y = Range("d17")
    > > >
    > > >
    > > > With Application
    > > > .ScreenUpdating = False
    > > > .DisplayAlerts = False
    > > > sCurrFName = Dir(Y.Value & "*.xls")
    > > > Do While sCurrFName <> ""
    > > > Workbooks.Open (Y.Value & sCurrFName)
    > > > Workbooks(sCurrFName).SaveAs (Y.Value & sCurrFName),
    > > > Password:=WorksheetFunction.VLookup(sCurrFName, Range("k1:l3"), 2, 0)
    > > > Workbooks(sCurrFName).Close
    > > > sCurrFName = Dir
    > > > Loop
    > > > .ScreenUpdating = True
    > > > .DisplayAlerts = True
    > > > End With
    > > >
    > > > End Sub
    > > >


  5. #5
    Dave Peterson
    Guest

    Re: VBA VLookup Problem: Run-Time error '1004'

    That means that you're not finding a match.

    You can either check for the run time error:

    dim pwd as string
    on error resume next
    pwd = WorksheetFunction.VLookup(sCurrFName, Range("k1:l3"), 2, 0)
    if err.number <> 0 then
    'not found
    else
    'found it
    end if

    or you can drop the worksheetfunction and use application--and check for an
    error:

    dim pwd as variant
    pwd = application.VLookup(sCurrFName, Range("k1:l3"), 2, 0)
    if iserror(pwd) then
    'not found
    else
    'found
    end if

    I think the application.vlookup() is easier to read later.

    hurlbut777 wrote:
    >
    > Pasted below is code that when ran is resulting in a "Run-Time error '1004'
    > Unable to get the VLookup property of the WorksheetFunction Class." The
    > problem is in the string of code where I am trying to apply a password to a
    > file based on a result from a vlookup. Can someone help me get this
    > corrected, I believe I am very close...thanks.
    >
    > Sub Protect_File_Labor()
    >
    > Dim sCurrFName As String
    > Dim X As Integer
    > Dim Y As Range
    > Set Y = Range("d17")
    >
    > With Application
    > .ScreenUpdating = False
    > .DisplayAlerts = False
    > sCurrFName = Dir(Y.Value & "*.xls")
    > Do While sCurrFName <> ""
    > Workbooks.Open (Y.Value & sCurrFName)
    > Workbooks(sCurrFName).SaveAs (Y.Value & sCurrFName),
    > Password:=WorksheetFunction.VLookup(sCurrFName, Range("k1:l3"), 2, 0)
    > Workbooks(sCurrFName).Close
    > sCurrFName = Dir
    > Loop
    > .ScreenUpdating = True
    > .DisplayAlerts = True
    > End With
    >
    > End Sub


    --

    Dave Peterson

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1