[COLOR=green]'Force explicit declaration of variables[/COLOR]
[COLOR=darkblue]Option[/COLOR] [COLOR=darkblue]Explicit[/COLOR]
[COLOR=darkblue]Sub[/COLOR] copytestofvlclosedfile()
[COLOR=green]'Declare variables[/COLOR]
[COLOR=darkblue]Dim[/COLOR] sPath [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR]
[COLOR=darkblue]Dim[/COLOR] sFile [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR]
[COLOR=darkblue]Dim[/COLOR] sSheet [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR]
[COLOR=darkblue]Dim[/COLOR] sRef [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR]
[COLOR=darkblue]Dim[/COLOR] sFullName [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR]
[COLOR=darkblue]Dim[/COLOR] wbSource [COLOR=darkblue]As[/COLOR] Workbook
[COLOR=darkblue]Dim[/COLOR] wksDest [COLOR=darkblue]As[/COLOR] Worksheet
[COLOR=darkblue]Dim[/COLOR] LastRow [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR]
[COLOR=darkblue]Dim[/COLOR] bWorkbookOpened [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Boolean[/COLOR]
[COLOR=green]'Turn off screen updating[/COLOR]
Application.ScreenUpdating = [COLOR=darkblue]False[/COLOR]
[COLOR=green]'Turn on error handling[/COLOR]
[COLOR=darkblue]On[/COLOR] [COLOR=darkblue]Error[/COLOR] [COLOR=darkblue]GoTo[/COLOR] ErrHandler
[COLOR=green]'If no worksheet is active, exit sub[/COLOR]
[COLOR=darkblue]If[/COLOR] TypeName(ActiveSheet) <> "Worksheet" [COLOR=darkblue]Then[/COLOR]
MsgBox "No worksheet is active.!", vbExclamation
[COLOR=darkblue]GoTo[/COLOR] ExitSub
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
[COLOR=green]'Assign the active worksheet to a variable[/COLOR]
[COLOR=darkblue]Set[/COLOR] wksDest = ActiveSheet
[COLOR=green]'Specify path to source file (change accordingly)[/COLOR]
sPath = "C:\Users\sganuja\Desktop\"
[COLOR=green]'Check if path exists[/COLOR]
[COLOR=darkblue]If[/COLOR] Len(Dir(sPath, vbDirectory)) = 0 [COLOR=darkblue]Then[/COLOR]
MsgBox "Path does not exist.", vbInformation
[COLOR=darkblue]GoTo[/COLOR] ExitSub
[COLOR=darkblue]Else[/COLOR]
[COLOR=green]'Make sure path ends in back slash[/COLOR]
[COLOR=darkblue]If[/COLOR] Right(sPath, 1) <> "\" [COLOR=darkblue]Then[/COLOR]
sPath = sPath & "\"
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
[COLOR=green]'Specify source file (change accordingly)[/COLOR]
sFile = "BUYERLIST.xlsx"
sSheet = "Sheet1"
sRef = "$A$1:$E$383"
[COLOR=green]'Specify path and source file[/COLOR]
sFullName = sPath & sFile
[COLOR=green]'Check if workbook exists[/COLOR]
[COLOR=darkblue]If[/COLOR] Len(Dir(sFullName, vbNormal)) = 0 [COLOR=darkblue]Then[/COLOR]
MsgBox "Workbook does not exist.", vbInformation
[COLOR=darkblue]GoTo[/COLOR] ExitSub
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
[COLOR=green]'Open specified workbook as read only[/COLOR]
[COLOR=darkblue]Set[/COLOR] wbSource = Workbooks.Open(Filename:=sFullName, ReadOnly:=True)
bWorkbookOpened = [COLOR=darkblue]True[/COLOR]
[COLOR=green]'Do stuff[/COLOR]
'Enter lookup formula and convert to values
[COLOR=darkblue]With[/COLOR] wksDest
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
[COLOR=darkblue]If[/COLOR] LastRow >= 2 [COLOR=darkblue]Then[/COLOR]
.Range("B2:D2").FormulaArray = "=vlookup(a2," & wbSource.Worksheets(sSheet).Range(sRef).Address(, , , [COLOR=darkblue]True[/COLOR]) & ",{2,3,4},false)"
.Range("B2:D2").Copy .Range("B3:D" & LastRow)
.Calculate
[COLOR=darkblue]With[/COLOR] .Range("B2:D" & LastRow)
.Value = .Value
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
[COLOR=darkblue]Else[/COLOR]
MsgBox "No data is available!", vbExclamation
[COLOR=darkblue]GoTo[/COLOR] ExitSub
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
ExitSub:
[COLOR=green]'If source workbook was opened, close without saving[/COLOR]
[COLOR=darkblue]If[/COLOR] bWorkbookOpened [COLOR=darkblue]Then[/COLOR]
wbSource.Close SaveChanges:=[COLOR=darkblue]False[/COLOR]
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
[COLOR=green]'Turn screen updating back on[/COLOR]
Application.ScreenUpdating = True
[COLOR=green]'Clear from memory[/COLOR]
[COLOR=darkblue]Set[/COLOR] wbSource = [COLOR=darkblue]Nothing[/COLOR]
[COLOR=darkblue]Set[/COLOR] wbDest = [COLOR=darkblue]Nothing[/COLOR]
[COLOR=darkblue]Exit[/COLOR] [COLOR=darkblue]Sub[/COLOR]
ErrHandler:
MsgBox "Error " & Err.Number & ": " & Err.Description, vbCritical, "Error"
[COLOR=darkblue]Resume[/COLOR] ExitSub
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]