How to delete rows below the last row contains a specified string (last match)? by VBA
Hi I'm searching and trying to merge some VBA codes to overcome my problem but haven't been succesfull so far. What i want to do is search specific words in a Column (i.e. "XxX" this a just a part of that cell) and find the last match, and delete the rows below this row by using VBA. I have 278 txt files needs to be converted into xlsx for applying this in batch. I'm adding an example so maybe I'll explain better.
Ağ Teknolojileri Elemanı
Network Technologies Personnel
(Level 5)
12UY0046-5/A1
Occupational Health and Safety, Quality, Work Organization and Professional Development
XxX Explains OHS measures.
XxX Explains environment protection measures.
XxX Defines quality applications.
XxX Explains how to make work organization.
XxX Defines activities needed to be carried out for professional development.
12UY0046-5/A2
Basis of Computer Hardware and Software
XxX Explains the operating logic of the computer.
XxX Explains basic computer use.
XxX Explains the working principles of computer energy hardwares.
XxX Describes the basic features of computer components.
df
fsd
s
gfd
gdfg
dfs
fd
Note: All in "A" column.
For this sample I want the VBA code will find the last cell that contains "XxX" (XxX Describes the basic features of computer components.) and delete all the rows below this row.
I don't know if i can do this to txt files. But nevertheless i couldn't find a way to do it for txt files, i need to change those txt files into xlsx.
Thanks.
excel vba match
add a comment |
Hi I'm searching and trying to merge some VBA codes to overcome my problem but haven't been succesfull so far. What i want to do is search specific words in a Column (i.e. "XxX" this a just a part of that cell) and find the last match, and delete the rows below this row by using VBA. I have 278 txt files needs to be converted into xlsx for applying this in batch. I'm adding an example so maybe I'll explain better.
Ağ Teknolojileri Elemanı
Network Technologies Personnel
(Level 5)
12UY0046-5/A1
Occupational Health and Safety, Quality, Work Organization and Professional Development
XxX Explains OHS measures.
XxX Explains environment protection measures.
XxX Defines quality applications.
XxX Explains how to make work organization.
XxX Defines activities needed to be carried out for professional development.
12UY0046-5/A2
Basis of Computer Hardware and Software
XxX Explains the operating logic of the computer.
XxX Explains basic computer use.
XxX Explains the working principles of computer energy hardwares.
XxX Describes the basic features of computer components.
df
fsd
s
gfd
gdfg
dfs
fd
Note: All in "A" column.
For this sample I want the VBA code will find the last cell that contains "XxX" (XxX Describes the basic features of computer components.) and delete all the rows below this row.
I don't know if i can do this to txt files. But nevertheless i couldn't find a way to do it for txt files, i need to change those txt files into xlsx.
Thanks.
excel vba match
add a comment |
Hi I'm searching and trying to merge some VBA codes to overcome my problem but haven't been succesfull so far. What i want to do is search specific words in a Column (i.e. "XxX" this a just a part of that cell) and find the last match, and delete the rows below this row by using VBA. I have 278 txt files needs to be converted into xlsx for applying this in batch. I'm adding an example so maybe I'll explain better.
Ağ Teknolojileri Elemanı
Network Technologies Personnel
(Level 5)
12UY0046-5/A1
Occupational Health and Safety, Quality, Work Organization and Professional Development
XxX Explains OHS measures.
XxX Explains environment protection measures.
XxX Defines quality applications.
XxX Explains how to make work organization.
XxX Defines activities needed to be carried out for professional development.
12UY0046-5/A2
Basis of Computer Hardware and Software
XxX Explains the operating logic of the computer.
XxX Explains basic computer use.
XxX Explains the working principles of computer energy hardwares.
XxX Describes the basic features of computer components.
df
fsd
s
gfd
gdfg
dfs
fd
Note: All in "A" column.
For this sample I want the VBA code will find the last cell that contains "XxX" (XxX Describes the basic features of computer components.) and delete all the rows below this row.
I don't know if i can do this to txt files. But nevertheless i couldn't find a way to do it for txt files, i need to change those txt files into xlsx.
Thanks.
excel vba match
Hi I'm searching and trying to merge some VBA codes to overcome my problem but haven't been succesfull so far. What i want to do is search specific words in a Column (i.e. "XxX" this a just a part of that cell) and find the last match, and delete the rows below this row by using VBA. I have 278 txt files needs to be converted into xlsx for applying this in batch. I'm adding an example so maybe I'll explain better.
Ağ Teknolojileri Elemanı
Network Technologies Personnel
(Level 5)
12UY0046-5/A1
Occupational Health and Safety, Quality, Work Organization and Professional Development
XxX Explains OHS measures.
XxX Explains environment protection measures.
XxX Defines quality applications.
XxX Explains how to make work organization.
XxX Defines activities needed to be carried out for professional development.
12UY0046-5/A2
Basis of Computer Hardware and Software
XxX Explains the operating logic of the computer.
XxX Explains basic computer use.
XxX Explains the working principles of computer energy hardwares.
XxX Describes the basic features of computer components.
df
fsd
s
gfd
gdfg
dfs
fd
Note: All in "A" column.
For this sample I want the VBA code will find the last cell that contains "XxX" (XxX Describes the basic features of computer components.) and delete all the rows below this row.
I don't know if i can do this to txt files. But nevertheless i couldn't find a way to do it for txt files, i need to change those txt files into xlsx.
Thanks.
excel vba match
excel vba match
asked Nov 25 '18 at 12:16
TunaTuna
82
82
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Try:
Option Explicit
Sub delAfter()
Const sFind As String = "XxX"
Dim WS As Worksheet, R As Range
'Find last sFind
Set WS = Worksheets("sheet2") 'or ActiveSheet or whatever
With WS.Cells
Set R = .Find(what:=sFind, _
after:=.Item(1, 1), _
LookIn:=xlValues, _
lookat:=xlPart, _
searchorder:=xlByRows, _
searchdirection:=xlPrevious, _
MatchCase:=True)
If Not R Is Nothing Then
Set R = WS.Range(R.Offset(1, 0), .Cells(.Rows.Count, R.Column).End(xlUp))
R.EntireRow.Delete 'deletes entire row. If just want a single column, adjust
End If
End With
End Sub
Will delete entire rows after finding last XxX
.
If you examine the code, you will see we start in the first row, but the searchorder is xlprevious so we will be searching from the bottom.
If you need to restrict it to a specific column, just change the range being searched.
This worked like a charm. Thanks for supporting Ron.
– Tuna
Nov 25 '18 at 13:26
add a comment |
Ok,
This code should work for strings containing XxX in columns a
Option Explicit
Sub Delete()
Dim wb As Workbook
Dim ws As Worksheet
Dim Lastrow As Long, i As Long, RowNum As Long
Dim str As String
Dim r As Range, Cell As Range
Set wb = ThisWorkbook
Set ws = wb.Sheets("Blad1")
str = "XxX"
Lastrow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
Set r = ws.Range(ws.Cells(1, 1), ws.Cells(Lastrow, 1))
For Each Cell In r
If Cell.Value Like "*" & str & "*" Then
i = Cell.Row
Exit For
End If
Next Cell
For RowNum = i + 1 To Lastrow
ws.Rows(i + 1).Delete
Next RowNum
End Sub
This deletes all rows until 6. row. 1 to 6 rows are there. There is something wrong here. This vba finds the first match not the last one
– Tuna
Nov 25 '18 at 12:40
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53467343%2fhow-to-delete-rows-below-the-last-row-contains-a-specified-string-last-match%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Try:
Option Explicit
Sub delAfter()
Const sFind As String = "XxX"
Dim WS As Worksheet, R As Range
'Find last sFind
Set WS = Worksheets("sheet2") 'or ActiveSheet or whatever
With WS.Cells
Set R = .Find(what:=sFind, _
after:=.Item(1, 1), _
LookIn:=xlValues, _
lookat:=xlPart, _
searchorder:=xlByRows, _
searchdirection:=xlPrevious, _
MatchCase:=True)
If Not R Is Nothing Then
Set R = WS.Range(R.Offset(1, 0), .Cells(.Rows.Count, R.Column).End(xlUp))
R.EntireRow.Delete 'deletes entire row. If just want a single column, adjust
End If
End With
End Sub
Will delete entire rows after finding last XxX
.
If you examine the code, you will see we start in the first row, but the searchorder is xlprevious so we will be searching from the bottom.
If you need to restrict it to a specific column, just change the range being searched.
This worked like a charm. Thanks for supporting Ron.
– Tuna
Nov 25 '18 at 13:26
add a comment |
Try:
Option Explicit
Sub delAfter()
Const sFind As String = "XxX"
Dim WS As Worksheet, R As Range
'Find last sFind
Set WS = Worksheets("sheet2") 'or ActiveSheet or whatever
With WS.Cells
Set R = .Find(what:=sFind, _
after:=.Item(1, 1), _
LookIn:=xlValues, _
lookat:=xlPart, _
searchorder:=xlByRows, _
searchdirection:=xlPrevious, _
MatchCase:=True)
If Not R Is Nothing Then
Set R = WS.Range(R.Offset(1, 0), .Cells(.Rows.Count, R.Column).End(xlUp))
R.EntireRow.Delete 'deletes entire row. If just want a single column, adjust
End If
End With
End Sub
Will delete entire rows after finding last XxX
.
If you examine the code, you will see we start in the first row, but the searchorder is xlprevious so we will be searching from the bottom.
If you need to restrict it to a specific column, just change the range being searched.
This worked like a charm. Thanks for supporting Ron.
– Tuna
Nov 25 '18 at 13:26
add a comment |
Try:
Option Explicit
Sub delAfter()
Const sFind As String = "XxX"
Dim WS As Worksheet, R As Range
'Find last sFind
Set WS = Worksheets("sheet2") 'or ActiveSheet or whatever
With WS.Cells
Set R = .Find(what:=sFind, _
after:=.Item(1, 1), _
LookIn:=xlValues, _
lookat:=xlPart, _
searchorder:=xlByRows, _
searchdirection:=xlPrevious, _
MatchCase:=True)
If Not R Is Nothing Then
Set R = WS.Range(R.Offset(1, 0), .Cells(.Rows.Count, R.Column).End(xlUp))
R.EntireRow.Delete 'deletes entire row. If just want a single column, adjust
End If
End With
End Sub
Will delete entire rows after finding last XxX
.
If you examine the code, you will see we start in the first row, but the searchorder is xlprevious so we will be searching from the bottom.
If you need to restrict it to a specific column, just change the range being searched.
Try:
Option Explicit
Sub delAfter()
Const sFind As String = "XxX"
Dim WS As Worksheet, R As Range
'Find last sFind
Set WS = Worksheets("sheet2") 'or ActiveSheet or whatever
With WS.Cells
Set R = .Find(what:=sFind, _
after:=.Item(1, 1), _
LookIn:=xlValues, _
lookat:=xlPart, _
searchorder:=xlByRows, _
searchdirection:=xlPrevious, _
MatchCase:=True)
If Not R Is Nothing Then
Set R = WS.Range(R.Offset(1, 0), .Cells(.Rows.Count, R.Column).End(xlUp))
R.EntireRow.Delete 'deletes entire row. If just want a single column, adjust
End If
End With
End Sub
Will delete entire rows after finding last XxX
.
If you examine the code, you will see we start in the first row, but the searchorder is xlprevious so we will be searching from the bottom.
If you need to restrict it to a specific column, just change the range being searched.
answered Nov 25 '18 at 13:03
Ron RosenfeldRon Rosenfeld
23.8k41640
23.8k41640
This worked like a charm. Thanks for supporting Ron.
– Tuna
Nov 25 '18 at 13:26
add a comment |
This worked like a charm. Thanks for supporting Ron.
– Tuna
Nov 25 '18 at 13:26
This worked like a charm. Thanks for supporting Ron.
– Tuna
Nov 25 '18 at 13:26
This worked like a charm. Thanks for supporting Ron.
– Tuna
Nov 25 '18 at 13:26
add a comment |
Ok,
This code should work for strings containing XxX in columns a
Option Explicit
Sub Delete()
Dim wb As Workbook
Dim ws As Worksheet
Dim Lastrow As Long, i As Long, RowNum As Long
Dim str As String
Dim r As Range, Cell As Range
Set wb = ThisWorkbook
Set ws = wb.Sheets("Blad1")
str = "XxX"
Lastrow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
Set r = ws.Range(ws.Cells(1, 1), ws.Cells(Lastrow, 1))
For Each Cell In r
If Cell.Value Like "*" & str & "*" Then
i = Cell.Row
Exit For
End If
Next Cell
For RowNum = i + 1 To Lastrow
ws.Rows(i + 1).Delete
Next RowNum
End Sub
This deletes all rows until 6. row. 1 to 6 rows are there. There is something wrong here. This vba finds the first match not the last one
– Tuna
Nov 25 '18 at 12:40
add a comment |
Ok,
This code should work for strings containing XxX in columns a
Option Explicit
Sub Delete()
Dim wb As Workbook
Dim ws As Worksheet
Dim Lastrow As Long, i As Long, RowNum As Long
Dim str As String
Dim r As Range, Cell As Range
Set wb = ThisWorkbook
Set ws = wb.Sheets("Blad1")
str = "XxX"
Lastrow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
Set r = ws.Range(ws.Cells(1, 1), ws.Cells(Lastrow, 1))
For Each Cell In r
If Cell.Value Like "*" & str & "*" Then
i = Cell.Row
Exit For
End If
Next Cell
For RowNum = i + 1 To Lastrow
ws.Rows(i + 1).Delete
Next RowNum
End Sub
This deletes all rows until 6. row. 1 to 6 rows are there. There is something wrong here. This vba finds the first match not the last one
– Tuna
Nov 25 '18 at 12:40
add a comment |
Ok,
This code should work for strings containing XxX in columns a
Option Explicit
Sub Delete()
Dim wb As Workbook
Dim ws As Worksheet
Dim Lastrow As Long, i As Long, RowNum As Long
Dim str As String
Dim r As Range, Cell As Range
Set wb = ThisWorkbook
Set ws = wb.Sheets("Blad1")
str = "XxX"
Lastrow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
Set r = ws.Range(ws.Cells(1, 1), ws.Cells(Lastrow, 1))
For Each Cell In r
If Cell.Value Like "*" & str & "*" Then
i = Cell.Row
Exit For
End If
Next Cell
For RowNum = i + 1 To Lastrow
ws.Rows(i + 1).Delete
Next RowNum
End Sub
Ok,
This code should work for strings containing XxX in columns a
Option Explicit
Sub Delete()
Dim wb As Workbook
Dim ws As Worksheet
Dim Lastrow As Long, i As Long, RowNum As Long
Dim str As String
Dim r As Range, Cell As Range
Set wb = ThisWorkbook
Set ws = wb.Sheets("Blad1")
str = "XxX"
Lastrow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
Set r = ws.Range(ws.Cells(1, 1), ws.Cells(Lastrow, 1))
For Each Cell In r
If Cell.Value Like "*" & str & "*" Then
i = Cell.Row
Exit For
End If
Next Cell
For RowNum = i + 1 To Lastrow
ws.Rows(i + 1).Delete
Next RowNum
End Sub
answered Nov 25 '18 at 12:32
LambikLambik
500413
500413
This deletes all rows until 6. row. 1 to 6 rows are there. There is something wrong here. This vba finds the first match not the last one
– Tuna
Nov 25 '18 at 12:40
add a comment |
This deletes all rows until 6. row. 1 to 6 rows are there. There is something wrong here. This vba finds the first match not the last one
– Tuna
Nov 25 '18 at 12:40
This deletes all rows until 6. row. 1 to 6 rows are there. There is something wrong here. This vba finds the first match not the last one
– Tuna
Nov 25 '18 at 12:40
This deletes all rows until 6. row. 1 to 6 rows are there. There is something wrong here. This vba finds the first match not the last one
– Tuna
Nov 25 '18 at 12:40
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53467343%2fhow-to-delete-rows-below-the-last-row-contains-a-specified-string-last-match%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown