+ Reply to Thread
Results 1 to 5 of 5

Help with ReDim

Hybrid View

  1. #1
    Registered User
    Join Date
    06-04-2012
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2010
    Posts
    10

    Help with ReDim

    I have a spreadsheet that includes a script to generate serial numbers. The script looks at the last s/n in the range and the rewrites the range with the next series of numbers. The old serial number format used to look like this "Model B 123456" but the numbers have now gone to the format "Model B 1234-B-1" with the "-B-1" remaining the same for each serial number.

    Sub GenerateNewSerialNumbers()
        
        Const strModel As String = "Model B "
           
        Dim arrNew() As String
        Dim nSerial As Long
        Dim rIndex As Long
        Dim cIndex As Long
        
        ReDim arrNew(1 To 80, 1 To 7)
        nSerial = --Mid(ActiveSheet.Range("G20").Text, InStrRev(ActiveSheet.Range("G20").Text, " "))
        
        For rIndex = 1 To 80
            For cIndex = 1 To 7 Step 2
                nSerial = nSerial + 1
                arrNew(rIndex, cIndex) = strModel & nSerial
            Next cIndex
        Next rIndex
        
        Range("A1:G20").Value = arrNew
        
    End Sub

    I have tried to change this script to get it to work, but there is something happening that I am not understanding. I think it might be in the ReDim section of the script.

    Could someone please point me in the right direction so I can get this script working again for the new serial number? I realize there are simpler ways to do this, but we have some people who arent too computer savvy so it works well for them to be able to open the spreadsheet, click the "Generate New Serial Numbers" button and then print.

    Thanks.

  2. #2
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Help with ReDim

    hi c.young, option

    Sub GenerateNewSerialNumbers()
        
        Const strModel As String = "Model B "
        Const strEnd As String = "-B-1"
        
        Dim arrNew() As String
        Dim nSerial As Long
        Dim rIndex As Long
        Dim cIndex As Long
        
        ReDim arrNew(1 To 80, 1 To 7)
        nSerial = Replace(Replace(Range("G20"), strModel, ""), strEnd, "")
        
        For rIndex = 1 To 80
            For cIndex = 1 To 7 Step 2
                nSerial = nSerial + 1
                arrNew(rIndex, cIndex) = strModel & nSerial & strEnd
            Next cIndex
        Next rIndex
        
        Range("A1:G20").Value = arrNew
        
    End Sub

  3. #3
    Registered User
    Join Date
    06-04-2012
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Help with ReDim

    Watersev,

    I get a type mismatch when I run that.

    I have attached the old version and the new version of the spread sheets.Model B Warranty Labels.xlsm UL Model B Warranty Labels.xlsm

    The new serial number format is in the UL spreadsheet.

  4. #4
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Help with ReDim

    your original post stated "Model B " and now it appears to be "MODEL B ", change constant strModel in the code and you will get expected result

  5. #5
    Registered User
    Join Date
    06-04-2012
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Help with ReDim

    Sorry to miss that small error. Yes, the script now works, thank you.

+ 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] ReDim=overwrite?
    By Arne Hegefors in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-22-2006, 12:05 PM
  2. [SOLVED] ReDim Matrix
    By Arne Hegefors in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-22-2006, 05:15 AM
  3. ReDim Array
    By Viktor Ygdorff in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-10-2006, 11:09 AM
  4. [SOLVED] Dim and Redim
    By Sean in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-05-2006, 02:35 PM
  5. [SOLVED] redim preserve
    By RobcPettit@yahoo.co.uk in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-15-2005, 09:45 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