+ Reply to Thread
Results 1 to 7 of 7

Thread: Generate Serial No. by using VBA

  1. #1
    Registered User
    Join Date
    10-16-2010
    Location
    bangalore
    MS-Off Ver
    Excel 2007
    Posts
    93

    Generate Serial No. by using VBA

    Hi,

    I need to generate a serial no.in serial no.colum when I click on command button and once the serial no.generated that particular row should be locked automaticall and even it should not allow to edit that if the user click on unprotect unless they provide the valid password.

    Attached the sample sheet.

    Rgards,
    Anwitha
    Attached Files Attached Files

  2. #2
    Forum Guru, retired Admin royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    25,640

    Re: Generate Serial No. by using VBA

    So how will you serial number be made up?
    Hope that helps.

    RoyUK
    --------
    If you are pleased with a member's answer then use the Star icon to rate it, if you are pleased enough to part with cash consider a donation to Children in Need

    For Excel Tips & Solutions, free examples and tutorials why not check out my downloads

    New members please read & follow the Forum Rules

    Remember to mark your questions Solved and rate the answer(s)

  3. #3
    Registered User
    Join Date
    10-16-2010
    Location
    bangalore
    MS-Off Ver
    Excel 2007
    Posts
    93

    Re: Generate Serial No. by using VBA

    Its 1,2,3,4,5.....

  4. #4
    Registered User
    Join Date
    10-16-2010
    Location
    bangalore
    MS-Off Ver
    Excel 2007
    Posts
    93

    Re: Generate Serial No. by using VBA

    Can anybody help me on this?

  5. #5
    Valued Forum Contributor Haseeb A's Avatar
    Join Date
    05-24-2011
    Location
    India | Kwt
    MS-Off Ver
    2007
    Posts
    1,460

    Re: Generate Serial No. by using VBA

    Hello Anwitha,

    I don't know much about VBA, but try the attached. The code basically doing is,

    1. Unprotect the sheet
    2. Adding serial numbers in Col_A
    3. Protect the rows 1 to Row contain the last SL Number.

    Hope this helps.

    Private Sub CommandButton1_Click()
        ActiveSheet.Unprotect Password:="password"
      
        Dim FR As Long, LR As Long
        FR = Range("A1").End(xlDown).Row + 1
        LR = Range("A" & Rows.Count).End(xlUp).Row + 1
        
        With Range("A" & FR & ":A" & LR)
            .FormulaR1C1 = "=MAX(R" & FR - 1 & "C:R[-1]C)+1"
            .Value = .Value
        End With
        On Error Resume Next
        
        ActiveSheet.Protection.AllowEditRanges(1).Delete
        ActiveSheet.Protection.AllowEditRanges.Add Title:="Range1", Range:=Rows(LR + 1 & ":" & Rows.Count)
        ActiveSheet.Protect Password:="password"
    
    End Sub
    Attached Files Attached Files
    HTH; Haseeb

    If your problem is solved, please say so clearly, and mark your thread as Solved:
    Forum Rules & How to Mark a thread as SOLVED

  6. #6
    Registered User
    Join Date
    10-16-2010
    Location
    bangalore
    MS-Off Ver
    Excel 2007
    Posts
    93

    Re: Generate Serial No. by using VBA

    Habeeb you are genius..its working...

    Cheers..
    Anwitha

  7. #7
    Forum Guru, retired Admin royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    25,640

    Re: Generate Serial No. by using VBA

    Why use VBA? A Dynamic Named Range and the Max Function would work.

    If you need VBA the use the worksheet_change event

    Option Explicit
    
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
        If Target > "" Then Exit Sub
        Dim rRng As Range
        Set rRng = Range(Cells(4, 1), Cells(Rows.Count, 1).End(xlUp))
        If Intersect(Target, rRng) Is Nothing Then Target.Value = _
           Application.WorksheetFunction.Max(rRng) + 1
    End Sub
    Worksheet event code is stored on a worksheet module. To add it to your worksheet, do the following:

    Copy the code
    Select the worksheet in which you the code to run
    Right click on the sheet tab and choose View Code, to open the Visual Basic Editor.
    Where the cursor is flashing, choose Edit | Paste
    Hope that helps.

    RoyUK
    --------
    If you are pleased with a member's answer then use the Star icon to rate it, if you are pleased enough to part with cash consider a donation to Children in Need

    For Excel Tips & Solutions, free examples and tutorials why not check out my downloads

    New members please read & follow the Forum Rules

    Remember to mark your questions Solved and rate the answer(s)

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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.2.0