• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Formatting the excel with VBA

Hi
I want below formatting points in a single macro.. Could you please help me on this..

  • Select all Rows starting at row 2 to the bottom
  • Delete all rows that do not have “20” in column D
  • Delete Columns H-AD
  • Copy & Paste the following headers starting with the first date field (new column H)
DoBDoHEntry DateTerm DateDisable DateDeath DateOfficer Staus CdOwn%YTD OverrideHrsSrceLev Elig OptCdDivTermRehire

  • Delete the following columns:
    • OfficerStausCd
    • Own%
    • YTDOverrideHrs
    • SrceLevEligOptCd
    • Div
  • Save changes
 

Attachments

  • Test Sample.xlsx
    41.3 KB · Views: 2
Code:
Option Explicit

Sub foo()
    Dim i As Long, lr As Long
    lr = Range("A" & Rows.Count).End(xlUp).Row
    Dim rng As Range
    Set rng = Sheets("Sheet1").Range("A1:M1")
    Application.ScreenUpdating = False
    For i = lr To 2 Step -1
        If Range("D" & i) <> 20 Then
            Range("D" & i).EntireRow.Delete
        End If
    Next i

    Range("H1:AD1").EntireColumn.Delete
    rng.Copy Range("H1")
    Range("N1:R1").EntireColumn.Delete
    Application.ScreenUpdating = True
    MsgBox "complete"
End Sub

Assumes the Header you want inserted is sitting on a sheet named Sheet1 in the range area A1:M1
 
Back
Top