VBA to add Headers.

XcelNoobster

New Member
Joined
Jun 7, 2022
Messages
40
So I have the following report:
Screenshot 2023-05-16 090327.png


I would like to create a VBA macro that when run does the following:
- removes the first 4 rows,
- adds new column "Item Number" and "Description" followed by all headers in row 7(ex. Item Number, Description, Level, Part Type/ Document Type, etc...)
- Adds that sections Item Number and Description to all the entries in that section(for example, Item Number: N711 should be added to the the rows from 8 - 13 THEN Item Number N6501-5 to rows 17-20:

This is what it should look like at the end:

Screenshot 2023-05-16 092633.png
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Hello @XcelNoobster. Thanks for posting on MrExcel board.


I give you the macro, just support me with 3 data that you must put in the macro.
1) The reference text where the items begin: "Item Number:" In this line:​
sItem = "Item Number:"
2) The name of the sheet where you have the data. In this line:​
With Sheets("Report")
3) And one last piece of information, the output will be in a sheet, so you will have to create a sheet to put the output in and adjust the name on this line:​
With Sheets("Result")


Try this macro:
VBA Code:
Sub AddHeaders()
  Dim a As Variant, b As Variant
  Dim i As Long, j As Long, k As Long, lr As Long, lc As Long
  Dim f As Range
  Dim sItem As String, iNum As String, iDes As String
  
  sItem = "Item Number:"
  
  With Sheets("Report")
    Set f = .Range("A:A").Find(sItem, , xlValues, xlWhole, , , False)
    If Not f Is Nothing Then
      lr = .Range("A" & Rows.Count).End(3).Row
      lc = .Cells.Find("*", , xlValues, xlPart, xlByColumns, xlPrevious).Column
      a = .Range("A" & f.Row, .Cells(lr, lc)).Value
    Else
      MsgBox "The text does not exist: " & sItem
      Exit Sub
    End If
  End With
  
  ReDim b(1 To UBound(a, 1), 1 To UBound(a, 2) + 2)
  
  b(1, 1) = "Item Number"
  b(1, 2) = "Description"
  For j = 1 To UBound(a, 2)
    b(1, j + 2) = a(3, j)
  Next
  k = 1
  For i = 1 To UBound(a, 1)
    If a(i, 1) = sItem Then
      iNum = a(i, 2)
      iDes = a(i + 1, 2)
      i = i + 3
    End If
    k = k + 1
    b(k, 1) = iNum
    b(k, 2) = iDes
    For j = 1 To UBound(a, 2)
      b(k, j + 2) = a(i, j)
    Next
  Next
  
  With Sheets("Result")
    .Cells.ClearContents
    .Range("A1").Resize(UBound(b, 1), UBound(b, 2)).Value = b
  End With
End Sub


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

Forum statistics

Threads
1,214,833
Messages
6,121,864
Members
449,052
Latest member
Fuddy_Duddy

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