Results 1 to 6 of 6

looping code

Threaded View

Mayaleigh Need help looping vba 01-09-2009, 10:20 AM
TyeJae If you could please post your... 01-09-2009, 10:33 AM
Mayaleigh DATA SHEETS folder as... 01-09-2009, 10:56 AM
Mayaleigh Books1 is a modified version... 01-09-2009, 10:58 AM
TyeJae Here is where I am at so far.... 01-09-2009, 12:32 PM
Mayaleigh Thanks so much, I'm trying to... 01-09-2009, 03:12 PM
  1. #1
    Registered User
    Join Date
    01-09-2009
    Location
    Raleigh, NC
    MS-Off Ver
    Excel 2003
    Posts
    4

    Need help looping vba

    Hi. I am new at all this, and have had tons of help with the below macro, but now we're needing someone to maybe review it and see where we've gone wrong. I am fully lost at this point, so any help would be greatly appreciated.

    The left table is partial from the workbook that this macro will be saved to.
    The right table is in another workbook called directory.xls (used for reference by the macro).

    I’ve entered the variables used in the code to the tables for reference.

    This Sheet
    B.............D
    pnum........qty

    Directory.xls
    A..........B........C
    dnum.....dfol.....file


    What we’re trying to do with the macro is :-

    1. look at “pnum”, if this cell is blank then the macro ends.

    2. If “pnum” contains an entry then it looks to “qty”, if this is blank or 0 then it looks at pnum in the next row.

    3. When it finds “qty” greater than 0 it needs to search “dnum” in the directory to find an exact match for “pnum” exiting if no match is found. (thinking message and back to next row in #2 instead).

    4. It then needs to take the text of “dfol” in the same row and pass that to the next part of the code as the source directory for the file copy, with “file” being the filename to copy.

    5. After this is done it then needs to return to the top of the loop and look at the next entry in #2

    Sub CopyFile()
    Dim fso
    Dim file As String, sfol As String, dfol As String
    Dim pnum As String, qty As Integer, dnum As String
    Dim x As Integer, y As Integer
    x = 2
    qty = ActiveSheet.Range("D" & x).Value
    pnum = ActiveSheet.Range("B" & x).Text
    
    If pnum = "" Then MsgBox "No more files to copy"
    Exit Sub
    If qty > 0 Then GoTo Line1 Else x = x + 1
    Line1:
    y = 1
    dnum = Range("T:\DRAFTING RESOURCES\DATA SHEETS\[directory.xls]Sheet1!A" & y).Text
    If dnum = "" Then MsgBox pnum & "Not found in directory": Exit Sub Else If dnum = pnum Then GoTo Line2 Else y = y + 1
    Line2:
    sfol = Range("T:\DRAFTING RESOURCES\DATA SHEETS\[directory.xls]Sheet1!B" & y).Text
    file = Range("T:\DRAFTING RESOURCES\DATA SHEETS\[directory.xls]Sheet1!C" & y).Text
    dfol = ActiveWorkbook.Path
    Set fso = CreateObject("Scripting.FileSystemObject")
    If Not fso.FileExists(sfol & file) Then
        MsgBox sfol & file & " does not exist!", vbExclamation, "Source File Missing"
    ElseIf Not fso.FileExists(dfol & file) Then
        fso.CopyFile (sfol & file), dfol, True
    Else
        MsgBox dfol & file & " already exists!", vbExclamation, "Destination File Exists"
    End If
    x = x + 1
    Set y = 1
    End Sub
    Last edited by Mayaleigh; 01-09-2009 at 10:26 AM.

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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