VBA insert n new columns and calculate difference of previous n-2 columns

Bering

Board Regular
Joined
Aug 22, 2018
Messages
185
Office Version
  1. 2016
Platform
  1. Windows
Hello,

I have a workbook with 2 worksheets: in sheet1, column B, users enter a list of items (the number of item can vary but the last item is always called Total).
For each item in that list, a macro adds a corresponding column in sheet2 (headers = item) plus an additional one, unrelated to the list. The macro will also insert some formulae in each new column in sheet2 (row 6 to 53).

Example: in sheet2 there are 20 columns, the user enters 4 items in sheet1, the macro will create 5 additional columns, hence 25 in total.
For the last item in the list in sheet1, which is always called Total, I would like to add in the corresponding column in sheet2, a formula that calculates the difference of the n- 2 previous columns (3 in this example): item Total would correspond to Column 24 and I would like to populate the range row 6 to 53 with a formula: Column 21 - Column 22 - Column 23

I am not sure how I could achieve this.

Many thanks in advance
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Assuming the items on sheet1 start in cell B2 and the headings on sheet2 are in row 5, try this:

VBA Code:
Sub insert_new_columns()
  Dim f As Range, lc As Long, n As Long
  Dim sh1 As Worksheet, sh2 As Worksheet
  
  Set sh1 = Sheets("Sheet1")
  Set sh2 = Sheets("Sheet2")
  
  Set f = sh1.Range("B:B").Find("Total", , xlValues, xlWhole, , , False)
  If Not f Is Nothing Then
    With sh1.Range("B2", f)
      .Copy
      n = .Rows.Count - 1
    End With
    If n > 0 Then
      lc = sh2.Cells(5, Columns.Count).End(1).Column + 1
      sh2.Cells(5, lc).PasteSpecial xlPasteValues, , , True
      With sh2.Cells(6, lc + n)
        If n = 1 Then
          .Formula = "=" & .Offset(0, -n).Address(0, 0)
        Else
          .Formula = "=" & .Offset(0, -n).Address(0, 0) & "-SUM(" & .Offset(0, -n + 1).Address(0, 0) & ":" & .Offset(0, -1).Address(0, 0) & ")"
        End If
        .Copy sh2.Range(sh2.Cells(6, lc + n), sh2.Cells(53, lc + n))
      End With
    Else
      MsgBox "No items"
    End If
  Else
    MsgBox "The item 'Total' does not exist"
  End If
End Sub
 
Upvote 0
Thank you sooo much Dante, this works perfectly (y) (y) (y)


Assuming the items on sheet1 start in cell B2 and the headings on sheet2 are in row 5, try this:

VBA Code:
Sub insert_new_columns()
  Dim f As Range, lc As Long, n As Long
  Dim sh1 As Worksheet, sh2 As Worksheet

  Set sh1 = Sheets("Sheet1")
  Set sh2 = Sheets("Sheet2")

  Set f = sh1.Range("B:B").Find("Total", , xlValues, xlWhole, , , False)
  If Not f Is Nothing Then
    With sh1.Range("B2", f)
      .Copy
      n = .Rows.Count - 1
    End With
    If n > 0 Then
      lc = sh2.Cells(5, Columns.Count).End(1).Column + 1
      sh2.Cells(5, lc).PasteSpecial xlPasteValues, , , True
      With sh2.Cells(6, lc + n)
        If n = 1 Then
          .Formula = "=" & .Offset(0, -n).Address(0, 0)
        Else
          .Formula = "=" & .Offset(0, -n).Address(0, 0) & "-SUM(" & .Offset(0, -n + 1).Address(0, 0) & ":" & .Offset(0, -1).Address(0, 0) & ")"
        End If
        .Copy sh2.Range(sh2.Cells(6, lc + n), sh2.Cells(53, lc + n))
      End With
    Else
      MsgBox "No items"
    End If
  Else
    MsgBox "The item 'Total' does not exist"
  End If
End Sub
 
Last edited by a moderator:
Upvote 0
Hello Dante,

Just a question if you don't mind: suppose I need to insert the formula =P*$E in range 5 - last row in column P; How would that translate using the notation Offset().Address() in your code above?

Many thanks
 
Upvote 0
P*E in P would produce a circular reference error.
It would always be P*E, because it may be that P is empty. I don't quite understand what it would be like, since the columns are dynamic, it depends on the number of items, so P could be empty or with an item or with "Total"
 
Upvote 0
Apologies, I meant to say P$4*E (I have an amount in row P4 that I need to allocate by a ratio in column E from row 5 to last row)

P*E in P would produce a circular reference error.
It would always be P*E, because it may be that P is empty. I don't quite understand what it would be like, since the columns are dynamic, it depends on the number of items, so P could be empty or with an item or with "Total"
 
Upvote 0
Sorry, but I still don't understand. This is very clear to you because you are seeing it and you know your data.
It would help if you put an image explaining where you want the formula.
 
Upvote 0
Example.JPG



I hope this clarifies, thank you
 
Upvote 0
Always in column P? Or in the first column where the macro begins to put items?
Only in the P or also in the Q and in the R?
 
Upvote 0
all the columns created by the macro actually, so yes P, Q and R in this example.

Many thanks




Always in column P? Or in the first column where the macro begins to put items?
Only in the P or also in the Q and in the R?
 
Upvote 0

Forum statistics

Threads
1,214,606
Messages
6,120,478
Members
448,967
Latest member
visheshkotha

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