Results 1 to 17 of 17

Decrease serial number if matching file found

Threaded View

beenbee Decrease serial number if... 01-06-2014, 06:55 AM
Norie Re: Decrease serial number if... 01-06-2014, 07:02 AM
beenbee Re: Decrease serial number if... 01-06-2014, 07:12 AM
Norie Re: Decrease serial number if... 01-06-2014, 07:21 AM
beenbee Re: Decrease serial number if... 01-06-2014, 07:31 AM
Norie Re: Decrease serial number if... 01-06-2014, 07:35 AM
beenbee Re: Decrease serial number if... 01-06-2014, 07:45 AM
beenbee Re: Decrease serial number if... 01-06-2014, 07:43 AM
beenbee Re: Decrease serial number if... 01-06-2014, 08:55 AM
Norie Re: Decrease serial number if... 01-06-2014, 09:14 AM
beenbee Re: Decrease serial number if... 01-06-2014, 09:37 AM
beenbee Re: Decrease serial number if... 01-06-2014, 09:31 AM
Norie Re: Decrease serial number if... 01-06-2014, 09:33 AM
Norie Re: Decrease serial number if... 01-06-2014, 09:44 AM
beenbee Re: Decrease serial number if... 01-06-2014, 10:00 AM
beenbee Re: Decrease serial number if... 01-06-2014, 09:59 AM
beenbee Re: Decrease serial number if... 01-06-2014, 10:30 AM
  1. #1
    Forum Contributor
    Join Date
    10-07-2013
    Location
    Wilts, England
    MS-Off Ver
    Excel 2013
    Posts
    100

    Decrease serial number if matching file found

    Hi Everyone, Any help with any part of this issue would be a great help. I have posted this a couple of times before. You are probably getting sick of seeing this but I hoped someone may be able to help, I'm not giving up on this issue.
    http://www.excelforum.com/excel-prog...ml#post3530142

    So to the brain achingly annoying issue. What I'm trying to achieve is;
    1. When a new document is opened from my template a unique serial number (in format "00000") is generated (currently using txt file)
    2. Once the number is generated a search is preformed to see if a file exists saved with the same serial number as a name.
    3. If a file exists then the txt files stored value is decreased by 1 and that is displayed in "K5".
    4. If the search returns a 0 then the original serial number is displayed.

    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
                     
       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
       MinusOne = 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

    Currently i don't belive the search function is working. So any help with that would be greatly appreciated and oddly once the search fails to run properly the original serial number dissapears.

    Thanks Everyone.
    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. Incrament serial number up or down using txt file and directory search
    By beenbee in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 01-29-2014, 08:38 AM
  2. Replies: 1
    Last Post: 02-21-2013, 05:49 PM
  3. Replies: 3
    Last Post: 09-18-2012, 01:01 AM
  4. 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
  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