I'm working on optimizing some code I wrote and I have a question about storing a value in a variable vs looking it up multiple times. Just thinking about it, it seems that looking it up once and storing it in a variable is more efficient, but I guess I don't really know.
The code below is how I started, the 2nd code block is what I revised the sub to. The first has one less line of code, but the second seems like it would be more efficient since it's only doing one lookup.
I'd like opinions on which is better and why.
Thanks!
The code below is how I started, the 2nd code block is what I revised the sub to. The first has one less line of code, but the second seems like it would be more efficient since it's only doing one lookup.
I'd like opinions on which is better and why.
Thanks!
Code:
Sub ProcessFiles()
Set objFolder = objFSO.GetFolder("D:\Downloads")
For Each objFile In objFolder.Files
'Launch SQL process to import data in file
'SQL launch code goes here
strNewName = fldProcessed & "\" & Left(objFile.Name,Len(objFile.Name)-4) & ".old"
If FileExists(strNewName) Then
DeleteFile(strNewName)
End If
objFile.Move strNewName
Next
End Sub
Code:
Sub ProcessFiles()
Set objFolder = objFSO.GetFolder("D:\Downloads")
For Each objFile In objFolder.Files
strFileName = objFile.Name
'Launch SQL process to import data in file
'SQL launch code goes here
strNewName = fldProcessed & "\" & Left(strFileName,Len(strFileName)-4) & ".old"
If FileExists(strNewName) Then
DeleteFile(strNewName)
End If
objFile.Move strNewName
Next
End Sub