VBA Excel- Get Cell value and associated rows into another worksheet based on User Input
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
All-
I'm very new to VBA and I really need help. I have a worksheet called Sheet 1 that looks like this (This is where the data will be copied from)
and another sheet (Sheet2) that looks like this (this is where the data will be copied to). Notice that the order is not the same as above
When a user types in a place such as "Paris" I want it to copy all corresponding values with "Paris" and it's associated rows. So the end result should look like this
Here is the code I have so far. Right now I can pull all the corresponding values based on the Users input, but I cannot for the life of me figure out how to get the associated rows. Please help! Any input will be highly appreciated.
Dim x As String
Dim K As Long
Dim ct As Variant
Dim r As Range
Dim w1 As Worksheet
Dim w2 As Worksheet
Set w1 = Sheets("Sheet1")
Set w2 = Sheets("Sheet2")
a = Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
x = Application.InputBox("Please Enter Place")
w1.Activate
K = 3
For Each r In Intersect(Range("C3:C3" & a), ActiveSheet.UsedRange)
ct = r.Value
If InStr(ct, x) > 0 And ct <> "" Then
r.Copy w2.Cells(K, 1)
K = K + 1
w2.Activate
End If
Next r
End Sub
excel vba
add a comment |
All-
I'm very new to VBA and I really need help. I have a worksheet called Sheet 1 that looks like this (This is where the data will be copied from)
and another sheet (Sheet2) that looks like this (this is where the data will be copied to). Notice that the order is not the same as above
When a user types in a place such as "Paris" I want it to copy all corresponding values with "Paris" and it's associated rows. So the end result should look like this
Here is the code I have so far. Right now I can pull all the corresponding values based on the Users input, but I cannot for the life of me figure out how to get the associated rows. Please help! Any input will be highly appreciated.
Dim x As String
Dim K As Long
Dim ct As Variant
Dim r As Range
Dim w1 As Worksheet
Dim w2 As Worksheet
Set w1 = Sheets("Sheet1")
Set w2 = Sheets("Sheet2")
a = Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
x = Application.InputBox("Please Enter Place")
w1.Activate
K = 3
For Each r In Intersect(Range("C3:C3" & a), ActiveSheet.UsedRange)
ct = r.Value
If InStr(ct, x) > 0 And ct <> "" Then
r.Copy w2.Cells(K, 1)
K = K + 1
w2.Activate
End If
Next r
End Sub
excel vba
Please do not tack on additional questions to your question. The original question was using VBA to obtain your list, so by you editing a newVLOOKUP
question makes this question too broad and invalidates prior answers. If you have a new question, please start a new one. You may add a link in your new question that points to this one for context if you like. You may want to take a look at How to Ask for more information. I have rolled back your question to keep it on topic for Stack Overflow. Thanks!!
– K.Dᴀᴠɪs
Nov 30 '18 at 15:09
Ok. That makes sense. I apologize. I'll start a new question.
– C-Lo
Nov 30 '18 at 15:57
No need to apologize. I just wanted to make sure you were aware :)
– K.Dᴀᴠɪs
Nov 30 '18 at 15:58
add a comment |
All-
I'm very new to VBA and I really need help. I have a worksheet called Sheet 1 that looks like this (This is where the data will be copied from)
and another sheet (Sheet2) that looks like this (this is where the data will be copied to). Notice that the order is not the same as above
When a user types in a place such as "Paris" I want it to copy all corresponding values with "Paris" and it's associated rows. So the end result should look like this
Here is the code I have so far. Right now I can pull all the corresponding values based on the Users input, but I cannot for the life of me figure out how to get the associated rows. Please help! Any input will be highly appreciated.
Dim x As String
Dim K As Long
Dim ct As Variant
Dim r As Range
Dim w1 As Worksheet
Dim w2 As Worksheet
Set w1 = Sheets("Sheet1")
Set w2 = Sheets("Sheet2")
a = Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
x = Application.InputBox("Please Enter Place")
w1.Activate
K = 3
For Each r In Intersect(Range("C3:C3" & a), ActiveSheet.UsedRange)
ct = r.Value
If InStr(ct, x) > 0 And ct <> "" Then
r.Copy w2.Cells(K, 1)
K = K + 1
w2.Activate
End If
Next r
End Sub
excel vba
All-
I'm very new to VBA and I really need help. I have a worksheet called Sheet 1 that looks like this (This is where the data will be copied from)
and another sheet (Sheet2) that looks like this (this is where the data will be copied to). Notice that the order is not the same as above
When a user types in a place such as "Paris" I want it to copy all corresponding values with "Paris" and it's associated rows. So the end result should look like this
Here is the code I have so far. Right now I can pull all the corresponding values based on the Users input, but I cannot for the life of me figure out how to get the associated rows. Please help! Any input will be highly appreciated.
Dim x As String
Dim K As Long
Dim ct As Variant
Dim r As Range
Dim w1 As Worksheet
Dim w2 As Worksheet
Set w1 = Sheets("Sheet1")
Set w2 = Sheets("Sheet2")
a = Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
x = Application.InputBox("Please Enter Place")
w1.Activate
K = 3
For Each r In Intersect(Range("C3:C3" & a), ActiveSheet.UsedRange)
ct = r.Value
If InStr(ct, x) > 0 And ct <> "" Then
r.Copy w2.Cells(K, 1)
K = K + 1
w2.Activate
End If
Next r
End Sub
excel vba
excel vba
edited Nov 30 '18 at 15:07
K.Dᴀᴠɪs
7,300112440
7,300112440
asked Nov 22 '18 at 7:41
C-LoC-Lo
345
345
Please do not tack on additional questions to your question. The original question was using VBA to obtain your list, so by you editing a newVLOOKUP
question makes this question too broad and invalidates prior answers. If you have a new question, please start a new one. You may add a link in your new question that points to this one for context if you like. You may want to take a look at How to Ask for more information. I have rolled back your question to keep it on topic for Stack Overflow. Thanks!!
– K.Dᴀᴠɪs
Nov 30 '18 at 15:09
Ok. That makes sense. I apologize. I'll start a new question.
– C-Lo
Nov 30 '18 at 15:57
No need to apologize. I just wanted to make sure you were aware :)
– K.Dᴀᴠɪs
Nov 30 '18 at 15:58
add a comment |
Please do not tack on additional questions to your question. The original question was using VBA to obtain your list, so by you editing a newVLOOKUP
question makes this question too broad and invalidates prior answers. If you have a new question, please start a new one. You may add a link in your new question that points to this one for context if you like. You may want to take a look at How to Ask for more information. I have rolled back your question to keep it on topic for Stack Overflow. Thanks!!
– K.Dᴀᴠɪs
Nov 30 '18 at 15:09
Ok. That makes sense. I apologize. I'll start a new question.
– C-Lo
Nov 30 '18 at 15:57
No need to apologize. I just wanted to make sure you were aware :)
– K.Dᴀᴠɪs
Nov 30 '18 at 15:58
Please do not tack on additional questions to your question. The original question was using VBA to obtain your list, so by you editing a new
VLOOKUP
question makes this question too broad and invalidates prior answers. If you have a new question, please start a new one. You may add a link in your new question that points to this one for context if you like. You may want to take a look at How to Ask for more information. I have rolled back your question to keep it on topic for Stack Overflow. Thanks!!– K.Dᴀᴠɪs
Nov 30 '18 at 15:09
Please do not tack on additional questions to your question. The original question was using VBA to obtain your list, so by you editing a new
VLOOKUP
question makes this question too broad and invalidates prior answers. If you have a new question, please start a new one. You may add a link in your new question that points to this one for context if you like. You may want to take a look at How to Ask for more information. I have rolled back your question to keep it on topic for Stack Overflow. Thanks!!– K.Dᴀᴠɪs
Nov 30 '18 at 15:09
Ok. That makes sense. I apologize. I'll start a new question.
– C-Lo
Nov 30 '18 at 15:57
Ok. That makes sense. I apologize. I'll start a new question.
– C-Lo
Nov 30 '18 at 15:57
No need to apologize. I just wanted to make sure you were aware :)
– K.Dᴀᴠɪs
Nov 30 '18 at 15:58
No need to apologize. I just wanted to make sure you were aware :)
– K.Dᴀᴠɪs
Nov 30 '18 at 15:58
add a comment |
1 Answer
1
active
oldest
votes
Assign the entire range to an array for quicker looping, then once the array finds a match to your inputstring, rewrite the values to your 2nd sheet.
Sub test()
Dim ws1 As Worksheet, ws2 As Worksheet, wsArr()
set ws1 = thisworkbook.worksheets("Sheet1")
set ws2 = thisworkbook.worksheets("Sheet2")
With ws1
wsArr = .Range(.Cells(3, 1), .Cells(LastRow(ws1), 4)).Value
End With
Dim findStr As String
findStr = InputBox("Please Enter Place")
Dim i As Long, r as long
Application.ScreenUpdating = False
With ws2
.Range("A3:D3").Value = array("Place", "Name", "Thing", "Animal")
For i = LBound(wsArr) To UBound(wsArr)
If wsArr(i, 3) = findStr Then
r = LastRow(ws2) + 1
.Cells(r, 1) = wsArr(i, 3)
.Cells(r, 2) = wsArr(i, 1)
.Cells(r, 3) = wsArr(i, 2)
.Cells(r, 4) = wsArr(i, 4)
End If
Next
End With
Application.ScreenUpdating = True
End Sub
Function LastRow(ByVal ws As Worksheet, Optional ByVal col As Variant = 1) As Long
With ws
LastRow = .Cells(.Rows.Count, col).End(xlUp).Row
End With
End Function
For even better performance, consider doing a COUNTIF()
to get the count of the number of findStr
occurances in your range - that way you can use this value to ReDim
a new array in order to write the matches there, then write the array to Sheet2
all at once.
And unfortunately, although my sub was namedtest()
, I didn't actually test it. So let me know if any issues arise.
– K.Dᴀᴠɪs
Nov 22 '18 at 8:01
Thank you so much, it works perfectly! How would I modify the code if I have more than 4 columns in Sheet 1(Say 10) and I only need to pull 6 of those columns into Sheet 2? I was playing with the code and I changed the ranges and the Array values but I get a subscript out of range error. Sorry for so many questions, I'm really trying to understand this for the future
– C-Lo
Nov 23 '18 at 1:42
The 4 in.Cells(LastRow(ws1), 4)).Value
is how many columns in your sheet1 that you wanted. You would change that to 10 for 10 columns. In sheet2, the 4 in.Cells(r, 4) = ...
(inside theFor i =
loop) will determine the output column.
– K.Dᴀᴠɪs
Nov 23 '18 at 1:46
Then to decide which columns you wanted to pull from would be the last number in= wsArr(i, 3)
(the3
means you are pulling from column 3 in Sheet1).
– K.Dᴀᴠɪs
Nov 23 '18 at 1:48
And finally, the.Range("A3:D3").Value = array("Place", "Name", "Thing", "Animal")
will be your headers in Sheet2. You would need to change the:D3
to the last column, and also add more headers to the array.
– K.Dᴀᴠɪs
Nov 23 '18 at 1:50
|
show 3 more comments
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%2f53426027%2fvba-excel-get-cell-value-and-associated-rows-into-another-worksheet-based-on-us%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
Assign the entire range to an array for quicker looping, then once the array finds a match to your inputstring, rewrite the values to your 2nd sheet.
Sub test()
Dim ws1 As Worksheet, ws2 As Worksheet, wsArr()
set ws1 = thisworkbook.worksheets("Sheet1")
set ws2 = thisworkbook.worksheets("Sheet2")
With ws1
wsArr = .Range(.Cells(3, 1), .Cells(LastRow(ws1), 4)).Value
End With
Dim findStr As String
findStr = InputBox("Please Enter Place")
Dim i As Long, r as long
Application.ScreenUpdating = False
With ws2
.Range("A3:D3").Value = array("Place", "Name", "Thing", "Animal")
For i = LBound(wsArr) To UBound(wsArr)
If wsArr(i, 3) = findStr Then
r = LastRow(ws2) + 1
.Cells(r, 1) = wsArr(i, 3)
.Cells(r, 2) = wsArr(i, 1)
.Cells(r, 3) = wsArr(i, 2)
.Cells(r, 4) = wsArr(i, 4)
End If
Next
End With
Application.ScreenUpdating = True
End Sub
Function LastRow(ByVal ws As Worksheet, Optional ByVal col As Variant = 1) As Long
With ws
LastRow = .Cells(.Rows.Count, col).End(xlUp).Row
End With
End Function
For even better performance, consider doing a COUNTIF()
to get the count of the number of findStr
occurances in your range - that way you can use this value to ReDim
a new array in order to write the matches there, then write the array to Sheet2
all at once.
And unfortunately, although my sub was namedtest()
, I didn't actually test it. So let me know if any issues arise.
– K.Dᴀᴠɪs
Nov 22 '18 at 8:01
Thank you so much, it works perfectly! How would I modify the code if I have more than 4 columns in Sheet 1(Say 10) and I only need to pull 6 of those columns into Sheet 2? I was playing with the code and I changed the ranges and the Array values but I get a subscript out of range error. Sorry for so many questions, I'm really trying to understand this for the future
– C-Lo
Nov 23 '18 at 1:42
The 4 in.Cells(LastRow(ws1), 4)).Value
is how many columns in your sheet1 that you wanted. You would change that to 10 for 10 columns. In sheet2, the 4 in.Cells(r, 4) = ...
(inside theFor i =
loop) will determine the output column.
– K.Dᴀᴠɪs
Nov 23 '18 at 1:46
Then to decide which columns you wanted to pull from would be the last number in= wsArr(i, 3)
(the3
means you are pulling from column 3 in Sheet1).
– K.Dᴀᴠɪs
Nov 23 '18 at 1:48
And finally, the.Range("A3:D3").Value = array("Place", "Name", "Thing", "Animal")
will be your headers in Sheet2. You would need to change the:D3
to the last column, and also add more headers to the array.
– K.Dᴀᴠɪs
Nov 23 '18 at 1:50
|
show 3 more comments
Assign the entire range to an array for quicker looping, then once the array finds a match to your inputstring, rewrite the values to your 2nd sheet.
Sub test()
Dim ws1 As Worksheet, ws2 As Worksheet, wsArr()
set ws1 = thisworkbook.worksheets("Sheet1")
set ws2 = thisworkbook.worksheets("Sheet2")
With ws1
wsArr = .Range(.Cells(3, 1), .Cells(LastRow(ws1), 4)).Value
End With
Dim findStr As String
findStr = InputBox("Please Enter Place")
Dim i As Long, r as long
Application.ScreenUpdating = False
With ws2
.Range("A3:D3").Value = array("Place", "Name", "Thing", "Animal")
For i = LBound(wsArr) To UBound(wsArr)
If wsArr(i, 3) = findStr Then
r = LastRow(ws2) + 1
.Cells(r, 1) = wsArr(i, 3)
.Cells(r, 2) = wsArr(i, 1)
.Cells(r, 3) = wsArr(i, 2)
.Cells(r, 4) = wsArr(i, 4)
End If
Next
End With
Application.ScreenUpdating = True
End Sub
Function LastRow(ByVal ws As Worksheet, Optional ByVal col As Variant = 1) As Long
With ws
LastRow = .Cells(.Rows.Count, col).End(xlUp).Row
End With
End Function
For even better performance, consider doing a COUNTIF()
to get the count of the number of findStr
occurances in your range - that way you can use this value to ReDim
a new array in order to write the matches there, then write the array to Sheet2
all at once.
And unfortunately, although my sub was namedtest()
, I didn't actually test it. So let me know if any issues arise.
– K.Dᴀᴠɪs
Nov 22 '18 at 8:01
Thank you so much, it works perfectly! How would I modify the code if I have more than 4 columns in Sheet 1(Say 10) and I only need to pull 6 of those columns into Sheet 2? I was playing with the code and I changed the ranges and the Array values but I get a subscript out of range error. Sorry for so many questions, I'm really trying to understand this for the future
– C-Lo
Nov 23 '18 at 1:42
The 4 in.Cells(LastRow(ws1), 4)).Value
is how many columns in your sheet1 that you wanted. You would change that to 10 for 10 columns. In sheet2, the 4 in.Cells(r, 4) = ...
(inside theFor i =
loop) will determine the output column.
– K.Dᴀᴠɪs
Nov 23 '18 at 1:46
Then to decide which columns you wanted to pull from would be the last number in= wsArr(i, 3)
(the3
means you are pulling from column 3 in Sheet1).
– K.Dᴀᴠɪs
Nov 23 '18 at 1:48
And finally, the.Range("A3:D3").Value = array("Place", "Name", "Thing", "Animal")
will be your headers in Sheet2. You would need to change the:D3
to the last column, and also add more headers to the array.
– K.Dᴀᴠɪs
Nov 23 '18 at 1:50
|
show 3 more comments
Assign the entire range to an array for quicker looping, then once the array finds a match to your inputstring, rewrite the values to your 2nd sheet.
Sub test()
Dim ws1 As Worksheet, ws2 As Worksheet, wsArr()
set ws1 = thisworkbook.worksheets("Sheet1")
set ws2 = thisworkbook.worksheets("Sheet2")
With ws1
wsArr = .Range(.Cells(3, 1), .Cells(LastRow(ws1), 4)).Value
End With
Dim findStr As String
findStr = InputBox("Please Enter Place")
Dim i As Long, r as long
Application.ScreenUpdating = False
With ws2
.Range("A3:D3").Value = array("Place", "Name", "Thing", "Animal")
For i = LBound(wsArr) To UBound(wsArr)
If wsArr(i, 3) = findStr Then
r = LastRow(ws2) + 1
.Cells(r, 1) = wsArr(i, 3)
.Cells(r, 2) = wsArr(i, 1)
.Cells(r, 3) = wsArr(i, 2)
.Cells(r, 4) = wsArr(i, 4)
End If
Next
End With
Application.ScreenUpdating = True
End Sub
Function LastRow(ByVal ws As Worksheet, Optional ByVal col As Variant = 1) As Long
With ws
LastRow = .Cells(.Rows.Count, col).End(xlUp).Row
End With
End Function
For even better performance, consider doing a COUNTIF()
to get the count of the number of findStr
occurances in your range - that way you can use this value to ReDim
a new array in order to write the matches there, then write the array to Sheet2
all at once.
Assign the entire range to an array for quicker looping, then once the array finds a match to your inputstring, rewrite the values to your 2nd sheet.
Sub test()
Dim ws1 As Worksheet, ws2 As Worksheet, wsArr()
set ws1 = thisworkbook.worksheets("Sheet1")
set ws2 = thisworkbook.worksheets("Sheet2")
With ws1
wsArr = .Range(.Cells(3, 1), .Cells(LastRow(ws1), 4)).Value
End With
Dim findStr As String
findStr = InputBox("Please Enter Place")
Dim i As Long, r as long
Application.ScreenUpdating = False
With ws2
.Range("A3:D3").Value = array("Place", "Name", "Thing", "Animal")
For i = LBound(wsArr) To UBound(wsArr)
If wsArr(i, 3) = findStr Then
r = LastRow(ws2) + 1
.Cells(r, 1) = wsArr(i, 3)
.Cells(r, 2) = wsArr(i, 1)
.Cells(r, 3) = wsArr(i, 2)
.Cells(r, 4) = wsArr(i, 4)
End If
Next
End With
Application.ScreenUpdating = True
End Sub
Function LastRow(ByVal ws As Worksheet, Optional ByVal col As Variant = 1) As Long
With ws
LastRow = .Cells(.Rows.Count, col).End(xlUp).Row
End With
End Function
For even better performance, consider doing a COUNTIF()
to get the count of the number of findStr
occurances in your range - that way you can use this value to ReDim
a new array in order to write the matches there, then write the array to Sheet2
all at once.
edited Nov 22 '18 at 8:10
answered Nov 22 '18 at 7:55
K.DᴀᴠɪsK.Dᴀᴠɪs
7,300112440
7,300112440
And unfortunately, although my sub was namedtest()
, I didn't actually test it. So let me know if any issues arise.
– K.Dᴀᴠɪs
Nov 22 '18 at 8:01
Thank you so much, it works perfectly! How would I modify the code if I have more than 4 columns in Sheet 1(Say 10) and I only need to pull 6 of those columns into Sheet 2? I was playing with the code and I changed the ranges and the Array values but I get a subscript out of range error. Sorry for so many questions, I'm really trying to understand this for the future
– C-Lo
Nov 23 '18 at 1:42
The 4 in.Cells(LastRow(ws1), 4)).Value
is how many columns in your sheet1 that you wanted. You would change that to 10 for 10 columns. In sheet2, the 4 in.Cells(r, 4) = ...
(inside theFor i =
loop) will determine the output column.
– K.Dᴀᴠɪs
Nov 23 '18 at 1:46
Then to decide which columns you wanted to pull from would be the last number in= wsArr(i, 3)
(the3
means you are pulling from column 3 in Sheet1).
– K.Dᴀᴠɪs
Nov 23 '18 at 1:48
And finally, the.Range("A3:D3").Value = array("Place", "Name", "Thing", "Animal")
will be your headers in Sheet2. You would need to change the:D3
to the last column, and also add more headers to the array.
– K.Dᴀᴠɪs
Nov 23 '18 at 1:50
|
show 3 more comments
And unfortunately, although my sub was namedtest()
, I didn't actually test it. So let me know if any issues arise.
– K.Dᴀᴠɪs
Nov 22 '18 at 8:01
Thank you so much, it works perfectly! How would I modify the code if I have more than 4 columns in Sheet 1(Say 10) and I only need to pull 6 of those columns into Sheet 2? I was playing with the code and I changed the ranges and the Array values but I get a subscript out of range error. Sorry for so many questions, I'm really trying to understand this for the future
– C-Lo
Nov 23 '18 at 1:42
The 4 in.Cells(LastRow(ws1), 4)).Value
is how many columns in your sheet1 that you wanted. You would change that to 10 for 10 columns. In sheet2, the 4 in.Cells(r, 4) = ...
(inside theFor i =
loop) will determine the output column.
– K.Dᴀᴠɪs
Nov 23 '18 at 1:46
Then to decide which columns you wanted to pull from would be the last number in= wsArr(i, 3)
(the3
means you are pulling from column 3 in Sheet1).
– K.Dᴀᴠɪs
Nov 23 '18 at 1:48
And finally, the.Range("A3:D3").Value = array("Place", "Name", "Thing", "Animal")
will be your headers in Sheet2. You would need to change the:D3
to the last column, and also add more headers to the array.
– K.Dᴀᴠɪs
Nov 23 '18 at 1:50
And unfortunately, although my sub was named
test()
, I didn't actually test it. So let me know if any issues arise.– K.Dᴀᴠɪs
Nov 22 '18 at 8:01
And unfortunately, although my sub was named
test()
, I didn't actually test it. So let me know if any issues arise.– K.Dᴀᴠɪs
Nov 22 '18 at 8:01
Thank you so much, it works perfectly! How would I modify the code if I have more than 4 columns in Sheet 1(Say 10) and I only need to pull 6 of those columns into Sheet 2? I was playing with the code and I changed the ranges and the Array values but I get a subscript out of range error. Sorry for so many questions, I'm really trying to understand this for the future
– C-Lo
Nov 23 '18 at 1:42
Thank you so much, it works perfectly! How would I modify the code if I have more than 4 columns in Sheet 1(Say 10) and I only need to pull 6 of those columns into Sheet 2? I was playing with the code and I changed the ranges and the Array values but I get a subscript out of range error. Sorry for so many questions, I'm really trying to understand this for the future
– C-Lo
Nov 23 '18 at 1:42
The 4 in
.Cells(LastRow(ws1), 4)).Value
is how many columns in your sheet1 that you wanted. You would change that to 10 for 10 columns. In sheet2, the 4 in .Cells(r, 4) = ...
(inside the For i =
loop) will determine the output column.– K.Dᴀᴠɪs
Nov 23 '18 at 1:46
The 4 in
.Cells(LastRow(ws1), 4)).Value
is how many columns in your sheet1 that you wanted. You would change that to 10 for 10 columns. In sheet2, the 4 in .Cells(r, 4) = ...
(inside the For i =
loop) will determine the output column.– K.Dᴀᴠɪs
Nov 23 '18 at 1:46
Then to decide which columns you wanted to pull from would be the last number in
= wsArr(i, 3)
(the 3
means you are pulling from column 3 in Sheet1).– K.Dᴀᴠɪs
Nov 23 '18 at 1:48
Then to decide which columns you wanted to pull from would be the last number in
= wsArr(i, 3)
(the 3
means you are pulling from column 3 in Sheet1).– K.Dᴀᴠɪs
Nov 23 '18 at 1:48
And finally, the
.Range("A3:D3").Value = array("Place", "Name", "Thing", "Animal")
will be your headers in Sheet2. You would need to change the :D3
to the last column, and also add more headers to the array.– K.Dᴀᴠɪs
Nov 23 '18 at 1:50
And finally, the
.Range("A3:D3").Value = array("Place", "Name", "Thing", "Animal")
will be your headers in Sheet2. You would need to change the :D3
to the last column, and also add more headers to the array.– K.Dᴀᴠɪs
Nov 23 '18 at 1:50
|
show 3 more comments
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%2f53426027%2fvba-excel-get-cell-value-and-associated-rows-into-another-worksheet-based-on-us%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
Please do not tack on additional questions to your question. The original question was using VBA to obtain your list, so by you editing a new
VLOOKUP
question makes this question too broad and invalidates prior answers. If you have a new question, please start a new one. You may add a link in your new question that points to this one for context if you like. You may want to take a look at How to Ask for more information. I have rolled back your question to keep it on topic for Stack Overflow. Thanks!!– K.Dᴀᴠɪs
Nov 30 '18 at 15:09
Ok. That makes sense. I apologize. I'll start a new question.
– C-Lo
Nov 30 '18 at 15:57
No need to apologize. I just wanted to make sure you were aware :)
– K.Dᴀᴠɪs
Nov 30 '18 at 15:58