automatically auto numbering in multiple columns based on fill number

KalilMe

Active Member
Joined
Mar 5, 2021
Messages
343
Office Version
  1. 2016
Platform
  1. Windows
hi
I would auto numbering each column contain word of header is ITEM what I write type of number then should when press the next cell autonumber and so on to reach a different number
first
00 (1).xlsx
EFGHIJ
1itembranditembranditembrand
2bsd/12oli1-12fj-1water 1ch/jj00-1cheese1
3oli1-13water 2cheese2
4oli1-14water 3cheese3
5oli1-15water 4cheese4
6oli1-16water 5cheese5
7oli1-17water 6cheese6
8oli1-18water 7cheese7
9oli1-19water 8cheese8
10oli1-20water 9cheese9
11oli1-21water 10cheese10
12ss-210tuna1-1water 11cheese11
13tuna1-2water 12cheese12
14tuna1-310-s001drink1cheese13
15tuna1-4drink2cheese14
16tuna1-5drink3cheese15
17tuna1-6drink4cheese16
18fd-12food1drink5cheese17
19food2drink6bt-200butter1
20food3drink7butter2
21food4drink8butter3
22food5drink9butter4
23food6drink10butter5
24food7drink11butter6
25food8drink12butter7
26food9drink13butter8
27food10drink14butter9
28food11drink15butter10
29food12drink16butter11
1


expected result
00 (1).xlsx
EFGHIJ
1itembranditembranditembrand
2bsd/12oli1-12fj-1water 1ch/jj00-1cheese1
3bsd/13oli1-13fj-2water 2ch/jj00-2cheese2
4bsd/14oli1-14fj-3water 3ch/jj00-3cheese3
5bsd/15oli1-15fj-4water 4ch/jj00-4cheese4
6bsd/16oli1-16fj-5water 5ch/jj00-5cheese5
7bsd/17oli1-17fj-6water 6ch/jj00-6cheese6
8bsd/18oli1-18fj-7water 7ch/jj00-7cheese7
9bsd/19oli1-19fj-8water 8ch/jj00-8cheese8
10bsd/20oli1-20fj-9water 9ch/jj00-9cheese9
11bsd/21oli1-21fj-10water 10ch/jj00-10cheese10
12ss-210tuna1-1fj-11water 11ch/jj00-11cheese11
13ss-211tuna1-2fj-12water 12ch/jj00-12cheese12
14ss-212tuna1-310-s001drink1ch/jj00-13cheese13
15ss-213tuna1-410-s002drink2ch/jj00-14cheese14
16ss-214tuna1-510-s003drink3ch/jj00-15cheese15
17ss-215tuna1-610-s004drink4ch/jj00-16cheese16
18fd-12food110-s005drink5ch/jj00-17cheese17
19fd-13food210-s006drink6bt-200butter1
20fd-14food310-s007drink7bt-201butter2
21fd-15food410-s008drink8bt-202butter3
22fd-16food510-s009drink9bt-203butter4
23fd-17food610-s010drink10bt-204butter5
24fd-18food710-s011drink11bt-205butter6
25fd-19food810-s012drink12bt-206butter7
26fd-20food910-s013drink13bt-207butter8
27fd-21food1010-s014drink14bt-208butter9
28fd-22food1110-s015drink15bt-209butter10
29fd-23food1210-s016drink16bt-210butter11
ة1
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Does this do what you want...

VBA Code:
Sub Food()

    Dim lCol As Long, i As Long, lRow As Long, lRow2 As Long, ct As Long, x As Long
    Dim itm As String

    lCol = Cells(1, Columns.Count).End(xlToLeft).Column
    For i = 5 To lCol
        If Cells(1, i) = "item" Then
            lRow = Cells(Rows.Count, i + 1).End(xlUp).Row
            ct = Application.WorksheetFunction.CountA(Range(Cells(2, i), Cells(lRow, i)))
            For x = 1 To ct
                lRow2 = Cells(lRow, i).End(xlUp).Row
                itm = Cells(lRow2, i).Value
                Cells(lRow2, i).Resize(lRow - lRow2 + 1) = itm
                lRow = lRow2 - 1
            Next
        End If
    Next

End Sub
 
Upvote 0
thanks
this is what I got after run the code
00 (1).xlsx
EFGHIJ
1itembranditembranditembrand
2bsd/12oli1-12fj-1water 1ch/jj00-1cheese1
3bsd/12oli1-13fj-1water 2ch/jj00-1cheese2
4bsd/12oli1-14fj-1water 3ch/jj00-1cheese3
5bsd/12oli1-15fj-1water 4ch/jj00-1cheese4
6bsd/12oli1-16fj-1water 5ch/jj00-1cheese5
7bsd/12oli1-17fj-1water 6ch/jj00-1cheese6
8bsd/12oli1-18fj-1water 7ch/jj00-1cheese7
9bsd/12oli1-19fj-1water 8ch/jj00-1cheese8
10bsd/12oli1-20fj-1water 9ch/jj00-1cheese9
11bsd/12oli1-21fj-1water 10ch/jj00-1cheese10
12ss-210tuna1-1fj-1water 11ch/jj00-1cheese11
13ss-210tuna1-2fj-1water 12ch/jj00-1cheese12
14ss-210tuna1-310-s001drink1ch/jj00-1cheese13
15ss-210tuna1-410-s001drink2ch/jj00-1cheese14
16ss-210tuna1-510-s001drink3ch/jj00-1cheese15
17ss-210tuna1-610-s001drink4ch/jj00-1cheese16
18fd-12food110-s001drink5ch/jj00-1cheese17
19fd-12food210-s001drink6bt-200butter1
20fd-12food310-s001drink7bt-200butter2
21fd-12food410-s001drink8bt-200butter3
22fd-12food510-s001drink9bt-200butter4
23fd-12food610-s001drink10bt-200butter5
24fd-12food710-s001drink11bt-200butter6
25fd-12food810-s001drink12bt-200butter7
26fd-12food910-s001drink13bt-200butter8
27fd-12food1010-s001drink14bt-200butter9
28fd-12food1110-s001drink15bt-200butter10
29fd-12food1210-s001drink16bt-200butter11
1

this is not the same image2 it should increment
 
Upvote 0
My bad, how about this...

VBA Code:
Sub Food2()

    Dim lCol As Long, i As Long, lRow As Long, lRow2 As Long, ct As Long, x As Long
    Dim itm As String
    
    Application.ScreenUpdating = False
    lCol = Cells(1, Columns.Count).End(xlToLeft).Column
    For i = 5 To lCol
        If Cells(1, i) = "item" Then
            lRow = Cells(Rows.Count, i + 1).End(xlUp).Row
            ct = Application.WorksheetFunction.CountA(Range(Cells(2, i), Cells(lRow, i)))
            For x = 1 To ct
                lRow2 = Cells(lRow, i).End(xlUp).Row
                Cells(lRow2, i).Select
                Selection.AutoFill Destination:=Range(Selection, Cells(lRow, i)), Type:=xlFillDefault
                lRow = lRow2 - 1
            Next
        End If
    Next
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0
You're welcome. I was happy to help. Thanks for the feedback!
 
Upvote 0
sorry I come back again but the code need fixing if I write 1 it doesn't increment 2,3... it copies the same value it shows 1,1,1 and if I repeat running macro it gives error "application defined or object defined error " in this line
VBA Code:
lRow2 = Cells(lRow, i).End(xlUp).Row
and replace word ITEM for all numbers . may you fix it please ?
 
Upvote 0
This will take care of the event that you have a number and not a string to be filled. However the code as written was designed to only be run once on any data set as provided in your sample. The only way for anyone to write code for every possibility that you may have is to provide a more comprehensive example of your needs. I have added a line that will force the sub to exit before it writes the word "item" down the column.

VBA Code:
Sub Food2()

    Dim lCol As Long, i As Long, lRow As Long, lRow2 As Long, ct As Long, x As Long
    Dim itm As String
    
    Application.ScreenUpdating = False
    lCol = Cells(1, Columns.Count).End(xlToLeft).Column
    For i = 5 To lCol
        If Cells(1, i) = "item" Then
            lRow = Cells(Rows.Count, i + 1).End(xlUp).Row
            ct = Application.WorksheetFunction.CountA(Range(Cells(2, i), Cells(lRow, i)))
            For x = 1 To ct
                lRow2 = Cells(lRow, i).End(xlUp).Row
                If lRow2 = 1 Then
                    MsgBox "Invalid Column Found - Sub Will Now Exit"
                    Exit Sub
                End If
                Cells(lRow2, i).Select
                If Application.WorksheetFunction.IsNumber(Selection) Then
                    Selection.AutoFill Destination:=Range(Selection, Cells(lRow, i)), Type:=xlFillSeries
                Else
                    Selection.AutoFill Destination:=Range(Selection, Cells(lRow, i)), Type:=xlFillDefault
                End If
                lRow = lRow2 - 1
            Next
        End If
    Next
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0
Solution
You're welcome. Thanks again for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,386
Messages
6,119,214
Members
448,874
Latest member
b1step2far

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top