0

I want to select a lot of rows in Excel in order to delete them, and doing this manually literaly takes hours. I have the specific row numbers in a .txt file with a number on each line. Like this:

1
5
8
9

Is there some cool way to tell Excel to select/delete these lines automatically?

Example:
http://i.imgur.com/ccQegoC.png

8
  • 2
    What is the condition that must be met to delete a row?
    – Eric F
    Commented Nov 11, 2015 at 12:16
  • There isn't any logical condition. The row is deleted due to a specific vendor not working with us anymore - seemingly random rows. So I have the specific rows (1, 5, 8, 9 as in the example) in a text file and need to select/delete these rows.
    – Aphex
    Commented Nov 11, 2015 at 12:30
  • Selecting the rows, right clicking, and selecting delete is the fastest way to delete rows in excel. You could supposedly use VBA but there would be no speed increase.
    – Eric F
    Commented Nov 11, 2015 at 12:32
  • If you want to delete multiple rows for the same vendor then that's a different story and you can use VBA to do so, to find all occurrences.
    – Eric F
    Commented Nov 11, 2015 at 12:33
  • Let's say I have a document with 2000 rows of data and I need to delete 500 of them in random locations - it can easily take 20-30 minutes if not more. If I could somehow run a e.g. VBA script on the seperate text file selecting the specific lines, we're talking about seconds of work. Do you know if Kutools can be in use?
    – Aphex
    Commented Nov 11, 2015 at 12:36

1 Answer 1

0

Here is an example to get you started on the VBA side of things. I created a workbook that has two fields, Vendor and the File location. I also created a VBA sub procedure that will open the text file and replace any line that = the vendor number with a return carriage on the end of it and replace that with nothing (""):

Sub Remove_line_item()
Dim myFile As String, text As String, textline As String, posLat As Integer, posLong As Integer
myFile = Range("B2").Value
myFile = Application.GetOpenFilename()
Open myFile For Input As #1
Do Until EOF(1)
Line Input #1, textline
text = text & textline
Loop
text = Replace(text, Range("A2").Value & ChrW(14), "")

myFile.Write text
myFile.Close
End Sub

enter image description here

I only used one file path at a time but you could go one step further and go through all text files in a directory too. This could be a good start for you though hopefully.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .