Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

macro for deleting list of items from spreadsheet

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
I am new to this, and trying to write a short program to take a text file input, with lines of text, and delete all of those lines from my spreadsheet. for example, if i had a spreadsheet with the following data in a column:

1
2
3
4
5

and a .txt file with the following contents:

1
2
5

I'd want the program to delete the rows in the spreadhseet containing 1, 2 and 5, leaving only the rows that had contained 3 and 4.

this is what i got, which doesn't work:

sub macro1()
dim data as string
dim x as long

open "c:\data.txt" for input as #1
while not EOF(1)
line input #1, data
for x = 1 to 10 'check 10 rows
if cells (x,1) = data then
selection.rows(x).entirerow.delete
end if
next

wend
close #1
end sub


can anyone help me to get this to work? thanks for any help!

-Mike
 
Im not sure what "Selection" is , or why Option Explicit
doesn't catch it, but I think thats where your problem lies.
This will do what your looking for:

Public Sub test()

Dim data As String
Dim x As Long
Dim iFF As Integer
'good practice to use freefile
iFF = FreeFile

Open "c:\data.txt" For Input As iFF

While Not EOF(iFF)
Line Input #iFF, data
With Sheet1 ' sheet name here
For x = 1 To .UsedRange.Rows.Count ' check entire sheet?
If Cells(x, 1) = data Then .Rows(x).EntireRow.Delete
Next x
End With ' Sheet1
Wend

Close #iFF

End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top