Return lines Values between 2 lines with a specific sentence via FIND
I have this in Sheet 1 column A, with a lot of other text before and after :
I want to copy all what is betwen cell with :
Sponsor de l'Indice Marché Site Internet
and with :
DEFINITIONS APPLICABLES AUX(EVENTUELS), AU
In B8 sheet2 :
This is my pseudo-code (Not working on VBA) :
Dim x As Long
x = 1
Do While Worksheets("Adobe Reader").Range("A1:A500").Find("Sponsor de l'Indice March? Site Internet").Row != Worksheets("Sheet1").Range("A1:A500").Find("DEFINITIONS APPLICABLES AUX(EVENTUELS), AU").Row
Set SJ = Worksheets("Sheet1").Range("A1:A500").Find("Nom de l'Indice Code Bloomberg Sponsor de l'Indice March? Site Internet")
ActiveCell.Offset(1, 0).Select
ActiveCell.Offset(1, 0).Copy
Worksheets("Sheet2").Range("B8").Offset(ColumnOffset:=x - 1).Paste
excel vba excel-vba excel-2016
add a comment |
I have this in Sheet 1 column A, with a lot of other text before and after :
I want to copy all what is betwen cell with :
Sponsor de l'Indice Marché Site Internet
and with :
DEFINITIONS APPLICABLES AUX(EVENTUELS), AU
In B8 sheet2 :
This is my pseudo-code (Not working on VBA) :
Dim x As Long
x = 1
Do While Worksheets("Adobe Reader").Range("A1:A500").Find("Sponsor de l'Indice March? Site Internet").Row != Worksheets("Sheet1").Range("A1:A500").Find("DEFINITIONS APPLICABLES AUX(EVENTUELS), AU").Row
Set SJ = Worksheets("Sheet1").Range("A1:A500").Find("Nom de l'Indice Code Bloomberg Sponsor de l'Indice March? Site Internet")
ActiveCell.Offset(1, 0).Select
ActiveCell.Offset(1, 0).Copy
Worksheets("Sheet2").Range("B8").Offset(ColumnOffset:=x - 1).Paste
excel vba excel-vba excel-2016
You don't state your problem, so I will guess it lies with theActiveCell
statements, which you want to avoid since very often, the active cell is not the one you think. See this
– cybernetic.nomad
Nov 15 '18 at 18:00
My code translate more my mind than a true code, for exemple "!=" will not work here
– babou
Nov 15 '18 at 18:10
Then please edit your question to mention it is "pseudo-code" and let us know what the problem is!
– cybernetic.nomad
Nov 15 '18 at 18:14
add a comment |
I have this in Sheet 1 column A, with a lot of other text before and after :
I want to copy all what is betwen cell with :
Sponsor de l'Indice Marché Site Internet
and with :
DEFINITIONS APPLICABLES AUX(EVENTUELS), AU
In B8 sheet2 :
This is my pseudo-code (Not working on VBA) :
Dim x As Long
x = 1
Do While Worksheets("Adobe Reader").Range("A1:A500").Find("Sponsor de l'Indice March? Site Internet").Row != Worksheets("Sheet1").Range("A1:A500").Find("DEFINITIONS APPLICABLES AUX(EVENTUELS), AU").Row
Set SJ = Worksheets("Sheet1").Range("A1:A500").Find("Nom de l'Indice Code Bloomberg Sponsor de l'Indice March? Site Internet")
ActiveCell.Offset(1, 0).Select
ActiveCell.Offset(1, 0).Copy
Worksheets("Sheet2").Range("B8").Offset(ColumnOffset:=x - 1).Paste
excel vba excel-vba excel-2016
I have this in Sheet 1 column A, with a lot of other text before and after :
I want to copy all what is betwen cell with :
Sponsor de l'Indice Marché Site Internet
and with :
DEFINITIONS APPLICABLES AUX(EVENTUELS), AU
In B8 sheet2 :
This is my pseudo-code (Not working on VBA) :
Dim x As Long
x = 1
Do While Worksheets("Adobe Reader").Range("A1:A500").Find("Sponsor de l'Indice March? Site Internet").Row != Worksheets("Sheet1").Range("A1:A500").Find("DEFINITIONS APPLICABLES AUX(EVENTUELS), AU").Row
Set SJ = Worksheets("Sheet1").Range("A1:A500").Find("Nom de l'Indice Code Bloomberg Sponsor de l'Indice March? Site Internet")
ActiveCell.Offset(1, 0).Select
ActiveCell.Offset(1, 0).Copy
Worksheets("Sheet2").Range("B8").Offset(ColumnOffset:=x - 1).Paste
excel vba excel-vba excel-2016
excel vba excel-vba excel-2016
edited Nov 15 '18 at 18:27
babou
asked Nov 15 '18 at 17:53
baboubabou
13211
13211
You don't state your problem, so I will guess it lies with theActiveCell
statements, which you want to avoid since very often, the active cell is not the one you think. See this
– cybernetic.nomad
Nov 15 '18 at 18:00
My code translate more my mind than a true code, for exemple "!=" will not work here
– babou
Nov 15 '18 at 18:10
Then please edit your question to mention it is "pseudo-code" and let us know what the problem is!
– cybernetic.nomad
Nov 15 '18 at 18:14
add a comment |
You don't state your problem, so I will guess it lies with theActiveCell
statements, which you want to avoid since very often, the active cell is not the one you think. See this
– cybernetic.nomad
Nov 15 '18 at 18:00
My code translate more my mind than a true code, for exemple "!=" will not work here
– babou
Nov 15 '18 at 18:10
Then please edit your question to mention it is "pseudo-code" and let us know what the problem is!
– cybernetic.nomad
Nov 15 '18 at 18:14
You don't state your problem, so I will guess it lies with the
ActiveCell
statements, which you want to avoid since very often, the active cell is not the one you think. See this– cybernetic.nomad
Nov 15 '18 at 18:00
You don't state your problem, so I will guess it lies with the
ActiveCell
statements, which you want to avoid since very often, the active cell is not the one you think. See this– cybernetic.nomad
Nov 15 '18 at 18:00
My code translate more my mind than a true code, for exemple "!=" will not work here
– babou
Nov 15 '18 at 18:10
My code translate more my mind than a true code, for exemple "!=" will not work here
– babou
Nov 15 '18 at 18:10
Then please edit your question to mention it is "pseudo-code" and let us know what the problem is!
– cybernetic.nomad
Nov 15 '18 at 18:14
Then please edit your question to mention it is "pseudo-code" and let us know what the problem is!
– cybernetic.nomad
Nov 15 '18 at 18:14
add a comment |
1 Answer
1
active
oldest
votes
You can try this:
Option Explicit
Sub copyRangeBetweenLookUpValues()
Dim lookUpValue1 As String
Dim lookUpValue2 As String
Dim lookUpValue1R As Long, lookUpValue1C As Long, lookUpValue2R As Long, lookUpValue2C As Long
'set your lookup values here
lookUpValue1 = "Sponsor de l'Indice Marché Site Internet"
lookUpValue2 = "DEFINITIONS APPLICABLES AUX(EVENTUELS), AU"
'find row and column of first value
lookUpValue1R = Cells.Find(lookUpValue1).Row
lookUpValue1C = Cells.Find(lookUpValue1).Column
'find row and column of second value
lookUpValue2R = Cells.Find(lookUpValue2).Row
lookUpValue2C = Cells.Find(lookUpValue2).Column
'copy range between these 2 values (but without values so first Row+1, second Row -1)
Range(Cells(lookUpValue1R + 1, lookUpValue1C), Cells(lookUpValue2R - 1, lookUpValue2C)).Copy
'paste
Range("B1").PasteSpecial xlPasteAll
End Sub
Thank you !! It works well ! And if values are in sheet1 and should it be paste in sheet 2 ? I found it with Select function but it's ugly...
– babou
Nov 16 '18 at 8:17
1
You can do it like so: Worksheets("sheet2").Range("B1").PasteSpecial xlPasteAll and if you want to target another workbook you can specify it before worksheet. Basically if you do not specify it assumes that you want to target active workbook which is not very robust. Best way is to declare all target sheets workbooks etc and then refer to them using variable name. If it get too long just use With statements.
– Pawel Czyz
Nov 16 '18 at 8:21
And if there is no lookUpValue2 ? How can I cancel all the code ?
– babou
Nov 16 '18 at 16:20
Dim rngFound As Range Set rngFound = Sheets("WhateverSheet").UsedRange.Find(What:="SoughtValue",LookIn:=xlFormulas) If Not rngFound Is Nothing Then 'you found the value - do whatever Else ' you didn't find the value End if
– Pawel Czyz
Nov 17 '18 at 18:25
More about it here: mrexcel.com/forum/excel-questions/…
– Pawel Czyz
Nov 17 '18 at 18:25
|
show 1 more 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%2f53325304%2freturn-lines-values-between-2-lines-with-a-specific-sentence-via-find%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can try this:
Option Explicit
Sub copyRangeBetweenLookUpValues()
Dim lookUpValue1 As String
Dim lookUpValue2 As String
Dim lookUpValue1R As Long, lookUpValue1C As Long, lookUpValue2R As Long, lookUpValue2C As Long
'set your lookup values here
lookUpValue1 = "Sponsor de l'Indice Marché Site Internet"
lookUpValue2 = "DEFINITIONS APPLICABLES AUX(EVENTUELS), AU"
'find row and column of first value
lookUpValue1R = Cells.Find(lookUpValue1).Row
lookUpValue1C = Cells.Find(lookUpValue1).Column
'find row and column of second value
lookUpValue2R = Cells.Find(lookUpValue2).Row
lookUpValue2C = Cells.Find(lookUpValue2).Column
'copy range between these 2 values (but without values so first Row+1, second Row -1)
Range(Cells(lookUpValue1R + 1, lookUpValue1C), Cells(lookUpValue2R - 1, lookUpValue2C)).Copy
'paste
Range("B1").PasteSpecial xlPasteAll
End Sub
Thank you !! It works well ! And if values are in sheet1 and should it be paste in sheet 2 ? I found it with Select function but it's ugly...
– babou
Nov 16 '18 at 8:17
1
You can do it like so: Worksheets("sheet2").Range("B1").PasteSpecial xlPasteAll and if you want to target another workbook you can specify it before worksheet. Basically if you do not specify it assumes that you want to target active workbook which is not very robust. Best way is to declare all target sheets workbooks etc and then refer to them using variable name. If it get too long just use With statements.
– Pawel Czyz
Nov 16 '18 at 8:21
And if there is no lookUpValue2 ? How can I cancel all the code ?
– babou
Nov 16 '18 at 16:20
Dim rngFound As Range Set rngFound = Sheets("WhateverSheet").UsedRange.Find(What:="SoughtValue",LookIn:=xlFormulas) If Not rngFound Is Nothing Then 'you found the value - do whatever Else ' you didn't find the value End if
– Pawel Czyz
Nov 17 '18 at 18:25
More about it here: mrexcel.com/forum/excel-questions/…
– Pawel Czyz
Nov 17 '18 at 18:25
|
show 1 more comment
You can try this:
Option Explicit
Sub copyRangeBetweenLookUpValues()
Dim lookUpValue1 As String
Dim lookUpValue2 As String
Dim lookUpValue1R As Long, lookUpValue1C As Long, lookUpValue2R As Long, lookUpValue2C As Long
'set your lookup values here
lookUpValue1 = "Sponsor de l'Indice Marché Site Internet"
lookUpValue2 = "DEFINITIONS APPLICABLES AUX(EVENTUELS), AU"
'find row and column of first value
lookUpValue1R = Cells.Find(lookUpValue1).Row
lookUpValue1C = Cells.Find(lookUpValue1).Column
'find row and column of second value
lookUpValue2R = Cells.Find(lookUpValue2).Row
lookUpValue2C = Cells.Find(lookUpValue2).Column
'copy range between these 2 values (but without values so first Row+1, second Row -1)
Range(Cells(lookUpValue1R + 1, lookUpValue1C), Cells(lookUpValue2R - 1, lookUpValue2C)).Copy
'paste
Range("B1").PasteSpecial xlPasteAll
End Sub
Thank you !! It works well ! And if values are in sheet1 and should it be paste in sheet 2 ? I found it with Select function but it's ugly...
– babou
Nov 16 '18 at 8:17
1
You can do it like so: Worksheets("sheet2").Range("B1").PasteSpecial xlPasteAll and if you want to target another workbook you can specify it before worksheet. Basically if you do not specify it assumes that you want to target active workbook which is not very robust. Best way is to declare all target sheets workbooks etc and then refer to them using variable name. If it get too long just use With statements.
– Pawel Czyz
Nov 16 '18 at 8:21
And if there is no lookUpValue2 ? How can I cancel all the code ?
– babou
Nov 16 '18 at 16:20
Dim rngFound As Range Set rngFound = Sheets("WhateverSheet").UsedRange.Find(What:="SoughtValue",LookIn:=xlFormulas) If Not rngFound Is Nothing Then 'you found the value - do whatever Else ' you didn't find the value End if
– Pawel Czyz
Nov 17 '18 at 18:25
More about it here: mrexcel.com/forum/excel-questions/…
– Pawel Czyz
Nov 17 '18 at 18:25
|
show 1 more comment
You can try this:
Option Explicit
Sub copyRangeBetweenLookUpValues()
Dim lookUpValue1 As String
Dim lookUpValue2 As String
Dim lookUpValue1R As Long, lookUpValue1C As Long, lookUpValue2R As Long, lookUpValue2C As Long
'set your lookup values here
lookUpValue1 = "Sponsor de l'Indice Marché Site Internet"
lookUpValue2 = "DEFINITIONS APPLICABLES AUX(EVENTUELS), AU"
'find row and column of first value
lookUpValue1R = Cells.Find(lookUpValue1).Row
lookUpValue1C = Cells.Find(lookUpValue1).Column
'find row and column of second value
lookUpValue2R = Cells.Find(lookUpValue2).Row
lookUpValue2C = Cells.Find(lookUpValue2).Column
'copy range between these 2 values (but without values so first Row+1, second Row -1)
Range(Cells(lookUpValue1R + 1, lookUpValue1C), Cells(lookUpValue2R - 1, lookUpValue2C)).Copy
'paste
Range("B1").PasteSpecial xlPasteAll
End Sub
You can try this:
Option Explicit
Sub copyRangeBetweenLookUpValues()
Dim lookUpValue1 As String
Dim lookUpValue2 As String
Dim lookUpValue1R As Long, lookUpValue1C As Long, lookUpValue2R As Long, lookUpValue2C As Long
'set your lookup values here
lookUpValue1 = "Sponsor de l'Indice Marché Site Internet"
lookUpValue2 = "DEFINITIONS APPLICABLES AUX(EVENTUELS), AU"
'find row and column of first value
lookUpValue1R = Cells.Find(lookUpValue1).Row
lookUpValue1C = Cells.Find(lookUpValue1).Column
'find row and column of second value
lookUpValue2R = Cells.Find(lookUpValue2).Row
lookUpValue2C = Cells.Find(lookUpValue2).Column
'copy range between these 2 values (but without values so first Row+1, second Row -1)
Range(Cells(lookUpValue1R + 1, lookUpValue1C), Cells(lookUpValue2R - 1, lookUpValue2C)).Copy
'paste
Range("B1").PasteSpecial xlPasteAll
End Sub
answered Nov 15 '18 at 18:13
Pawel CzyzPawel Czyz
7472719
7472719
Thank you !! It works well ! And if values are in sheet1 and should it be paste in sheet 2 ? I found it with Select function but it's ugly...
– babou
Nov 16 '18 at 8:17
1
You can do it like so: Worksheets("sheet2").Range("B1").PasteSpecial xlPasteAll and if you want to target another workbook you can specify it before worksheet. Basically if you do not specify it assumes that you want to target active workbook which is not very robust. Best way is to declare all target sheets workbooks etc and then refer to them using variable name. If it get too long just use With statements.
– Pawel Czyz
Nov 16 '18 at 8:21
And if there is no lookUpValue2 ? How can I cancel all the code ?
– babou
Nov 16 '18 at 16:20
Dim rngFound As Range Set rngFound = Sheets("WhateverSheet").UsedRange.Find(What:="SoughtValue",LookIn:=xlFormulas) If Not rngFound Is Nothing Then 'you found the value - do whatever Else ' you didn't find the value End if
– Pawel Czyz
Nov 17 '18 at 18:25
More about it here: mrexcel.com/forum/excel-questions/…
– Pawel Czyz
Nov 17 '18 at 18:25
|
show 1 more comment
Thank you !! It works well ! And if values are in sheet1 and should it be paste in sheet 2 ? I found it with Select function but it's ugly...
– babou
Nov 16 '18 at 8:17
1
You can do it like so: Worksheets("sheet2").Range("B1").PasteSpecial xlPasteAll and if you want to target another workbook you can specify it before worksheet. Basically if you do not specify it assumes that you want to target active workbook which is not very robust. Best way is to declare all target sheets workbooks etc and then refer to them using variable name. If it get too long just use With statements.
– Pawel Czyz
Nov 16 '18 at 8:21
And if there is no lookUpValue2 ? How can I cancel all the code ?
– babou
Nov 16 '18 at 16:20
Dim rngFound As Range Set rngFound = Sheets("WhateverSheet").UsedRange.Find(What:="SoughtValue",LookIn:=xlFormulas) If Not rngFound Is Nothing Then 'you found the value - do whatever Else ' you didn't find the value End if
– Pawel Czyz
Nov 17 '18 at 18:25
More about it here: mrexcel.com/forum/excel-questions/…
– Pawel Czyz
Nov 17 '18 at 18:25
Thank you !! It works well ! And if values are in sheet1 and should it be paste in sheet 2 ? I found it with Select function but it's ugly...
– babou
Nov 16 '18 at 8:17
Thank you !! It works well ! And if values are in sheet1 and should it be paste in sheet 2 ? I found it with Select function but it's ugly...
– babou
Nov 16 '18 at 8:17
1
1
You can do it like so: Worksheets("sheet2").Range("B1").PasteSpecial xlPasteAll and if you want to target another workbook you can specify it before worksheet. Basically if you do not specify it assumes that you want to target active workbook which is not very robust. Best way is to declare all target sheets workbooks etc and then refer to them using variable name. If it get too long just use With statements.
– Pawel Czyz
Nov 16 '18 at 8:21
You can do it like so: Worksheets("sheet2").Range("B1").PasteSpecial xlPasteAll and if you want to target another workbook you can specify it before worksheet. Basically if you do not specify it assumes that you want to target active workbook which is not very robust. Best way is to declare all target sheets workbooks etc and then refer to them using variable name. If it get too long just use With statements.
– Pawel Czyz
Nov 16 '18 at 8:21
And if there is no lookUpValue2 ? How can I cancel all the code ?
– babou
Nov 16 '18 at 16:20
And if there is no lookUpValue2 ? How can I cancel all the code ?
– babou
Nov 16 '18 at 16:20
Dim rngFound As Range Set rngFound = Sheets("WhateverSheet").UsedRange.Find(What:="SoughtValue",LookIn:=xlFormulas) If Not rngFound Is Nothing Then 'you found the value - do whatever Else ' you didn't find the value End if
– Pawel Czyz
Nov 17 '18 at 18:25
Dim rngFound As Range Set rngFound = Sheets("WhateverSheet").UsedRange.Find(What:="SoughtValue",LookIn:=xlFormulas) If Not rngFound Is Nothing Then 'you found the value - do whatever Else ' you didn't find the value End if
– Pawel Czyz
Nov 17 '18 at 18:25
More about it here: mrexcel.com/forum/excel-questions/…
– Pawel Czyz
Nov 17 '18 at 18:25
More about it here: mrexcel.com/forum/excel-questions/…
– Pawel Czyz
Nov 17 '18 at 18:25
|
show 1 more 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%2f53325304%2freturn-lines-values-between-2-lines-with-a-specific-sentence-via-find%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
You don't state your problem, so I will guess it lies with the
ActiveCell
statements, which you want to avoid since very often, the active cell is not the one you think. See this– cybernetic.nomad
Nov 15 '18 at 18:00
My code translate more my mind than a true code, for exemple "!=" will not work here
– babou
Nov 15 '18 at 18:10
Then please edit your question to mention it is "pseudo-code" and let us know what the problem is!
– cybernetic.nomad
Nov 15 '18 at 18:14