+ Reply to Thread
Results 1 to 5 of 5

Moving files using VBA

  1. #1
    Registered User
    Join Date
    10-05-2023
    Location
    Finland
    MS-Off Ver
    365 MSO
    Posts
    3

    Moving files using VBA

    Hi,

    I am trying to automate moving files and I cannot get it to work. I was able to create the new folders automatically but not moving the invoices.

    I have an Excel file where I have sheet called serviceinvoice
    In A column I have user number and in B column I have invoice number

    In cell D1 I have a file path.

    In that folder I have pdf invoices and folders per user

    The name of the pdf invoice starts with the invoice number (12 characters)
    The name of the user folder starts with the user number (4 characters)

    Process that I am looking for is:
    1. check the invoice number from excel sheet
    2. find the pdf file that starts with that number
    3. go and get a user number from column A
    4. find the subfolder where the name starts with this number
    5. move the invoice to this folder
    6. if folder cannot be found, skip to next invoice
    7. do this to all invoices listed in the Excel sheet.

    This is how far I have got, but it doesn't work and I don't understand why. (And ChatGPT is clueless)

    I have checked that the invoices have the same numbers and that the subfolders are there. Please help!

    Sub MoveInvoices()

    Dim ws As Worksheet
    Dim folderPath As String
    Dim invoiceNumber As String
    Dim userNumber As String
    Dim invoiceFileName As String
    Dim userFolder As String

    ' Set worksheet
    Set ws = ThisWorkbook.Sheets("serviceinvoice")

    ' Get folder path from cell D1
    folderPath = ws.Range("D1").Value

    ' Loop through rows
    For i = 3 To ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
    invoiceNumber = Left(ws.Cells(i, 2).Value, 12) ' Use only the first 12 characters
    userNumber = Left(ws.Cells(i, 1).Value, 4) ' Use only the first four characters

    ' Construct file paths
    invoiceFileName = invoiceNumber & "*" ' Assumes invoice files are PDFs
    userFolder = userNumber ' The user folder starts with the user number

    ' Check if invoice file exists
    If Dir(folderPath & "" & invoiceFileName) <> "" Then

    ' Check if user folder exists, if not, skip this invoice
    If Dir(folderPath & "" & userFolder, vbDirectory) <> "" Then

    ' Move invoice to user subfolder
    FileCopy folderPath & "" & invoiceFileName, folderPath & "" & userFolder & "" & invoiceFileName
    Kill folderPath & "" & invoiceFileName ' Remove original file

    End If
    End If

    Next i

    End Sub

  2. #2
    Forum Contributor
    Join Date
    10-02-2012
    Location
    UK
    MS-Off Ver
    Office 365
    Posts
    252

    Re: Moving files using VBA

    You can't put an Asterix in a filename, you also forgot to include a blackslash in your copy command.

    Please Login or Register  to view this content.
    Note: This assumes that the value in D1 ends with a backslash.
    Last edited by PrizeGotti; 10-05-2023 at 07:12 AM.

  3. #3
    Registered User
    Join Date
    10-05-2023
    Location
    Finland
    MS-Off Ver
    365 MSO
    Posts
    3

    Re: Moving files using VBA

    It still doesn't work. I have backslash in the end of the file path. I tried this also:

    Sub MoveInvoices()

    Dim ws As Worksheet
    Dim folderPath, invoiceNumber, userNumber, invoiceFileName, userFolder As String

    ' Set worksheet
    Set ws = ThisWorkbook.Sheets("serviceinvoice")

    ' Get folder path from cell D1
    folderPath = ws.Range("D1").Value

    ' Loop through rows
    For i = 3 To ws.Cells(ws.Rows.Count, "A").End(xlUp).Row

    invoiceNumber = Left(ws.Cells(i, 2).Value, 12) ' Use only the first 12 characters
    userNumber = Left(ws.Cells(i, 1).Value, 4) ' Use only the first four characters

    ' Construct file paths
    invoiceFileName = invoiceNumber & ".pdf" ' Assumes invoice files are PDFs
    userFolder = userNumber ' The user folder starts with the user number

    ' Check if invoice file exists
    If Dir(folderPath & invoiceFileName) <> "" Then

    ' Check if user folder exists, if not, skip this invoice
    If Dir(folderPath & userFolder, vbDirectory) <> "" Then

    ' Move invoice to user subfolder
    FileCopy folderPath & invoiceFileName, folderPath & userFolder & "" & invoiceFileName
    Kill folderPath & invoiceFileName ' Remove original file

    End If
    End If

    Next i

    End Sub

    But that wasn't working either. I have also renamed one of the invoices so that it only has the invoice number as a name and it doesn't get moved. And I tried situation where the subfolder name is only the user number and that doesn't work. And I have tried this with and without having the back slashes between the folderPath and InvoiceFileName. And in different combinations with having and not having the backslash in the file path. Also tried this without the invoiceFileName in the destination path. Could there be an issue about the file type. I have them as Microsoft Edge PDF files?
    Last edited by Mags4; 10-05-2023 at 08:25 AM.

  4. #4
    Forum Contributor
    Join Date
    10-02-2012
    Location
    UK
    MS-Off Ver
    Office 365
    Posts
    252

    Re: Moving files using VBA

    My code works on my test file, so I'm not sure what else to suggest.

    Please Login or Register  to view this content.
    You need to include the backslash as per my code;

    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    10-05-2023
    Location
    Finland
    MS-Off Ver
    365 MSO
    Posts
    3

    Re: Moving files using VBA

    I can't get it to work. I am using google drive desktop folders but I did test on my local folders and it wasn't working there either. I have separate macro to create the user folders and that is working, so the file path must be ok. My invoice pdf names are format XX-2023-0152 firstname surname and my user subfolders are named 1234 firstname surname. And I have tried to copy the file name from the Excel sheet to the file name so that the dashes are definitely the same.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Macro to convert all xls files to xlsx with moving the files into new folders
    By nettadecoco in forum Excel Programming / VBA / Macros
    Replies: 28
    Last Post: 12-29-2023, 02:34 PM
  2. Moving files
    By raurie in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-08-2014, 10:52 AM
  3. Moving Files
    By Matty5894 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 07-31-2014, 05:35 AM
  4. Archiving Files - Creating New Folders and Moving Files
    By mccrimmon in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-17-2011, 06:47 AM
  5. Moving files
    By Picto in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-18-2007, 10:44 AM
  6. [SOLVED] Moving files
    By JT in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-10-2006, 11:10 AM
  7. Moving Files
    By PraxisPete in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-01-2005, 06:10 AM

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