• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Vlookup On Temp Open in Background file & result store in variable

Dear All,

just guideline require

I have a code that temp-open source file in background (Hidden Mode)
& vlookup & display result in active sheet's calling cell that already worked...

if same method use for store this result in variable to use this variable In further process.

"Object Dose Not Support This Property or Method"

error displayed

Code:
Sub Test_of_vlkp_closed_Result_Store_in_variable()
   
    'ONLY FOR VLOOKUP ONCLOSED FILE WHICH TEMP OPEN & CLOSED
                                         
'Declare variables
    Dim sPath As String
    Dim sFile As String
    Dim sSheet As String
    Dim sRef As String
    Dim sFullName As String
    Dim wbSource As Workbook
    Dim wksDest As Worksheet
   
    Dim bWorkbookOpened As Boolean
  Dim myname As String
  Dim fname As String
  Dim fpath As String
       
    'If no worksheet is active, exit sub
    If TypeName(ActiveSheet) <> "Worksheet" Then
        MsgBox "No worksheet is active.!", vbExclamation
        End If
   
    'Assign the active worksheet to a variable
    Set wksDest = ActiveSheet
   
    'Specify path to source file (change accordingly)
    sPath = "C:\BUYER MASTER\"
   
    'Check if path exists
    If Len(Dir(sPath, vbDirectory)) = 0 Then
        MsgBox "Path does not exist.", vbInformation
       
    Else
        'Make sure path ends in back slash
        If Right(sPath, 1) <> "\" Then
            sPath = sPath & "\"
        End If
    End If
   
    'Specify source file (change accordingly)
   
    'BEWARE HERE FOR SHEET NAME
           
    sFile = "SUITING-BUYER MASTER.xlsx"
   
    sSheet = "BUY MASTER"
   
    sRef = "$H$1:$I$500"
   
    'Specify path and source file
    sFullName = sPath & sFile
   
    'Check if workbook exists
    If Len(Dir(sFullName, vbNormal)) = 0 Then
        MsgBox "Workbook does not exist.", vbInformation
       
    End If
   
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    Application.Calculation = xlCalculationManual
       
    'Open specified workbook as read only
    Set wbSource = Workbooks.Open(FileName:=sFullName, ReadOnly:=True)
   
    bWorkbookOpened = True
   
    'Do stuff
            'myname = =vlookup(a2," & wbSource.Worksheets(sSheet).range(sRef).Address(, , , True) & ",{2,3,4},false)"
              'example--Result = Application.WorksheetFunction.Vlookup ( lookup_value, table_array, col_index_num, [range_lookup] )
             
          myname = Application.WorksheetFunction.vlookup(wksDest.range("A2").Value, wbSource(sFile).Worksheets(sSheet).range(sRef).Address, 2, False)
             
    'If source workbook was opened, close without saving
    If bWorkbookOpened Then
        wbSource.Close savechanges:=False
    End If
            'Clear from memory
    Set wbSource = Nothing
   
    Application.ScreenUpdating = True
    Application.EnableEvents = True
    Application.Calculation = xlCalculationAutomatic
   
                                 
            MsgBox myname
           
DisplayError:
                     
End Sub

Help will be appreciated..

Regards,
Chirag Raval
 
Dear All,

Original Working Code is here ...
& buyer master...for where to lookup ...also attached here

change path ...& test your self...working correctly ...

but when same below code use (after require modification)
for 'Result Store In Variable" .

"Object Dose Not Support This Property or Method"


Code:
'Force explicit declaration of variables
Option Explicit
Sub STG_Comp_Vlookup_closed_file()

  'Declare variables
    Dim sPath As String
    Dim sFile As String
    Dim sSheet As String
    Dim sRef As String
    Dim sFullName As String
    Dim wbSource As Workbook
    Dim wksDest As Worksheet
    Dim lastRow As Long
    Dim bWorkbookOpened As Boolean
   
    'FIRST STEP  Inserting a Column at Column B
range("B1").EntireColumn.Insert
'Inserting 2 Columns from C
range("C:D").EntireColumn.Insert
   
    'Turn off screen updating
    Application.ScreenUpdating = False
   
    'Turn on error handling
    On Error GoTo ErrHandler
   
    'If no worksheet is active, exit sub
    If TypeName(ActiveSheet) <> "Worksheet" Then
        MsgBox "No worksheet is active.!", vbExclamation
        GoTo ExitSub
    End If
   
    'Assign the active worksheet to a variable
    Set wksDest = ActiveSheet
   
    'Specify path to source file (change accordingly)
    sPath = "C:\Users\sganuja\Documents\"
   
    'Check if path exists
    If Len(Dir(sPath, vbDirectory)) = 0 Then
        MsgBox "Path does not exist.", vbInformation
        GoTo ExitSub
    Else
        'Make sure path ends in back slash
        If Right(sPath, 1) <> "\" Then
            sPath = sPath & "\"
        End If
    End If
   
    'Specify source file (change accordingly)
   
    'BEWARE HERE SHEET NAME MUST BE MATCH
   
    sFile = "STG BUYERLIST.XLSX"
   
    sSheet = "Sheet1"
   
    sRef = "$A$1:$D$390"
   
    'Specify path and source file
    sFullName = sPath & sFile
   
    'Check if workbook exists
    If Len(Dir(sFullName, vbNormal)) = 0 Then
        MsgBox "Workbook does not exist.", vbInformation
        GoTo ExitSub
    End If
       
    'Open specified workbook as read only
    Set wbSource = Workbooks.Open(FileName:=sFullName, ReadOnly:=True)
   
    bWorkbookOpened = True
   
    'Do stuff
   
 
 
    'Enter lookup formula and convert to values
    With wksDest
        lastRow = .Cells(.Rows.count, "A").End(xlUp).Row
        If lastRow >= 2 Then
            .range("B2:D2").FormulaArray = "=vlookup(a2," & wbSource.Worksheets(sSheet).range(sRef).Address(, , , True) & ",{2,3,4},false)"
            .range("B2:D2").Copy .range("B3:D" & lastRow)
            .Calculate
            With .range("B2:D" & lastRow)
                .Value = .Value
            End With
        Else
            MsgBox "No data is available!", vbExclamation
            GoTo ExitSub
        End If
    End With
   
ExitSub:
    'If source workbook was opened, close without saving
    If bWorkbookOpened Then
        wbSource.Close savechanges:=False
    End If
   
    'Turn screen updating back on
    Application.ScreenUpdating = True
   
    'Clear from memory
    Set wbSource = Nothing
    Set wksDest = Nothing
   
    Exit Sub
   
ErrHandler:
    MsgBox "Error " & Err.Number & ":  " & Err.Description, vbCritical, "Error"
    Resume ExitSub
   
End Sub

hope some can give proper hint.. for complete this task..

Regards,

Chirag Raval
 

Attachments

  • STG BUYERLIST.xlsx
    30.9 KB · Views: 3
Hi ,

The following will work , but will return only one result at a time :

myname = Application.Evaluate("=vlookup(a2," & wbSource.Worksheets(sSheet).Range(sRef).Address(, , , True) & ",2,false)")

Narayan
 
Dear Sir,

in new sheet , on Cell A2 just value is "11345" (Buyer No.)

& Run Below Code with your sugesstions...
but

Run TIme Error : 13 Type Mismatch. ....Error thrown by code on

myname = Application.Evaluate("=vlookup(a2," & wbSource.Worksheets(sSheet).range(sRef).Address(, , , True) & ",2,false)")



Code:
Option Explicit

Sub Test_of_vlkp_closed_Result_Store_in_variable()
   
    'ONLY FOR VLOOKUP ON CLOSED FILE WHICH TEMP OPEN & CLOSED
                                         
'Declare variables
    Dim sPath As String
    Dim sFile As String
    Dim sSheet As String
    Dim sRef As String
    Dim sFullName As String
    Dim wbSource As Workbook
    Dim wksDest As Worksheet
   
    Dim bWorkbookOpened As Boolean
  Dim myname As String
 
       
    'If no worksheet is active, exit sub
    If TypeName(ActiveSheet) <> "Worksheet" Then
        MsgBox "No worksheet is active.!", vbExclamation
        End If
   
    'Assign the active worksheet to a variable
    Set wksDest = ActiveSheet
   
    'Specify path to source file (change accordingly)
    sPath = "C:\BUYER MASTER\"
   
    'Check if path exists
    If Len(Dir(sPath, vbDirectory)) = 0 Then
        MsgBox "Path does not exist.", vbInformation
       
    Else
        'Make sure path ends in back slash
        If Right(sPath, 1) <> "\" Then
            sPath = sPath & "\"
        End If
    End If
   
    'Specify source file (change accordingly)
   
    'BEWARE HERE FOR SHEET NAME
           
    sFile = "SUITING-BUYER MASTER.xlsx"
   
    sSheet = "BUY MASTER"
   
    sRef = "$H$1:$I$500"
   
    'Specify path and source file
    sFullName = sPath & sFile
   
    'Check if workbook exists
    If Len(Dir(sFullName, vbNormal)) = 0 Then
        MsgBox "Workbook does not exist.", vbInformation
       
    End If
   
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    Application.Calculation = xlCalculationManual
       
    'Open specified workbook as read only
    Set wbSource = Workbooks.Open(FileName:=sFullName, ReadOnly:=True)
   
    bWorkbookOpened = True
   
    'Do stuff
            'myname = =vlookup(a2," & wbSource.Worksheets(sSheet).range(sRef).Address(, , , True) & ",{2,3,4},false)"
              'example--Result = Application.WorksheetFunction.Vlookup ( lookup_value, table_array, col_index_num, [range_lookup] )
             
myname = Application.Evaluate("=vlookup(a2," & wbSource.Worksheets(sSheet).range(sRef).Address(, , , True) & ",2,false)")
             
    'If source workbook was opened, close without saving
    If bWorkbookOpened Then
        wbSource.Close savechanges:=False
    End If
            'Clear from memory
    Set wbSource = Nothing
   
    Application.ScreenUpdating = True
    Application.EnableEvents = True
    Application.Calculation = xlCalculationAutomatic
                                     
            Debug.Print myname
           
DisplayError:
                     
End Sub

please help..

Regards,

Chirag Raval
 
Hi ,

You will have to verify that everything matches ; see what is present in the variable sSheet , and whether it matches the worksheet name exactly.

If you can debug the error , in the Immediate window , type in :

?"=vlookup(a2," & wbSource.Worksheets(sSheet).range(sRef).Address(, , , True) & ",2,false)"

and post what is displayed.

Narayan
 
Dear Sir,

Sheet name is ok...Location of file is Ok
I commented
'myname = Application.Evaluate("=vlookup(a2," & wbSource.Worksheets(sSheet).range(sRef).Address(, , , True) & ",2,false)")

& put your debug code

Print "=vlookup(a2," & wbSource.Worksheets(sSheet).range(sRef).Address(, , , True) & ",2,false)"

(?) convert in print word..

"Compile Error"
"Method not valid without suitable object." displayed

Please help ...which type of object required by code in this process?
or what to do to resolve this thread..

Regards,

Chirag Raval
 
Hi ,

When a Run Time Error occurs in VBA , there are two possibilities :

1. The only button displayed is the OK button , and pressing this immediately ends program execution

2. Two buttons , the END button and the DEBUG button , are displayed , and pressing the END button ends program execution , while pressing the DEBUG button allows you to enter commands in the Immediate window while program execution is paused at the error generating statement.

In your case , if it is the latter , then when the error is displayed , click on the DEBUG button , and then do what I posted in my earlier email viz. in the Immediate window , type in :

?"=vlookup(a2," & wbSource.Worksheets(sSheet).range(sRef).Address(, , , True) & ",2,false)"

Excel will display the result of the above ; post it here.

There is no need to comment the error generating line , since if this is done , program execution will halt at some other line due to some other error.

Narayan
 
dear sir,

firstly its halt on

Dim bWorkbookOpened As Boolean 's related checksum bWorkbookOpened=true

I delete this variable & related checksums from code...

re-run code...
code re-halt on
=vlookup(a2," & wbSource.Worksheets(sSheet).range(sRef).Address(, , , True) & ",2,false)"

then I obay as per guideline..
give ?= and halted line....give Enter

"compile error expected expression" displayed.

Regards,
Chirag Raval
 
Dear Sir,

I change Dim myname As string
to Dim myname As Variant
code run.. but result in immediate window
shown error 2042

when re -change as string
RUN TIME ERROR 13:
"type mismatch" displayed

on
myname = Application.Evaluate("=vlookup(a2," & wbSource.Worksheets(sSheet).Range(sRef).Address(, , , True) & ",2,false)")

I attached my buyer master for your reference & where to check my
normal excel file attached with above code

when I copy above line & give ?= and enter

compile error expected expression displayed

Regards,

Chirag Raval
 

Attachments

  • SUITING-BUYER MASTER.xlsx
    42.4 KB · Views: 1
  • TEST OF VLOOKUP CLOSED FILE.xlsm
    17.9 KB · Views: 1
Dear asir,

Run time error 424
Object Required. displayed

?"=vlookup(a2," & wbSource.Worksheets(sSheet).range(sRef).Address(, , , True) & ",2,false)"

and enter

Regards,
Chirag Raval

 
dear Sir,

I also catch wksdest

in with ...end with statement..... I guess may be code want workbook named Buyer Master as an object...

With wksDest
myname = Application.Evaluate("=vlookup(a2," & wbSource.Worksheets(sSheet).Range(sRef).Address(, , , True) & ",2,false)")

End With

each time buyer master open (that already always open each time since this thread start) ...but

run time error 13
type mismatch displayed on

myname = Application.Evaluate("=vlookup(a2," & wbSource.Worksheets(sSheet).range(sRef).Address(, , , True) & ",2,false)")


Regards,

Chirag Raval
 
Dear Sir,

Amazing, Its Work....Many Thanks ...you established rock solid example
for how to retrieve result like from Vlookup -Lookup from closed file through excel VBA & result store in variable...

But I can not know "How Its Work?"
I try to find ..but can not see any changes in my non working code compare with your working code?.

Regards,
Chirag Raval
 
Hi ,

It is the addition of this line of code :

wksDest.Activate

before the VLOOKUP ; the formula uses the reference A2 in its first parameter ; without the above line , the reference is to the active sheet in the workbook SUITING-BUYER MASTER.xlsx.

When ever Excel opens a workbook , it makes that the active workbook , and what ever worksheet is activated at that moment becomes the active worksheet.

In your formula , A2 is supposed to refer to the cell A2 in Sheet1 of the workbook TEST OF VLOOKUP CLOSED FILE.xlsm ; this is precisely the sheet reference in wksDest.

Narayan
 
Dear Sir,

Many Thanks ..for your guideline...
oh we are struggling till now for Just Activation of sheet where this code first try to what for lookup must be activate..

thanks again for thrown light on the situation..

Regards,
Chirag Raval
 
Dear Sir @NARAYANK991 ,

Little help require,

Can you explain "Your post no 3", that how its work..?

because now I am in new code need there are
first 3 character of particular cell as part of save as file name...

I already try with your working code.as above ..but..


Code:
With ActiveSheet
  Dim Season As String
Season = Application.Evaluate("=left(ActiveWorkbook.ActiveSheet.range("L3").Address ",3)")

  End With

Also try..


Code:
Season = Application.Evaluate("=left("ActiveWorkbook.ActiveSheet.range(L3).Address" & ,3)")

Also try..

Code:
Season = Application.Evaluate("=left (L3,3)")


Above first 2 examples raise various errors..

Last example ..save file without any error..but there are variable name "Season" instead of first 3 character of cell "L3"..


But can not achieve first 3 character of that cell in variable & hence can not use that string as pa part of file save name..

Hope Little Help..

Regards,
Chirag Raval
 
Hi ,

What will cell L3 contain ?

What exactly do you want to evaluate ?

The Application.Evaluate function takes as its parameter a worksheet formula ; can you post the worksheet formula which you want to use ? This worksheet formula will be one you enter in a worksheet cell , which will give you the result you want.

Narayan
 
Dear Sir,

2 requirement-2 codes-
(1) for file name contain Party's Name use this code

Code:
Dim s$
    With [Z1]
        .Formula = "=VLOOKUP(A3,'C:\BUYER MASTER\SHIRTING\[SHIRTING BUYER MASTER.xlsx]sheet1'!$F:$G,2,FALSE)"
        s = .Text
        .Value = ""
    End With

' FOR SAVE AS FILE NAME,  USED ABOVE CODE AS BELOW..

fpath = "C:\Users\sganuja\Desktop\ORDERS"
  fName = ActiveSheet.range("A3").Text & " -" & s & " YOUR BOOKED SHIRTING ORDER "

fOR REQ
 
Dear Sir,


I have 2 requirement

  1. File name contain party’s name …that name acquired by below code that working fine
Code:
Dim s$
    With [Z1]
        .Formula = "=VLOOKUP(A3,'C:\BUYER MASTER\SHIRTING\[SHIRTING BUYER MASTER.xlsx]sheet1'!$F:$G,2,FALSE)"
        s = .Text
        .Value = ""
    End With


'That used as below

fpath = "C:\Users\sganuja\Desktop\ORDERS"

  fName = ActiveSheet.range("A3").Text & " -" & s & " YOUR BOOKED SHIRTING ORDER "

ActiveWorkbook.SaveAs fileName:=fpath & "\" & fName & ".xlsx"

above code working fine…

2nd requirement is save as file name also contain active sheet’s cell “L2”, first 3 character

Below is your example code of Evaluate & use that variable in any requirement..

Code:
wksDest.Activate
      myname = Application.Evaluate("=vlookup(a2," & wbSource.Worksheets(sSheet).range(sRef).Address(, , , True) & ",2,false)")

this is just for example.

Obtained from this site

http://chandoo.org/forum/threads/vlookup-on-temp-open-in-background-file-result-store-in-variable.34791/#post-207817


I just want to file save as name contain cell “L3”, s first character in it.


Hope you understand,


Regards,

Chirag Raval
 
Hi ,

I am sorry but I don't understand.

Please understand that too many words can confuse ; use words sparingly but effectively.

If you only want the 3 left-most characters of the contents of cell L3 in the active sheet , then why do you need to use the Evaluate function ? You can simply use the LEFT function , as in :

Dim chars as String
chars = Left(ActiveSheet.Range("L3").Value , 3)

Narayan
 
Dear Sir,

This function's result I want as variable so I can use it
in my file save as name..so only evaluate can do this...

Regards,
Chirag Raval
 
Hi ,

That is exactly what I have posted ; chars is a String variable , and it will contain the 3 left-most characters of what ever text is in cell L3 in the active sheet.

chars can now be used anywhere in your code.

Narayan
 
Dear Sir,

My Mistake I put Char in string "Char" where its already sting

wrong coding
fName = ActiveSheet.range("A3").Text & " -" & S & " YOUR BOOKED SHIRTING" & " " & "chars" & " " & "ORDER "

RIGHT CODING (remove string signs " ")

fName = ActiveSheet.range("A3").Text & " -" & S & " YOUR BOOKED SHIRTING" & " " & chars & " " & "ORDER "

when I remove string character , its working now.

Thanks

Regards,
Chirag Raval
 
Back
Top