Quantcast
Channel: Active questions tagged excel - Stack Overflow
Viewing all articles
Browse latest Browse all 88066

Search column headers and insert new column if header does not already exist using Excel VBA

$
0
0

I have a spreadsheet that is updated regularly. The user will update two columns on sheet(create) with container type (this is the header name) and the quantity, which will be transferred to sheet(Tracking). I am trying to figure out how to search sheet2(Tracking for existing headers (container types), if found then quantity will be updated within that column for the next available row. If header is not found, therefore a new column is added to the right with that new header name, as well as updating the quantity. enter image description hereenter image description here

enter image description here

I did find some good example such as the below. However not sure how to apply it. Maybe there could be a way to loop it to search the headers.

Sub TrackR()

Dim cl As Range

    For Each cl In Range("1:1")
        If cl = sheets(“Create”).range(“J11:J36”) Then
           cl.EntireColumn.Insert Shift:=xlToRight
        End If

        cl.Offset(0, 1) = "New Conatainer Name"
    Next cl

Application.ScreenUpdating = False
  Sheets("Tracking").Range("A"& Rows.Count).End(xlUp).Offset(1, 0) = Date

 'Trailer No.
 Sheets("Create").Range("L8").Copy
 Sheets("Tracking").Range("B"& Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValues
 Application.CutCopyMode = False

 'total container qty
 Sheets("Create").Range("G43").Copy
 Sheets("Tracking").Range("D"& Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValues
 Application.CutCopyMode = False

 'Supplier
 Sheets("Create").Range("K4").Copy
 Sheets("Tracking").Range("C"& Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValues
 Application.CutCopyMode = False

    'quantities
 Sheets("Create").Range("L11").Copy
 Sheets("Tracking").Range("E"& Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValues
 Application.CutCopyMode = False

 Sheets("Create").Range("L12").Copy
 Sheets("Tracking").Range("F"& Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValues
 Application.CutCopyMode = False

 Sheets("Create").Range("L13").Copy
 Sheets("Tracking").Range("G"& Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValues
 Application.CutCopyMode = False

 Sheets("Create").Range("L14").Copy
 Sheets("Tracking").Range("H"& Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValues
 Application.CutCopyMode = False

     Sheets("Create").Range("L15").Copy
 Sheets("Tracking").Range("I"& Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValues
 Application.CutCopyMode = False

Application.ScreenUpdating = False

End Sub

Viewing all articles
Browse latest Browse all 88066

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>