Search for row in closed workbook, then copy Specific data cells from a row (Not the entire row) into my...











up vote
0
down vote

favorite












I'm new to the this site and new to VBA in general, i'm looking for any help possible? if possible!?



I am trying to copy cell data from one sheet to another based on a reference match.



Basically I want to search for a row in a closed workbook using a reference number, then copy certain cells from that row (not entire row) in certain cells in my open workbook..



I assume a click button would be the best method for this?



so insert reference into open workbook, click button and pull data from closed workbook.. (sounds so easy on paper! ha)



for example.



search ref No.- 123456



Search this ref no. for a particular row within closed workbook "Weekly stats" then copy only cells - D2, E2, F2, G2 & S2 from that particular row into currently opened workbook.



is this possible? would save me so much time!



any help appreciated!



Thanks,










share|improve this question







New contributor




Drew.M is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • Welcome to Stack Overflow. Before you ask a question, there is an expectation that you show some effort to solve a specific problem. Stack Overflow is not a free code writing or resource discovery service.
    – Dragonthoughts
    Nov 8 at 9:25















up vote
0
down vote

favorite












I'm new to the this site and new to VBA in general, i'm looking for any help possible? if possible!?



I am trying to copy cell data from one sheet to another based on a reference match.



Basically I want to search for a row in a closed workbook using a reference number, then copy certain cells from that row (not entire row) in certain cells in my open workbook..



I assume a click button would be the best method for this?



so insert reference into open workbook, click button and pull data from closed workbook.. (sounds so easy on paper! ha)



for example.



search ref No.- 123456



Search this ref no. for a particular row within closed workbook "Weekly stats" then copy only cells - D2, E2, F2, G2 & S2 from that particular row into currently opened workbook.



is this possible? would save me so much time!



any help appreciated!



Thanks,










share|improve this question







New contributor




Drew.M is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • Welcome to Stack Overflow. Before you ask a question, there is an expectation that you show some effort to solve a specific problem. Stack Overflow is not a free code writing or resource discovery service.
    – Dragonthoughts
    Nov 8 at 9:25













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I'm new to the this site and new to VBA in general, i'm looking for any help possible? if possible!?



I am trying to copy cell data from one sheet to another based on a reference match.



Basically I want to search for a row in a closed workbook using a reference number, then copy certain cells from that row (not entire row) in certain cells in my open workbook..



I assume a click button would be the best method for this?



so insert reference into open workbook, click button and pull data from closed workbook.. (sounds so easy on paper! ha)



for example.



search ref No.- 123456



Search this ref no. for a particular row within closed workbook "Weekly stats" then copy only cells - D2, E2, F2, G2 & S2 from that particular row into currently opened workbook.



is this possible? would save me so much time!



any help appreciated!



Thanks,










share|improve this question







New contributor




Drew.M is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I'm new to the this site and new to VBA in general, i'm looking for any help possible? if possible!?



I am trying to copy cell data from one sheet to another based on a reference match.



Basically I want to search for a row in a closed workbook using a reference number, then copy certain cells from that row (not entire row) in certain cells in my open workbook..



I assume a click button would be the best method for this?



so insert reference into open workbook, click button and pull data from closed workbook.. (sounds so easy on paper! ha)



for example.



search ref No.- 123456



Search this ref no. for a particular row within closed workbook "Weekly stats" then copy only cells - D2, E2, F2, G2 & S2 from that particular row into currently opened workbook.



is this possible? would save me so much time!



any help appreciated!



Thanks,







excel vba






share|improve this question







New contributor




Drew.M is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question







New contributor




Drew.M is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question






New contributor




Drew.M is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Nov 8 at 9:19









Drew.M

1




1




New contributor




Drew.M is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Drew.M is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Drew.M is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • Welcome to Stack Overflow. Before you ask a question, there is an expectation that you show some effort to solve a specific problem. Stack Overflow is not a free code writing or resource discovery service.
    – Dragonthoughts
    Nov 8 at 9:25


















  • Welcome to Stack Overflow. Before you ask a question, there is an expectation that you show some effort to solve a specific problem. Stack Overflow is not a free code writing or resource discovery service.
    – Dragonthoughts
    Nov 8 at 9:25
















Welcome to Stack Overflow. Before you ask a question, there is an expectation that you show some effort to solve a specific problem. Stack Overflow is not a free code writing or resource discovery service.
– Dragonthoughts
Nov 8 at 9:25




Welcome to Stack Overflow. Before you ask a question, there is an expectation that you show some effort to solve a specific problem. Stack Overflow is not a free code writing or resource discovery service.
– Dragonthoughts
Nov 8 at 9:25












1 Answer
1






active

oldest

votes

















up vote
0
down vote













Paste this in your sheet code, adjust input cell & path.
Test by changing value in cell A1.
Some alternatives included for experimenting.



    Private Sub Worksheet_Change(ByVal Target As Range)
'Input cell
If Not Intersect(Target, Range("A1")) Is Nothing Then
If Target.Value <> "" Then
Application.ScreenUpdating = False

'Row selection by target value
rwnum = Target.Value

Dim wb As Workbook

''For user selected workbook
'Dim dP As String
'dP = Environ$("USERPROFILE") & "" & "Downloads": ChDrive "C:"
'ChDir dP
'Dim fNP As Variant
'fNP = Application.GetOpenFilename(FileFilter:="Excel Files (*.XLS), *.XLS", Title:="Select file")
'Set wb = Workbooks.Open(fNP)


''For static workbook with static path
'Set wb = Workbooks.Open("C:UsersUserNameDownloadsWeekly stats.xls")


''For static workbook with semi dynamical path
Set wb = Workbooks.Open(Environ$("USERPROFILE") & "" & "Downloads" & "Weekly stats.xls")
'C:UsersUserName Downloads Weekly stats.xls


Dim rng As Range
Set rng = wb.ActiveSheet.Range("A1").CurrentRegion

''Static columns
c1 = 1: c2 = 2: c3 = 3: c4 = 4: c5 = 16

''Dynamic columns
'With rng
' c1 = WorksheetFunction.Match("Example header text 1", .Rows(1), 0)
' c2 = WorksheetFunction.Match("Example header text 2", .Rows(1), 0)
' c3 = WorksheetFunction.Match("Example header text 3", .Rows(1), 0)
' c4 = WorksheetFunction.Match("Example header text 4", .Rows(1), 0)
' c5 = WorksheetFunction.Match("Example header text 5", .Rows(1), 0)
'End With

Dim Mrng As Range

''Static destination
'Set Mrng = Me.Range("D2:S2")

''Same row destination
'Set Mrng = Me.Cells(rwnum, 4).Resize(, 16)

''Next destination
Set Mrng = Me.Range("D1048576").End(xlUp).Offset(1).Resize(, 16)



With Mrng
.Cells(c1).Value = rng(rwnum, c1)
.Cells(c2).Value = rng(rwnum, c2)
.Cells(c3).Value = rng(rwnum, c3)
.Cells(c4).Value = rng(rwnum, c4)
.Cells(c5).Value = rng(rwnum, c5)
End With

wb.Close savechanges:=False

Target.Select

Application.ScreenUpdating = True
End If
End If
End Sub





share|improve this answer























  • Thanks for the reply! appreciated! I cant seem to get this to work? Could you please simplify this for me..? the closed workbook is from static path and will not move
    – Drew.M
    2 days ago











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',
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
});


}
});






Drew.M is a new contributor. Be nice, and check out our Code of Conduct.










 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53204689%2fsearch-for-row-in-closed-workbook-then-copy-specific-data-cells-from-a-row-not%23new-answer', 'question_page');
}
);

Post as a guest
































1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote













Paste this in your sheet code, adjust input cell & path.
Test by changing value in cell A1.
Some alternatives included for experimenting.



    Private Sub Worksheet_Change(ByVal Target As Range)
'Input cell
If Not Intersect(Target, Range("A1")) Is Nothing Then
If Target.Value <> "" Then
Application.ScreenUpdating = False

'Row selection by target value
rwnum = Target.Value

Dim wb As Workbook

''For user selected workbook
'Dim dP As String
'dP = Environ$("USERPROFILE") & "" & "Downloads": ChDrive "C:"
'ChDir dP
'Dim fNP As Variant
'fNP = Application.GetOpenFilename(FileFilter:="Excel Files (*.XLS), *.XLS", Title:="Select file")
'Set wb = Workbooks.Open(fNP)


''For static workbook with static path
'Set wb = Workbooks.Open("C:UsersUserNameDownloadsWeekly stats.xls")


''For static workbook with semi dynamical path
Set wb = Workbooks.Open(Environ$("USERPROFILE") & "" & "Downloads" & "Weekly stats.xls")
'C:UsersUserName Downloads Weekly stats.xls


Dim rng As Range
Set rng = wb.ActiveSheet.Range("A1").CurrentRegion

''Static columns
c1 = 1: c2 = 2: c3 = 3: c4 = 4: c5 = 16

''Dynamic columns
'With rng
' c1 = WorksheetFunction.Match("Example header text 1", .Rows(1), 0)
' c2 = WorksheetFunction.Match("Example header text 2", .Rows(1), 0)
' c3 = WorksheetFunction.Match("Example header text 3", .Rows(1), 0)
' c4 = WorksheetFunction.Match("Example header text 4", .Rows(1), 0)
' c5 = WorksheetFunction.Match("Example header text 5", .Rows(1), 0)
'End With

Dim Mrng As Range

''Static destination
'Set Mrng = Me.Range("D2:S2")

''Same row destination
'Set Mrng = Me.Cells(rwnum, 4).Resize(, 16)

''Next destination
Set Mrng = Me.Range("D1048576").End(xlUp).Offset(1).Resize(, 16)



With Mrng
.Cells(c1).Value = rng(rwnum, c1)
.Cells(c2).Value = rng(rwnum, c2)
.Cells(c3).Value = rng(rwnum, c3)
.Cells(c4).Value = rng(rwnum, c4)
.Cells(c5).Value = rng(rwnum, c5)
End With

wb.Close savechanges:=False

Target.Select

Application.ScreenUpdating = True
End If
End If
End Sub





share|improve this answer























  • Thanks for the reply! appreciated! I cant seem to get this to work? Could you please simplify this for me..? the closed workbook is from static path and will not move
    – Drew.M
    2 days ago















up vote
0
down vote













Paste this in your sheet code, adjust input cell & path.
Test by changing value in cell A1.
Some alternatives included for experimenting.



    Private Sub Worksheet_Change(ByVal Target As Range)
'Input cell
If Not Intersect(Target, Range("A1")) Is Nothing Then
If Target.Value <> "" Then
Application.ScreenUpdating = False

'Row selection by target value
rwnum = Target.Value

Dim wb As Workbook

''For user selected workbook
'Dim dP As String
'dP = Environ$("USERPROFILE") & "" & "Downloads": ChDrive "C:"
'ChDir dP
'Dim fNP As Variant
'fNP = Application.GetOpenFilename(FileFilter:="Excel Files (*.XLS), *.XLS", Title:="Select file")
'Set wb = Workbooks.Open(fNP)


''For static workbook with static path
'Set wb = Workbooks.Open("C:UsersUserNameDownloadsWeekly stats.xls")


''For static workbook with semi dynamical path
Set wb = Workbooks.Open(Environ$("USERPROFILE") & "" & "Downloads" & "Weekly stats.xls")
'C:UsersUserName Downloads Weekly stats.xls


Dim rng As Range
Set rng = wb.ActiveSheet.Range("A1").CurrentRegion

''Static columns
c1 = 1: c2 = 2: c3 = 3: c4 = 4: c5 = 16

''Dynamic columns
'With rng
' c1 = WorksheetFunction.Match("Example header text 1", .Rows(1), 0)
' c2 = WorksheetFunction.Match("Example header text 2", .Rows(1), 0)
' c3 = WorksheetFunction.Match("Example header text 3", .Rows(1), 0)
' c4 = WorksheetFunction.Match("Example header text 4", .Rows(1), 0)
' c5 = WorksheetFunction.Match("Example header text 5", .Rows(1), 0)
'End With

Dim Mrng As Range

''Static destination
'Set Mrng = Me.Range("D2:S2")

''Same row destination
'Set Mrng = Me.Cells(rwnum, 4).Resize(, 16)

''Next destination
Set Mrng = Me.Range("D1048576").End(xlUp).Offset(1).Resize(, 16)



With Mrng
.Cells(c1).Value = rng(rwnum, c1)
.Cells(c2).Value = rng(rwnum, c2)
.Cells(c3).Value = rng(rwnum, c3)
.Cells(c4).Value = rng(rwnum, c4)
.Cells(c5).Value = rng(rwnum, c5)
End With

wb.Close savechanges:=False

Target.Select

Application.ScreenUpdating = True
End If
End If
End Sub





share|improve this answer























  • Thanks for the reply! appreciated! I cant seem to get this to work? Could you please simplify this for me..? the closed workbook is from static path and will not move
    – Drew.M
    2 days ago













up vote
0
down vote










up vote
0
down vote









Paste this in your sheet code, adjust input cell & path.
Test by changing value in cell A1.
Some alternatives included for experimenting.



    Private Sub Worksheet_Change(ByVal Target As Range)
'Input cell
If Not Intersect(Target, Range("A1")) Is Nothing Then
If Target.Value <> "" Then
Application.ScreenUpdating = False

'Row selection by target value
rwnum = Target.Value

Dim wb As Workbook

''For user selected workbook
'Dim dP As String
'dP = Environ$("USERPROFILE") & "" & "Downloads": ChDrive "C:"
'ChDir dP
'Dim fNP As Variant
'fNP = Application.GetOpenFilename(FileFilter:="Excel Files (*.XLS), *.XLS", Title:="Select file")
'Set wb = Workbooks.Open(fNP)


''For static workbook with static path
'Set wb = Workbooks.Open("C:UsersUserNameDownloadsWeekly stats.xls")


''For static workbook with semi dynamical path
Set wb = Workbooks.Open(Environ$("USERPROFILE") & "" & "Downloads" & "Weekly stats.xls")
'C:UsersUserName Downloads Weekly stats.xls


Dim rng As Range
Set rng = wb.ActiveSheet.Range("A1").CurrentRegion

''Static columns
c1 = 1: c2 = 2: c3 = 3: c4 = 4: c5 = 16

''Dynamic columns
'With rng
' c1 = WorksheetFunction.Match("Example header text 1", .Rows(1), 0)
' c2 = WorksheetFunction.Match("Example header text 2", .Rows(1), 0)
' c3 = WorksheetFunction.Match("Example header text 3", .Rows(1), 0)
' c4 = WorksheetFunction.Match("Example header text 4", .Rows(1), 0)
' c5 = WorksheetFunction.Match("Example header text 5", .Rows(1), 0)
'End With

Dim Mrng As Range

''Static destination
'Set Mrng = Me.Range("D2:S2")

''Same row destination
'Set Mrng = Me.Cells(rwnum, 4).Resize(, 16)

''Next destination
Set Mrng = Me.Range("D1048576").End(xlUp).Offset(1).Resize(, 16)



With Mrng
.Cells(c1).Value = rng(rwnum, c1)
.Cells(c2).Value = rng(rwnum, c2)
.Cells(c3).Value = rng(rwnum, c3)
.Cells(c4).Value = rng(rwnum, c4)
.Cells(c5).Value = rng(rwnum, c5)
End With

wb.Close savechanges:=False

Target.Select

Application.ScreenUpdating = True
End If
End If
End Sub





share|improve this answer














Paste this in your sheet code, adjust input cell & path.
Test by changing value in cell A1.
Some alternatives included for experimenting.



    Private Sub Worksheet_Change(ByVal Target As Range)
'Input cell
If Not Intersect(Target, Range("A1")) Is Nothing Then
If Target.Value <> "" Then
Application.ScreenUpdating = False

'Row selection by target value
rwnum = Target.Value

Dim wb As Workbook

''For user selected workbook
'Dim dP As String
'dP = Environ$("USERPROFILE") & "" & "Downloads": ChDrive "C:"
'ChDir dP
'Dim fNP As Variant
'fNP = Application.GetOpenFilename(FileFilter:="Excel Files (*.XLS), *.XLS", Title:="Select file")
'Set wb = Workbooks.Open(fNP)


''For static workbook with static path
'Set wb = Workbooks.Open("C:UsersUserNameDownloadsWeekly stats.xls")


''For static workbook with semi dynamical path
Set wb = Workbooks.Open(Environ$("USERPROFILE") & "" & "Downloads" & "Weekly stats.xls")
'C:UsersUserName Downloads Weekly stats.xls


Dim rng As Range
Set rng = wb.ActiveSheet.Range("A1").CurrentRegion

''Static columns
c1 = 1: c2 = 2: c3 = 3: c4 = 4: c5 = 16

''Dynamic columns
'With rng
' c1 = WorksheetFunction.Match("Example header text 1", .Rows(1), 0)
' c2 = WorksheetFunction.Match("Example header text 2", .Rows(1), 0)
' c3 = WorksheetFunction.Match("Example header text 3", .Rows(1), 0)
' c4 = WorksheetFunction.Match("Example header text 4", .Rows(1), 0)
' c5 = WorksheetFunction.Match("Example header text 5", .Rows(1), 0)
'End With

Dim Mrng As Range

''Static destination
'Set Mrng = Me.Range("D2:S2")

''Same row destination
'Set Mrng = Me.Cells(rwnum, 4).Resize(, 16)

''Next destination
Set Mrng = Me.Range("D1048576").End(xlUp).Offset(1).Resize(, 16)



With Mrng
.Cells(c1).Value = rng(rwnum, c1)
.Cells(c2).Value = rng(rwnum, c2)
.Cells(c3).Value = rng(rwnum, c3)
.Cells(c4).Value = rng(rwnum, c4)
.Cells(c5).Value = rng(rwnum, c5)
End With

wb.Close savechanges:=False

Target.Select

Application.ScreenUpdating = True
End If
End If
End Sub






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 8 at 14:42

























answered Nov 8 at 14:28









mrkrister

335




335












  • Thanks for the reply! appreciated! I cant seem to get this to work? Could you please simplify this for me..? the closed workbook is from static path and will not move
    – Drew.M
    2 days ago


















  • Thanks for the reply! appreciated! I cant seem to get this to work? Could you please simplify this for me..? the closed workbook is from static path and will not move
    – Drew.M
    2 days ago
















Thanks for the reply! appreciated! I cant seem to get this to work? Could you please simplify this for me..? the closed workbook is from static path and will not move
– Drew.M
2 days ago




Thanks for the reply! appreciated! I cant seem to get this to work? Could you please simplify this for me..? the closed workbook is from static path and will not move
– Drew.M
2 days ago










Drew.M is a new contributor. Be nice, and check out our Code of Conduct.










 

draft saved


draft discarded


















Drew.M is a new contributor. Be nice, and check out our Code of Conduct.













Drew.M is a new contributor. Be nice, and check out our Code of Conduct.












Drew.M is a new contributor. Be nice, and check out our Code of Conduct.















 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53204689%2fsearch-for-row-in-closed-workbook-then-copy-specific-data-cells-from-a-row-not%23new-answer', 'question_page');
}
);

Post as a guest




















































































Popular posts from this blog

Guess what letter conforming each word

Port of Spain

Run scheduled task as local user group (not BUILTIN)