Add columns based on a cell value in the row

RickT0962

New Member
Joined
Jan 8, 2024
Messages
2
Office Version
  1. 365
Platform
  1. Windows
I have a spreadsheet that needs indents absed on the number in the cell in column B. So, if column b2 has the number 0 then row 2 has 0 indents between column b&c. If the value in cell b on any given row is 1, 2, 3, 4, etc then I need that many columns indented on that row. The additional columns / indents would always initiate be between columns b&c.
In the table below 6201.20 should have 1 indent row between indent and description while 6201.20.22.10 has 3 indent rows between indent and description.
Any help would be appreciated

numberIndentDescription
62010Men's or boys' overcoats, carcoats, capes, cloaks, anoraks (including ski jackets), windbreakers and similar articles (including padded, sleeveless jackets), other than those of heading 6203:
6201.201Of wool or fine animal hair:
6201.20.112Overcoats, carcoats, capes, cloaks and similar coats:
6201.20.11.103Men's (434)
6201.20.11.203Boys' (434)
2Anoraks (including ski-jackets), windbreakers and similar articles (including padded, sleeveless jackets):
3Recreational performance outerwear:
6201.20.19.004Padded, sleeveless jackets (459)
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
The problem seems to be that if row 2 does not want to have any indents, row3 will put one there anyway ?

Any column inserted on a sheet by default applies to every row, no ?

Sorry if my understanding of the problem is incorrect.
Rob
 
Upvote 0
Hi, welcome to MrExcel forum and happy new year!


In a copy of your sheet, try this macro:


VBA Code:
Sub indent_columns()
  Dim c As Range
 
  For Each c In Range("B2", Range("B" & Rows.Count).End(3))
    If c.Value > 0 Then
      Range(c.Offset(, 1), Cells(c.Row, 2 + c.Value)).Insert xlToRight
    End If
  Next
End Sub

Try and tell me.
Dante Amor
😇
 
Upvote 0
Solution
Hi, welcome to MrExcel forum and happy new year!


In a copy of your sheet, try this macro:


VBA Code:
Sub indent_columns()
  Dim c As Range
 
  For Each c In Range("B2", Range("B" & Rows.Count).End(3))
    If c.Value > 0 Then
      Range(c.Offset(, 1), Cells(c.Row, 2 + c.Value)).Insert xlToRight
    End If
  Next
End Sub

Try and tell me.
Dante Amor
😇
Hi Dante,

This worked like a charm.

Thank you very much.
 
Upvote 0

Forum statistics

Threads
1,215,091
Messages
6,123,062
Members
449,090
Latest member
fragment

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