1. Welcome to Chandoo.org Forums. Short message for you

    Hi Guest,

    Thanks for joining Chandoo.org forums. We are here to make you awesome in Excel. Before you post your first question, please read this short introduction guide. When posting or responding to questions please remember our values at Chandoo.org are: Humility, Passion, Fun, Awesomeness, Simplicity, Sharing Remember that we have people here for whom English is not there first language and we need to allow for this in our dealings.

  2. 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


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

help to make a maro to delete first 4 row on multiple file

Discussion in 'VBA Macros' started by Maxwolf, Jan 9, 2019.

  1. Maxwolf

    Maxwolf New Member

    ok, i would like a vba code to be able to open multiple file but on each file who is open the first 4 row must be erase.
  2. Stevie

    Stevie Active Member

    Hi Maxwolf,

    the below code should open all workbooks in a folder you specify, 1 by 1 and delete the contents of the first 4 rows before saving and closing that workbook.
    If instead you want to delete the rows entirely, replace 'clearcontents' with 'delete'.

    Please be very sure that you know what this will do and you want to do it before you run code like this, as VBA macros cannot be reversed with such as 'undo'.

    Code (vb):
    Sub del4rows()
    Dim directory As String
    Dim file As String
    Dim wb As Workbook
    directory = "" ' place the path here, such as "C:\Users\Maxwolf\ExcelFiles"
    file = Dir(directory & "\*.xlsx") ' may need to change to .xlsm or .xls as needed
    Do While file <> ""
        Set wb = Workbooks.Open(directory & "\" & file)
        wb.Sheets(1).Rows("1:4").ClearContents ' this assumes you have 1 sheet per workbook, if not, may want to add a loop here
        file = Dir()
    End Sub
    I hope this helps, I used this post as input for the file looping part:

    If this was useful, please click 'Like' on the bottom right!

    Maxwolf and Thomas Kuriakose like this.

Share This Page