Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9
Default Deleting rows from list of files

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

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,441
Default 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



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9
Default Deleting rows from list of files

Thanks Bernie-

I tried your statement but it gives the following error-

Run-time error "438"
Object doesn't support this property or method

Also please advice on a way to make this program faster. Is there a way
we can modify the excel files without opening them?

-Italia

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,441
Default Deleting rows from list of files

Try:

If WorksheetFunction.CountA(Selection.Rows(j)) = 0 Then
Selection.Rows(j).EntireRow.Delete
MsgBox "Hello"
End If

You must open the files to modify them.

HTH,
Bernie
MS Excel MVP

"italia" wrote in message
oups.com...
Thanks Bernie-

I tried your statement but it gives the following error-

Run-time error "438"
Object doesn't support this property or method

Also please advice on a way to make this program faster. Is there a way
we can modify the excel files without opening them?

-Italia



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9
Default Deleting rows from list of files

Thanks again !!!

I tried your suggestion. It does not delete anything unless it is
selected.
Is there a way that I can specify a range (say A3 to G200) and delete
this range?

-Italia



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,441
Default Deleting rows from list of files

Italia,

Change

Selection

to

Range("A1:G200")

HTH,
Bernie
MS Excel MVP
"italia" wrote in message
oups.com...
Thanks again !!!

I tried your suggestion. It does not delete anything unless it is
selected.
Is there a way that I can specify a range (say A3 to G200) and delete
this range?

-Italia



  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9
Default Deleting rows from list of files

I really appreciate your help.

I changed the Selection to Range ("A1:G200"). It is deleting rows from
the current file (worksheet where the macro exists) and not from any
other files.
We need to specify the particular file ("newwb") that we are opening. I
just done know how.

Thanks,
Italia

  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,441
Default Deleting rows from list of files

Italia,

Try:

For j = 200 To 1 Step -1
If WorksheetFunction.CountA(newwb.Worksheets("Sheet1" ) _
.Range("A1:G200").Rows(j)) = 0 Then
newwb.Worksheets("Sheet1").Range("A1:G200") _
.Rows(j).EntireRow.Delete
End If
Next j


HTH,
Bernie
MS Excel MVP

"italia" wrote in message
oups.com...
I really appreciate your help.

I changed the Selection to Range ("A1:G200"). It is deleting rows from
the current file (worksheet where the macro exists) and not from any
other files.
We need to specify the particular file ("newwb") that we are opening. I
just done know how.

Thanks,
Italia



  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9
Default Deleting rows from list of files

It give the following error-
Run-time error '9':
Subscript out of range

-Italia

  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9
Default Deleting rows from list of files

Hi Bernie-

I used the following and it did the trick.

newwb.Sheets(1).Rows("3:200").EntireRow.Delete
Thanks for your help.

Regards,
Italia

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Macro for deleting rows and serialising the remaing rows Srinivasulu Bhattaram Setting up and Configuration of Excel 1 November 12th 08 06:05 PM
Macro for deleting rows and serialising the remaing rows Srinivasulu Bhattaram Excel Worksheet Functions 1 November 12th 08 01:39 PM
deleting various rows of cell data throughout the master list tha. Flip Excel Discussion (Misc queries) 1 August 8th 06 03:14 PM
Deleting Rows Automatically using a Text File List mirdonamy Excel Discussion (Misc queries) 9 January 11th 06 11:11 PM
deleting hidden rows so i can print only the rows showing?????? jenn Excel Worksheet Functions 0 October 6th 05 04:05 PM


All times are GMT +1. The time now is 06:53 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"