Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

autonumbering with text

0

hi

I  try   to  find  way   to  autonumbering  with  text    so  far  it  doesn't  success  so   when  fill  in COL B  should autonumbering in COL A like  this  numbering 1, numbering 2  and  so  on , I  know   there  is  formula  does  that   but   I don't have to    pull down  every  time  increase  the  data  , that's  why  I prefer  by  vba  and  I  no  know   if  there  is  way  do  that  without  loop as in   the code .

 so  if  anybody can guide  me  I  truly  appreciate 

Private Sub Worksheet_Change(ByVal Target As Range)
 Dim cell As Range
 If Not Intersect(Range("B:B"), Target) Is Nothing Then
 For Each cell In Intersect(Range("B:B"), Target).Cells
 If cell.Value <> "" And Range("A" & cell.Row).Value = "" Then
 Range("A" & cell.Row).Value = "numbering" & " " & Application.Max(Range("A:A")) + 1
 End If
 Next
 End If
End Sub

thanks  in advance 

Answer
Discuss

Answers

0
Selected Answer

You are mixing two ideas in your code. One is to prepare a new line for new entries. The other is to apply numbering to already existing rows. The first you will need all the time but the second will be needed only once. Therefore it's not a good idea to mix the two ideas into one procedure.

However, before you start numbering you need a numbering method, and the method to use is to apply a number format to the cell. (Right-click > Format Cells > Numbers > Custom > Type). This is the format you should use:-

"numbering" 0

Now the word "numbering" is included in the cell format and all you need to enter is a number. In the attached workbook this is demonstrated in cell A5 where the number in the cell is 3 but the display is "numbering 3". You can change the format to "numbering 03" or "numbering 003" by adding more zeros to the format. You can also change the word to another language, for example. 

The code below applies this numbering method to all existing rows in your worksheet. Note that it should be saved to a standard code module (like Module1). Observe that the display format will have 2 digits.

Sub SetNumbering()
    ' 250

    Dim Rng     As Range

    With Worksheets("nu")
        ' from 3B to end of column B
        Set Rng = .Range(.Cells(3, "B"), .Cells(.Rows.Count, "B").End(xlUp))
    End With
    With Rng.Offset(0, -1)
        .NumberFormat = """numbering"" 00"
        .Formula = "=ROW()-2"
        .Copy
        .PasteSpecial xlValues
    End With
    Application.CutCopyMode = False
End Sub

The serial number of each row is 2 less than the row number. That makes for a formula like = ROW() -2, and exactly that is used in the code. To modify the number change the formula, like ROW()-3 or ROW()-1.

Now, for adding a row I usually prefer to double-click. Normally, double-click enters Edit mode. But in the attached workbook, if you double-click on the first empty cell at the bottom of column A, a new line is added. It copies the formats from row 3 (which you can change in the code), including borders, applies the number format, writes the serial number and selects the adjacent cell. This code must be in the code module of the worksheet on which you want the action.

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    ' 250

    Dim TriggerCell     As Range
    Dim R               As Long

    Set TriggerCell = Cells(Rows.Count, "A").End(xlUp).Offset(1)
    With Target
        If .Address = TriggerCell.Address Then
            Application.EnableEvents = False
            R = .Row
            Rows(3).Copy                ' copy formats from row 3
            Rows(R).Insert Shift:=xlDown
            On Error Resume Next        ' error if no special cells
            Rows(R).Cells.SpecialCells(xlCellTypeConstants).ClearContents
            On Error GoTo 0
            With Cells(R, 1)
                .NumberFormat = """numbering"" 00"
                .Value = R - 2          ' row number - 2
            End With
            Application.EnableEvents = True
            .Offset(, 1).Select
        End If
    End With
End Sub

You are well advised to add Option Explicit at the top of every code module as you see in the attached workbook. It saves tons of time and effort.

Discuss

Discussion

wow !  I  like  the  macro   in Worksheet_BeforeDoubleClick 
that's  impressive 
many  thanks  for  your  answering  
tubrak (rep: 24) May 29, '21 at 9:25 am
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login