How to delete 4 lines from each workbook in a specific folder-vba code required

kishorvimal

New Member
Joined
May 28, 2011
Messages
25
Hi,

Can anyone help me with vba code to delete starting 4 lines in each workbook in a folder.

Thanks in advance
vimal
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Hi vimal,

Here is code that deletes the first 4 rows of each worksheet of each Excel workbook in the current working folder.

Code:
Sub OpenAll()

'  This macro opens all files in the working (default) directory,
'  one at a time, then loops through all worksheets in the workbook
'  deleting the first four rows, and then closes each file.

   Dim Filename      As String
   Dim GoAhead       As Variant
   Dim WS            As Worksheet
   
   'Look for all files ending with .xls
   'Can include entire path if desired. This example assumes working
   'directory so no path specified.
   Filename = Dir("*.xls*")
   
   GoAhead = MsgBox("This macro will delete the first 4 rows of every workbook" & vbLf & _
                    "in " & Application.DefaultFilePath & ", starting with: " & vbLf & _
                    Filename & ". Are you sure?", vbYesNo, "Important Warning")
   If GoAhead = vbNo Then Exit Sub
   
   Application.DisplayAlerts = False
   
   Do While Filename <> ""
   
      Workbooks.Open Filename
      Application.StatusBar = Filename & " opened."
      
      For Each WS In Worksheets
         WS.Rows("1:4").Delete
      Next WS
      
      ActiveWorkbook.Close True
      Application.StatusBar = Filename & " closed."
      
      'read next filename
      Filename = Dir()
   Loop
   
   Application.DisplayAlerts = False
   Application.StatusBar = False

End Sub

To set the current working folder to the folder you want to process you can simply use File > Open, then use the Open dialog to browse to the folder you want, then click the Cancel button, i.e., you don't have to actually open a file in that folder to set it as the working folder.

Please use this macro with caution. It would be very easy to accidentally apply it to the wrong folder and wipe out the first four rows of a lot of worksheets you didn't intend to modify. It is for this reason that I put an "Are you sure?" popup in the macro.

Keep Excelling.

Damon
 
Upvote 0

Forum statistics

Threads
1,224,583
Messages
6,179,681
Members
452,937
Latest member
Bhg1984

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