0

I have a task to write an Excel macro to match a specific import template for a customer of mine. I have a large quantity of data and only some of it is needed. I have managed to write the VB to eliminate columns, rows, etc to fit the criteria I need.

What I do not know how to do, is add a row in the row 1 spot that will add data to each of the cells in the correct order.

For example I would want to add "OrderID" "CompanyName" "Contact" and "Address1" in successive cells across the row. Is this possible?

Also I would want to add new columns into the sheet, is there a way to specify where I add them?

The code I am currently using:

Sub Macro1()
'
'
' Keyboard Shortcut: Ctrl+a

Dim i As Long
Dim LastRow As Long

LastRow = ActiveCell.SpecialCells(xlCellTypeLastCell).Row

For i = LastRow To 1 Step -1
    If Cells(i, 1).Value <> "CUST" Then
        Rows(i).Delete
    End If
Next i

Columns(1).EntireColumn.Delete
Columns(2).EntireColumn.Delete
Columns(3).EntireColumn.Delete
Columns(5).EntireColumn.Delete
Columns(5).EntireColumn.Delete
Columns(5).EntireColumn.Delete
Columns(5).EntireColumn.Delete
Columns(5).EntireColumn.Delete
Columns(5).EntireColumn.Delete
Columns(5).EntireColumn.Delete
Columns(4).EntireColumn.Delete
Columns(4).EntireColumn.Delete

End Sub
Luuklag
  • 3,897
  • 11
  • 38
  • 57
ForrestFairway
  • 133
  • 1
  • 11
  • 1
    Have you tried using Excel's macro recorder to record the steps needed to insert the values you want in the first row? Once you do that, it would be an easy step to tidy the code up so that it doesn't use `Select`, or you could just continue to use the code with the `Select` statements still there. – YowE3K Jan 26 '17 at 18:26
  • While I actively suggest to avoid using [Active & Select](http://stackoverflow.com/questions/10714251/how-to-avoid-using-select-in-excel-vba-macros), I often reference [This](https://www.mrexcel.com/forum/excel-questions/77371-visual-basic-applications-insert-row-colum.html#post374234) when trying to insert new rows and columns (I always forget, so I have the link favorited!) – PartyHatPanda Jan 26 '17 at 19:40
  • Solved, thank you! – ForrestFairway Jan 27 '17 at 17:50

0 Answers0