+ Reply to Thread
Results 1 to 4 of 4

Need Help to Open File from assigned Folder and Check one condition , Delete and Save.

Hybrid View

  1. #1
    Valued Forum Contributor Naveed Raza's Avatar
    Join Date
    11-04-2012
    Location
    India, Hyderabad
    MS-Off Ver
    Excel, Access 2007/2010
    Posts
    1,338

    Need Help to Open File from assigned Folder and Check one condition , Delete and Save.

    Hi Everyone,

    I need small help in my written code and i know this will resolved here.

    I written a macro in File Checking and deleting workbook and rest all file will be there in assigned folder.

    I have some csv files in folder like (Johnxxx, Jessxxx, John25x) and it may be increase or decrease.

    what exactly i want here is , macro should open file and check if Range("A3") Empty then close file and delete file from the folder, and if Range("A3") contain some data then close the file and delete "xxx" from file name (means if in workbook "Johnxxx" range(A3) contain some data then "Johnxxx" workbook should close and delete "xxx" from file name "Johnxxx" to "John".

    I hope my explanation is understandable.

    Thanks in Advance.
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor Naveed Raza's Avatar
    Join Date
    11-04-2012
    Location
    India, Hyderabad
    MS-Off Ver
    Excel, Access 2007/2010
    Posts
    1,338

    Re: Need Help to Open File from assigned Folder and Check one condition , Delete and Save.

    Hi Everyone,

    I used Kill for file deleting, i just need code to remove "xxx" from file names.

    any help please !

    Thanks in advance.
    Last edited by Naveed Raza; 01-05-2013 at 09:35 AM.

  3. #3
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: Need Help to Open File from assigned Folder and Check one condition , Delete and Save.

    Try something like this...

    Sub checking()
        
        Dim wsHome As Worksheet
        Dim FilePath As String, FileName As String
        Dim NewFileName As String, FileExt As String
        Dim mrow As Long
        
        Set wsHome = ActiveWorkbook.Worksheets("Home")
        
        FilePath = wsHome.Range("C4").Value
        If Right(FilePath, 1) <> "\" Then FilePath = FilePath & "\"
        mrow = 5
        
        If Len(Dir$(FilePath, vbDirectory)) = 0 Then
            MsgBox FilePath, vbExclamation, "Cannot Locate Folder"
            Exit Sub
        Else
            Application.ScreenUpdating = False
            Do Until IsEmpty(wsHome.Range("F" & mrow))
                FileName = wsHome.Range("E" & mrow).Value
                If Len(Dir$(FilePath & FileName)) = 0 Then
                    wsHome.Range("G" & mrow).Value = "File Not Found" 'File didn't exist
                Else
                    Application.StatusBar = "Opening file: " & FilePath & FileName
                    With Workbooks.Open(FileName:=FilePath & FileName)
                        If .ActiveSheet.Range("A3").Value = "" Then
                            .Close SaveChanges:=False
                            Kill FilePath & FileName    'Delete File
                            wsHome.Range("G" & mrow).Value = "File Deleted"
                        Else
                            .Close SaveChanges:=False
                            wsHome.Range("G" & mrow).Value = "File Saved"
                            FileExt = Mid(FileName, InStrRev(FileName, "."))
                            NewFileName = Left(FileName, Len(FileName) - Len(FileExt))
                            Do While Right(NewFileName, 1) = "x"
                                NewFileName = Left(NewFileName, Len(NewFileName) - 1)
                            Loop
                            'Rename File
                            Name FilePath & FileName As FilePath & NewFileName & FileExt
                            wsHome.Range("H" & mrow).Value = NewFileName & FileExt
                        End If
                    End With
                End If
                mrow = mrow + 1
            Loop
            Application.ScreenUpdating = True
        End If
        Application.StatusBar = "Done"
        MsgBox "Assigned Work Successfully Done", vbInformation
        
    End Sub
    Last edited by AlphaFrog; 01-05-2013 at 11:02 AM.

  4. #4
    Valued Forum Contributor Naveed Raza's Avatar
    Join Date
    11-04-2012
    Location
    India, Hyderabad
    MS-Off Ver
    Excel, Access 2007/2010
    Posts
    1,338

    Re: Need Help to Open File from assigned Folder and Check one condition , Delete and Save.

    Thank you so much sir its work perfectly.

    Thank you once again

+ 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