VBA Excel file attributes

mrsushi

Board Regular
Joined
Nov 18, 2006
Messages
180
Office Version
  1. 2010
Good afternoon,

Is there a way to speed up the below code script? It runs perfectly for what it does, but its terribly slow.

Any advise or an alternative script to that below?

Many thanks
M


Sub GetFileAttributes()
Dim objFSO As Object
Dim objFile As Object
Dim filePath As String
Dim Lastrow As Long
Dim i As Long

Set objFSO = CreateObject("Scripting.FileSystemObject")
Lastrow = Cells(Rows.Count, "A").End(xlUp).Row

For i = 1 To Lastrow
filePath = Cells(i, 1).Value
If objFSO.FileExists(filePath) Then
Set objFile = objFSO.GetFile(filePath)
Cells(i, 2).Value = objFile.DateCreated
'Cells(i, 3).Value = objFile.DateLastModified
Else
Cells(i, 2).Value = "File not found"
'Cells(i, 3).Value = "File not found"
End If
Next i


Set objFSO = Nothing
Set objFile
 
Step #1 - Paste your code into the message post
Step #2 - With the mouse select all the code.
Step #3 - Press the
1709047949416.png
(VBA) button to wrap the code code tags.
Step#4 - Use the "Preview" button to make sure everything looks right before posting.
 
Upvote 0

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
HI, Can you assist with speeding up the below code please?
The code runs really slow. Can this be tweak to speed it up?
Regards
M

VBA Code:
Dim myFolder As String
Dim myFileName As String
Dim myRange As Range
Dim myCell As Range
Dim Lastrow As Long

Lastrow = Range("K" & Rows.Count).End(xlUp).Row
Set myRange = Range("K2:K" & Lastrow)
myFolder = "S:\Other\Datafeeds\Cpens"

For Each myCell In myRange
myFileName = myCell.Value
If Dir(myFolder & "\" & myFileName) = "" Then
myCell.Offset(0, -6).Value = "File Doesn't Exist."
Else
myCell.Offset(0, -6).Value = "File Exists."
End If
Next myCell
 
Upvote 0
Thanks for using code tags. The method to speed this up will be similar to the other block of code:

VBA Code:
Sub FileCheck()
    
    Dim myFolder As String
    Dim myFileName As String
    Dim myRange As Range
    Dim myCell As Range
    Dim Lastrow As Long
    
    Lastrow = Range("K" & Rows.Count).End(xlUp).Row
    Set myRange = Range("K2:K" & Lastrow)
    myFolder = "S:\Other\Datafeeds\Cpens"
    
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
    'Application.EnableEvents = False             <---- use if you have Worksheet_Change event code affected by the cells you are writing to.
    
    For Each myCell In myRange
        myFileName = myCell.Value
        If Dir(myFolder & "\" & myFileName) = "" Then
            myCell.Offset(0, -6).Value = "File Doesn't Exist."
        Else
            myCell.Offset(0, -6).Value = "File Exists."
        End If
    Next myCell
    
    Application.EnableEvents = True
    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = False
End Sub
 
Upvote 0
Solution
Ma
Thanks for using code tags. The method to speed this up will be similar to the other block of code:

VBA Code:
Sub FileCheck()
   
    Dim myFolder As String
    Dim myFileName As String
    Dim myRange As Range
    Dim myCell As Range
    Dim Lastrow As Long
   
    Lastrow = Range("K" & Rows.Count).End(xlUp).Row
    Set myRange = Range("K2:K" & Lastrow)
    myFolder = "S:\Other\Datafeeds\Cpens"
   
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
    'Application.EnableEvents = False             <---- use if you have Worksheet_Change event code affected by the cells you are writing to.
   
    For Each myCell In myRange
        myFileName = myCell.Value
        If Dir(myFolder & "\" & myFileName) = "" Then
            myCell.Offset(0, -6).Value = "File Doesn't Exist."
        Else
            myCell.Offset(0, -6).Value = "File Exists."
        End If
    Next myCell
   
    Application.EnableEvents = True
    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = False
End Sub

Many thanks again for your assistance.
Regards
M
 
Upvote 0

Forum statistics

Threads
1,215,107
Messages
6,123,126
Members
449,097
Latest member
mlckr

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top