Deleting rows from list of files
Italia,
Fully qualify your ranges, perhaps along the lines of:
If WorksheetFunction.CountA(newwb.Activesheet.Selecti on.Rows(j)) = 0 Then
newwb.Activesheet.Selection.Rows(j).EntireRow.Dele te
HTH,
Bernie
MS Excel MVP
"italia" wrote in message
oups.com...
I am trying to delete some rows (3 to 200) in all the excel files in
directiory "C:\Excel" using a macro.
Following is the code that I am using. It deletes the rows in the
current worksheet (worksheet where the macro exists). I know I am not
referencing it properly. Please help.
Thanks,
Italia
Sub testme()
Dim i As Long
Dim newwb As Workbook
Dim j As Long
Dim rng As Range
Const myfolder As String = "C:\Excel\"
With Application.FileSearch
.NewSearch
.LookIn = myfolder
.SearchSubFolders = False
.Filename = "*.xls"
If .Execute() 0 Then
For i = 1 To .FoundFiles.Count
Set newwb = Workbooks.Open(Filename:=.FoundFiles(i))
'Deleting code starts here
For j = 2000 To 3 Step -1
If WorksheetFunction.CountA(Selection.Rows(j)) = 0 Then
Sheet1.Rows(1).EntireRow.Delete
MsgBox "Hello"
End If
Next j
'Deleting code ends here
newwb.Close savechanges:=True
Next i
Else
MsgBox "There were no files found."
End If
End With
End Sub
|