Auto serial number VBA code

Zubair

Active Member
Joined
Jul 4, 2009
Messages
304
Office Version
  1. 2016
Platform
  1. Windows
Hi Experts.

I have the following 2 sheets, need VBA code to get the Document number automatically and data manual input, A5 to G manual input.

Want to move transactions from the Entry to Data tab and clear all data in the Entry sheet.

Entry
Document No.
Date
ProductSizeQuantityRateAmount
Black
13​
40​
3​
120​
Pink
15​
30​
5​
150​
Blue
13​
50​
4​
200​
Green
12​
80​
8​
640​

Data
Document No.DateProductSizeQuantityRateAmount
10001​
1/1/2022​
Black
13​
40​
3​
120​
10001​
1/1/2022​
Pink
15​
30​
5​
150​
10001​
1/1/2022​
Blue
13​
50​
4​
200​
10001​
1/1/2022​
Green
12​
80​
8​
640​
10002​
7/1/2022​
Blue
20​
10​
4​
40​
10002​
7/1/2022​
Black
15​
12​
3​
36​
10003​
22/1/2022Pink
15​
30​
5​
150​
10003​
22/1/2022Blue
13​
50​
4​
200​
10003​
22/1/2022Green
12​
80​
8​
640​
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
This could probably be done with Excel's built-in Input Form but here's some VBA.
Put this in the Entry sheet module :
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address <> "$B$2" Then Exit Sub
Application.EnableEvents = False
With Sheets("Data").Cells(Rows.Count, "A").End(3)
    If .Value = "Document No." Then
        [B1] = 100001
    Else: [B1] = .Value + 1
    End If
End With
Application.EnableEvents = True
End Sub
Put this in a normal module :
VBA Code:
Sub v()
Dim rng As Range, r%
Set rng = Range([A5], Cells(Rows.Count, "E").End(3))
r = rng.Rows.Count
With Sheets("Data").Cells(Rows.Count, "C").End(3)
    rng.Copy .Offset(1)
    [B1].Copy .Offset(1, -2).Resize(r)
    [B2].Copy .Offset(1, -1).Resize(r)
End With
Application.EnableEvents = False
Union([B1:B2], rng).ClearContents
Application.EnableEvents = True
End Sub

Do not enter the Document No. in B1 of the Entry sheet.
When you enter the date in B2 the Document No. will appear.

When you have completed the data on the Entry sheet, with the Entry sheet active run the macro "v".
 
Upvote 0
Solution

Forum statistics

Threads
1,214,929
Messages
6,122,317
Members
449,081
Latest member
tanurai

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