Add item in first digits with create space after it based on sheet name

Maklil

Board Regular
Joined
Jun 23, 2022
Messages
174
Office Version
  1. 2019
Platform
  1. Windows
Hello
I want add item in first digit and make one space based on sheet name also write INVOICE NO word after sheet name word and make one space after it for each sheet in column (B) and if there is already existed after add from first time then don't add again . I will add new sheets , but I want macro to implement for specific sheets , not all of sheets in workbook.
NEW.xlsx
ABCDEFG
1DATEINV NNOIDCASEQTYUNIT PRICEBALANCE
221/08/2023VT NO 1000FOO LL 1000PAID220.00123.0027,060.00
321/08/2023VT NO 1000GNOO HH 1200PAID100.00122.0012,200.00
421/08/2023VT NO 1000AS100-12PAID120.00111.0013,320.00
521/08/2023VT NO 1001MGFH GA-100NOT PAID100.00110.0011,000.00
621/08/2023VT NO 1001MGFH GA-101NOT PAID120.00111.0013,320.00
721/08/2023VT NO 1001MGFH GA-102NOT PAID110.00110.0012,100.00
821/08/2023VT NO 1001MGFH GA-103NOT PAID12.00100.001,200.00
921/08/2023VT NO 1002SSFOO 1000 MN1NOT PAID20.00124.002,480.00
10
1121/08/2023VT NO 1002SSFOO 1000 MN2NOT PAID30.00122.003,660.00
1221/08/2023VT NO 1002SSFOO 1000 MN3NOT PAID24.00124.002,976.00
1321/08/2023VT NO 1002SSFOO 1000 MN4NOT PAID22.00125.002,750.00
1421/08/2023VT NO 1002SSFOO 1000 MN5NOT PAID10.00144.001,440.00
1521/08/2023VT NO 1003OOIL AS-100PAID10.00145.001,450.00
1621/08/2023VT NO 1003OOIL AS-101PAID25.00150.003,750.00
1721/08/2023VT NO 1003OOIL AS-102PAID40.00155.006,200.00
1821/08/2023VT NO 1003OOIL AS-103PAID55.00160.008,800.00
19
2021/08/2023VT NO 1003OOIL AS-104PAID70.00165.0011,550.00
2121/08/2023VT NO 1003OOIL AS-105PAID85.00170.0014,450.00
2221/08/2023VT NO 1003OOIL AS-106PAID100.00175.0017,500.00
2321/08/2023VT NO 1003OOIL AS-107PAID115.00180.0020,700.00
2422/08/2023VT NO 1004OOIL AS-108PAID130.00185.0024,050.00
2522/08/2023VT NO 1004OOIL AS-109PAID145.00190.0027,550.00
BUYING
Cell Formulas
RangeFormula
G20:G25,G15:G18,G2:G8G2=E2*F2
G9,G11:G14G9=F9*E9



NEW.xlsx
ABCDEFG
1DATEINV NNOIDCASEQTYUNIT PRICEBALANCE
221/08/2023ST NO 1000GNOO HH 1200PAID10.00150.001,500.00
321/08/2023ST NO 1000AS100-12PAID10.00130.001,300.00
421/08/2023ST NO 1001MGFH GA-103NOT PAID15.00130.001,950.00
521/08/2023ST NO 1002SSFOO 1000 MN1NOT PAID2.00140.00280.00
6
721/08/2023ST NO 1002SSFOO 1000 MN2NOT PAID12.00145.001,740.00
821/08/2023ST NO 1002SSFOO 1000 MN3NOT PAID10.00145.001,450.00
924/08/2023VT NO 1003OOIL AS-100PAID10.00145.001,450.00
1024/08/2023VT NO 1003OOIL AS-101PAID25.00150.003,750.00
11
1224/08/2023VT NO 1003OOIL AS-102PAID40.00155.006,200.00
1324/08/2023VT NO 1003OOIL AS-103PAID55.00160.008,800.00
1424/08/2023VT NO 1003OOIL AS-104PAID70.00165.0011,550.00
1524/08/2023VT NO 1003OOIL AS-105PAID85.00170.0014,450.00
1624/08/2023VT NO 1003OOIL AS-106PAID100.00175.0017,500.00
1724/08/2023VT NO 1003OOIL AS-107PAID115.00180.0020,700.00
SELLING
Cell Formulas
RangeFormula
G12:G17,G9:G10,G2:G4G2=E2*F2
G5,G7:G8G5=F5*E5


result

NEW.xlsx
ABCDEFG
1DATEINV NNOIDCASEQTYUNIT PRICEBALANCE
221/08/2023BUYING INVOICE NO VT NO 1000FOO LL 1000PAID220.00123.0027,060.00
321/08/2023BUYING INVOICE NO VT NO 1000GNOO HH 1200PAID100.00122.0012,200.00
421/08/2023BUYING INVOICE NO VT NO 1000AS100-12PAID120.00111.0013,320.00
521/08/2023BUYING INVOICE NO VT NO 1001MGFH GA-100NOT PAID100.00110.0011,000.00
621/08/2023BUYING INVOICE NO VT NO 1001MGFH GA-101NOT PAID120.00111.0013,320.00
721/08/2023BUYING INVOICE NO VT NO 1001MGFH GA-102NOT PAID110.00110.0012,100.00
821/08/2023BUYING INVOICE NO VT NO 1001MGFH GA-103NOT PAID12.00100.001,200.00
921/08/2023BUYING INVOICE NO VT NO 1002SSFOO 1000 MN1NOT PAID20.00124.002,480.00
10
1121/08/2023BUYING INVOICE NO VT NO 1002SSFOO 1000 MN2NOT PAID30.00122.003,660.00
1221/08/2023BUYING INVOICE NO VT NO 1002SSFOO 1000 MN3NOT PAID24.00124.002,976.00
1321/08/2023BUYING INVOICE NO VT NO 1002SSFOO 1000 MN4NOT PAID22.00125.002,750.00
1421/08/2023BUYING INVOICE NO VT NO 1002SSFOO 1000 MN5NOT PAID10.00144.001,440.00
1521/08/2023BUYING INVOICE NO VT NO 1003OOIL AS-100PAID10.00145.001,450.00
1621/08/2023BUYING INVOICE NO VT NO 1003OOIL AS-101PAID25.00150.003,750.00
1721/08/2023BUYING INVOICE NO VT NO 1003OOIL AS-102PAID40.00155.006,200.00
1821/08/2023BUYING INVOICE NO VT NO 1003OOIL AS-103PAID55.00160.008,800.00
19
2021/08/2023BUYING INVOICE NO VT NO 1003OOIL AS-104PAID70.00165.0011,550.00
2121/08/2023BUYING INVOICE NO VT NO 1003OOIL AS-105PAID85.00170.0014,450.00
2221/08/2023BUYING INVOICE NO VT NO 1003OOIL AS-106PAID100.00175.0017,500.00
2321/08/2023BUYING INVOICE NO VT NO 1003OOIL AS-107PAID115.00180.0020,700.00
2422/08/2023BUYING INVOICE NO VT NO 1004OOIL AS-108PAID130.00185.0024,050.00
2522/08/2023BUYING INVOICE NO VT NO 1004OOIL AS-109PAID145.00190.0027,550.00
BUYING
Cell Formulas
RangeFormula
G20:G25,G15:G18,G2:G8G2=E2*F2
G9,G11:G14G9=F9*E9



NEW.xlsx
ABCDEFG
1DATEINV NNOIDCASEQTYUNIT PRICEBALANCE
221/08/2023SELLING INVOICE NO ST NO 1000GNOO HH 1200PAID10.00150.001,500.00
321/08/2023SELLING INVOICE NO ST NO 1000AS100-12PAID10.00130.001,300.00
421/08/2023SELLING INVOICE NO ST NO 1001MGFH GA-103NOT PAID15.00130.001,950.00
521/08/2023SELLING INVOICE NO ST NO 1002SSFOO 1000 MN1NOT PAID2.00140.00280.00
6
721/08/2023SELLING INVOICE NO ST NO 1002SSFOO 1000 MN2NOT PAID12.00145.001,740.00
821/08/2023SELLING INVOICE NO ST NO 1002SSFOO 1000 MN3NOT PAID10.00145.001,450.00
924/08/2023SELLING INVOICE NO ST NO 1003OOIL AS-100PAID10.00145.001,450.00
1024/08/2023SELLING INVOICE NO ST NO 1003OOIL AS-101PAID25.00150.003,750.00
11
1224/08/2023SELLING INVOICE NO ST NO 1003OOIL AS-102PAID40.00155.006,200.00
1324/08/2023SELLING INVOICE NO ST NO 1003OOIL AS-103PAID55.00160.008,800.00
1424/08/2023SELLING INVOICE NO ST NO 1003OOIL AS-104PAID70.00165.0011,550.00
1524/08/2023SELLING INVOICE NO ST NO 1003OOIL AS-105PAID85.00170.0014,450.00
1624/08/2023SELLING INVOICE NO ST NO 1003OOIL AS-106PAID100.00175.0017,500.00
1724/08/2023SELLING INVOICE NO ST NO 1003OOIL AS-107PAID115.00180.0020,700.00
SELLING
Cell Formulas
RangeFormula
G12:G17,G9:G10,G2:G4G2=E2*F2
G5,G7:G8G5=F5*E5
 
Last edited:

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
Try this Macro. Works on the active sheet.
VBA Code:
Sub AddItem()
Dim Sh$, Shadd$, Lr&, M

Sh = ActiveSheet.Name: Shadd = Sh & " INVOICE NO "
Lr = Range("A" & Rows.Count).End(xlUp).Row

Range("B2:B" & Lr) = Evaluate("IFerror(IF(Left(B2:B25,find("" "",B2:B" & Lr & ")-1)=""" & Sh & """,B2:B" & Lr & ",""" & Shadd & """&B2:B25),"""")")

End Sub
 
Last edited:
Upvote 0
thanks ,
but I would implement for specific sheet , not active sheet !
I will add new sheets , but I want macro to implement for specific sheets , not all of sheets in workbook.
also in this part
VBA Code:
M <span>=</span> Evaluate<span>(</span><span>"IFerror(IF(Left(B2:B25</span>
I would range is dynamic when add new data for each sheet. the data are not finish until B25.
 
Upvote 0
Code modified. Input box provided. Enter the name of the sheet. Range will be automatically taken care by macro.

VBA Code:
Sub AddItem()
Dim Sh$, Shadd$, Lr&, M

Sh = InputBox("Enter the name of the sheet: ", "SHEET NAME")

If Sh = "" Then Exit Sub
Shadd = Sh & " INVOICE NO "
Lr = Range("A" & Rows.Count).End(xlUp).Row

With Range("B2:B" & Lr)
.Value = Evaluate("IFerror(IF(Left(B2:B" & Lr & ",find("" "",B2:B" & Lr & ")-1)=""" & Sh & """,B2:B" & Lr & ",""" & Shadd & """&B2:B" & Lr & "),"""")")
.WrapText = False
.EntireColumn.AutoFit
End With

End Sub
 
Last edited:
Upvote 0
may put sheets in array and implement at once instead of using inputbox to do for each sheet individually like this ("BUYING","SELLING"....),please?
 
Upvote 0
Try. I have added 2 names in array. If required add further names. No other changes are required in code when more sheets are added.
VBA Code:
Sub AddItem()
Dim Shadd$, Rng$, Lr&, Sh, Shnames, M

Shnames = Array("SELLING", "BUYING")
For Each Sh In Shnames
If Sh = "" Then Exit Sub
With Sheets(Sh)
Shadd = Sh & " INVOICE NO "
Lr = .Range("A" & Rows.Count).End(xlUp).Row
Rng = "'" & Sh & "'!B2:B" & Lr
    With .Range("B2:B" & Lr)
    .Value = Evaluate("IFerror(IF(Left(" & Rng & ",find("" ""," & Rng & ")-1)=""" & Sh & """," & Rng & ",""" & Shadd & """&" & Rng & "),"""")")
    .WrapText = False
    .EntireColumn.AutoFit
    End With
End With
Next Sh
End Sub
 
Upvote 0
I have improved the code to avoid bugs. Try this.
VBA Code:
Sub AddItem()
Dim Shadd$, Rng$, Lr&, Sh, Shnames
Shnames = Array("SELLING", "BUYING")
For Each Sh In Shnames
If Sh = "" Then Exit Sub
With Sheets(Sh)
Shadd = Sh & " INVOICE NO "
Lr = .Range("A" & Rows.Count).End(xlUp).Row
Rng = "'" & Sh & "'!B2:B" & Lr
    With .Range("B2:B" & Lr)
    .Value = Evaluate("IFerror(If(" & Rng & "="""","""",IF(Left(" & Rng & ",Len(""" & Sh & """))=""" & Sh & """," & Rng & ",""" & Shadd & """&" & Rng & ")),"""")")
    .WrapText = False
    .EntireColumn.AutoFit
    End With
End With
Next Sh
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,223,880
Messages
6,175,154
Members
452,615
Latest member
bogeys2birdies

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