insert new column and autofill name repeatedly based on sheet name

tubrak

Board Regular
Joined
May 30, 2021
Messages
218
Office Version
  1. 2019
Platform
  1. Windows
Hello
I have multiple sheets in file , so what I want to insert new column after column date and fill name based on sheet name & repeat name up to last row , but don't insert column every time run the macro if it has already inserted column should not insert again , but if I change sheet name , then just change name for column has already inserted .

assss.xlsm
ABCDE
1DATEINVOICE NUMBERDEBIT CUCREDIT CUBALANCE
201/01/2023INV100221000010000
302/01/2023INV100231500025000
403/01/2023INV1002420000200043000
504/01/2023INV1002525000200066000
605/02/2023INV10026200068000
706/02/2023INV100271000078000
807/02/2023CASH100077000
908/02/2023BANK100076000
1009/02/2023CASH800068000
1110/02/2023CASH120066800
1211/02/2023INV10028234100066034
1312/02/2023INV100291230078334
AA
Cell Formulas
RangeFormula
E2E2=C2
E3:E13E3=E2+C3-D3


should be

assss.xlsm
ABCDEF
1DATENAMEINVOICE NUMBERDEBIT CUCREDIT CUBALANCE
201/01/2023AAINV100221000010000
302/01/2023AAINV100231500025000
403/01/2023AAINV1002420000200043000
504/01/2023AAINV1002525000200066000
605/02/2023AAINV10026200068000
706/02/2023AAINV100271000078000
807/02/2023AACASH100077000
908/02/2023AABANK100076000
1009/02/2023AACASH800068000
1110/02/2023AACASH120066800
1211/02/2023AAINV10028234100066034
1312/02/2023AAINV100291230078334
AA
Cell Formulas
RangeFormula
F2F2=D2
F3:F13F3=F2+D3-E3


another example

assss.xlsm
ABCDE
1DATEINVOICE NUMBERDEBIT CUCREDIT CUBALANCE
201/01/2023INV10011000010000
302/01/2023INV10111500025000
403/01/2023CASH200023000
504/01/2023CASH200021000
605/02/2023INV1002200023000
706/02/2023INV10031000033000
807/02/2023BANK100032000
908/02/2023BANK100031000
1009/02/2023BANK800023000
1110/02/2023BANK120021800
1211/02/2023INV10081200100022000
1312/02/2023INV100922000
AS
Cell Formulas
RangeFormula
E2E2=C2
E3:E13E3=E2+C3-D3



should be
assss.xlsm
ABCDEF
1DATENAMEINVOICE NUMBERDEBIT CUCREDIT CUBALANCE
201/01/2023ASINV10011000010000
302/01/2023ASINV10111500025000
403/01/2023ASCASH200023000
504/01/2023ASCASH200021000
605/02/2023ASINV1002200023000
706/02/2023ASINV10031000033000
807/02/2023ASBANK100032000
908/02/2023ASBANK100031000
1009/02/2023ASBANK800023000
1110/02/2023ASBANK120021800
1211/02/2023ASINV10081200100022000
1312/02/2023ASINV100922000
AS
Cell Formulas
RangeFormula
F2F2=D2
F3:F13F3=F2+D3-E3

and if I change sheet name for AS sheet to MA sheet
then will be
assss.xlsm
ABCDEF
1DATENAMEINVOICE NUMBERDEBIT CUCREDIT CUBALANCE
201/01/2023MAINV10011000010000
302/01/2023MAINV10111500025000
403/01/2023MACASH200023000
504/01/2023MACASH200021000
605/02/2023MAINV1002200023000
706/02/2023MAINV10031000033000
807/02/2023MABANK100032000
908/02/2023MABANK100031000
1009/02/2023MABANK800023000
1110/02/2023MABANK120021800
1211/02/2023MAINV10081200100022000
1312/02/2023MAINV100922000
MA
Cell Formulas
RangeFormula
F2F2=D2
F3:F13F3=F2+D3-E3
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Try this:

VBA Code:
Sub insertnewcolumn()
  Dim sh As Worksheet
  
  For Each sh In Sheets
    If sh.Range("A1").Value = "DATE" Then
      If sh.Range("B1").Value <> "NAME" Then
        sh.Range("B:B").Insert
        sh.Range("B1").Value = "NAME"
      End If
      sh.Range("B2:B" & sh.Range("A" & Rows.Count).End(3).Row).Value = sh.Name
    End If
  Next
End Sub

--------------
Let me know the result and I'll get back to you as soon as I can.
Cordially
Dante Amor
--------------​
 
Upvote 1
Solution

Forum statistics

Threads
1,223,909
Messages
6,175,310
Members
452,634
Latest member
cpostell

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