Results 1 to 12 of 12

Incrament serial number up or down using txt file and directory search

Threaded View

  1. #1
    Forum Contributor
    Join Date
    10-07-2013
    Location
    Wilts, England
    MS-Off Ver
    Excel 2013
    Posts
    100

    Incrament serial number up or down using txt file and directory search

    Hi Everyone,
    Any help on this issue would be greatly appreciated, even if it is just to tell me I'm on completely the wrong lines. I have posted on this issue before; http://www.excelforum.com/excel-prog...ml#post3513111

    What I am trying to achieve is generating a unique serial number that can not be duplicated.
    I want to be able to open a new document from an excel template and the serial number be generated from a text file (got that bit down). Then I want a loop to be run to check the folder the file was saved in to check if a file exists with the same serial number (the file will be saved with the serial number as a name). If a file is found the number stored in the txt file is decrease by -1 then that result is displayed. If no file is found then the result stays the same and that is displayed (when my current loop is run it displays the msg box to say "nothing found" then the serial number disappears).
    Sorry if this an incoherent run down, trying my best.

    Function GetSerialNumber(Optional FilePath As String, Optional FileName As String) As String
    
      Dim DefaultName As String
      Dim DefaultPath As String
      Dim FSO As Object
      Dim SeqNum As Variant
      Dim TxtFile As Object
      Dim TargetCell As Object
      
        DefaultPath = "C:\Documents and Settings\bethan\My Documents\Scripting\Delivery Note Job"
        DefaultName = "Invoice"
        
          FilePath = IIf(FilePath = "", DefaultPath, FilePath)
          FilePath = IIf(Right(FilePath, 1) <> "\", FilePath & "\", FilePath)
        
          FileName = IIf(FileName = "", DefaultName, FileName)
          FileName = FilePath & IIf(InStr(1, FileName, ".") = 0, FileName & ".txt", FileName)
        
          Set FSO = CreateObject("Scripting.FileSystemObject")
        
           'Open the file for Reading and Create the file if it doesn't exists
            Set TxtFile = FSO.OpenTextFile(FileName, 1, True, 0)
          
           'Read the serial number
            If Not TxtFile.AtEndOfStream Then SeqNum = TxtFile.ReadLine
            TxtFile.Close
          
           'Update the serial number
            Set TxtFile = FSO.OpenTextFile(FileName, 2, False, 0)
            SeqNum = Format(IIf(SeqNum = "", "1", Val(SeqNum) + 1), "000000")
            TxtFile.WriteLine SeqNum
          
       TxtFile.Close
       GetSerialNumber = SeqNum
       
         
         With ThisWorkbook.Sheets
                With Cells(7, 11)
                    If IsEmpty(.Value) Then
                        .Value = Date
                        .NumberFormat = "dd mmm yyyy"
                    End If
                    End With
     Visible = True
       Set FSO = Nothing
       Set TxtFile = Nothing
       End With
       End Function
       
    Function MinusOne(Optional FilePath As String, Optional FileName As String) As String
    
      Dim DefaultName As String
      Dim DefaultPath As String
      Dim FSO As Object
      Dim SeqNum As Variant
      Dim TxtFile As Object
      Dim TargetCell As Object
      
        DefaultPath = "C:\Documents and Settings\bethan\My Documents\Scripting\Delivery Note Job"
        DefaultName = "Invoice"
        
          FilePath = IIf(FilePath = "", DefaultPath, FilePath)
          FilePath = IIf(Right(FilePath, 1) <> "\", FilePath & "\", FilePath)
        
          FileName = IIf(FileName = "", DefaultName, FileName)
          FileName = FilePath & IIf(InStr(1, FileName, ".") = 0, FileName & ".txt", FileName)
        
          Set FSO = CreateObject("Scripting.FileSystemObject")
        
           'Open the file for Reading and Create the file if it doesn't exists
            Set TxtFile = FSO.OpenTextFile(FileName, 1, True, 0)
          
           'Read the serial number
            If Not TxtFile.AtEndOfStream Then SeqNum = TxtFile.ReadLine
            TxtFile.Close
          
           'Update the serial number
            Set TxtFile = FSO.OpenTextFile(FileName, 2, False, 0)
            SeqNum = Format(IIf(SeqNum = "", "1", Val(SeqNum) - 1), "000000")
            TxtFile.WriteLine SeqNum
          
       TxtFile.Close
       GetSerialNumber = SeqNum
       
         
         With ThisWorkbook.Sheets
                With Cells(7, 11)
                    If IsEmpty(.Value) Then
                        .Value = Date
                        .NumberFormat = "dd mmm yyyy"
                    End If
                    End With
                     
       Set FSO = Nothing
       Set TxtFile = Nothing
       End With
       End Function
       
      
    Function CheckIFVoid(Optional FilePath As String, Optional FileName As String) As String
    
    Dim MyFileCount As Integer
    Dim DefaultPath As String
    Dim TargetCell As Object
    Dim Execute As Variant
    
       DefaultPath = "C:\Documents and Settings\bethan\My Documents\Scripting\Delivery Note Job"
        Set TargetCell = Sheets(1).Range("K5")
        
    With Application.FileSearch
    .NewSearch
    .LookIn = DefaultPath
    .FileName = TargetCell
    
     MyFileCount = 0
      If .Execute() > 0 Then
      Sheets(1).Range("K5") = MinusOne()
      End If
     If .Execute() = 0 Then
     MsgBox ("nothing found")
     End If
      End With
    End Function
    
     Sub Auto_Open()
    If Cells(5, 11).Value = "" Then
      Sheets(1).Range("K5") = GetSerialNumber()
      End If
    If Cells(5, 11).Value >= 0 Then
    Sheets(1).Range("K5") = CheckIFVoid
      
      End If
    End Sub
    Any help or guidance on this issue would be amazing!
    Attached Files Attached Files

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Need VBA code to search file in directory and sub-directory and show result
    By johnchencanada in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-19-2012, 11:13 PM
  2. Finding a serial number in a list of serial numbers
    By zocoloco in forum Excel General
    Replies: 2
    Last Post: 02-04-2009, 05:20 AM
  3. Search for file in directory
    By ryanlcs in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-12-2008, 07:49 PM
  4. Search a directory for a file
    By Josh_123456 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-20-2006, 11:52 AM
  5. automatic new serial number for each new sheet within one file
    By ahmed in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 02-26-2005, 10:06 AM

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