Copy/paste cells based on match criteria to different rows on multiple sheets










0















hoping to get extra eyes on this - I've only dabbled with VBA on personal projects before, but have been successful in the past! This one is a doozy though, never done anything so complex. I'm working with a spreadsheet - first tab is the master list, with multiple questions on it - the following tabs are different questionnaires that use the questions in different sequences.



I'm trying to make a macro that will search through the other tabs and compare the "Q" column, which holds the question #. If a tab has a Q cell that matches to the master list, I'd like to copy A#:Q# from the master list row and paste it on that sheet.
With a lot of searching on this site, here's how far I got:



Sub Refresh()

Refresh Macro

Keyboard Shortcut: Ctrl+r
Dim wM As Worksheet, wR As Worksheet
Dim r1 As Range, R2 As Range
Dim cel1 As Range, cel2 As Range
Dim LastRow As Long
Application.ScreenUpdating = False

Set wR = ActiveSheet
Set wM = ActiveSheet.Next

With wR
Set R2 = .Range("Q1", .Cells(.Rows.Count, .Columns("Q:Q").Column).End(xlUp))
End With

With wM
Set r1 = .Range("Q1", .Cells(.Rows.Count, .Columns("Q:Q").Column).End(xlUp))
End With

LastRow = 1


On Error Resume Next
For Each cel1 In r1
With Application
Set cel2 = .Index(R2, .Match(cel1.Value, R2, 0)) 'find match in active page
If Err = 0 Then
copyResult cel2, LastRow 'copy result to next page
End If
Err.Clear
LastRow = LastRow + 1
End With
Next cel1

End Sub

Sub copyResult(cel As Range, row As Long)
Dim ws As Worksheet
Set ws = ActiveSheet.Next

cel.EntireRow.Copy ws.Cells(row, 1)

End Sub


I'm stuck in two places:
1. trying to find a way to trigger the next sheet for "wM" to get updated
2. getting the copyResult section to copy just part of the row - there's a note section on each tab that shouldn't get overwritten. I tried doing cel.Range("A" & cel.row:"Q" & cel.row).Copy, but it didn't work.



Open to all and any thoughts - thanks in advance!










share|improve this question






















  • For the last part cel.Range("A" & cel.row:"Q" & cel.row).Copy your syntax is wrong here. Does this work? cel.Range("A" & cel.row &":Q" & cel.row).Copy

    – Brotato
    Nov 13 '18 at 20:58











  • Possible duplicate of stackoverflow.com/questions/42902156/…

    – StoneGiant
    Nov 13 '18 at 21:05











  • to loop thru worksheets try Dim ws As Worksheet For Each ws In ActiveWorkbook.Worksheets do code Next ws

    – alowflyingpig
    Nov 14 '18 at 2:05











  • @Brotato - thanks. I tried this: cel.Range("A" & cel.row & ":Q" & cel.row).Copy ws.Cells(row, 1), and added text to row 6 on ACtiveSheet. But instead of copying from the beginning of the row and adding it to the correct row on the next tab, it copied from Q5 and pasted in A5.

    – R-R
    Nov 14 '18 at 13:54











  • @StoneGiant, it is similar, but I don't know if I can use the match logic since theirs is numeric? I also have multiple sheets to compare to the first one, I'm not sure how to loop through correctly.

    – R-R
    Nov 14 '18 at 13:57















0















hoping to get extra eyes on this - I've only dabbled with VBA on personal projects before, but have been successful in the past! This one is a doozy though, never done anything so complex. I'm working with a spreadsheet - first tab is the master list, with multiple questions on it - the following tabs are different questionnaires that use the questions in different sequences.



I'm trying to make a macro that will search through the other tabs and compare the "Q" column, which holds the question #. If a tab has a Q cell that matches to the master list, I'd like to copy A#:Q# from the master list row and paste it on that sheet.
With a lot of searching on this site, here's how far I got:



Sub Refresh()

Refresh Macro

Keyboard Shortcut: Ctrl+r
Dim wM As Worksheet, wR As Worksheet
Dim r1 As Range, R2 As Range
Dim cel1 As Range, cel2 As Range
Dim LastRow As Long
Application.ScreenUpdating = False

Set wR = ActiveSheet
Set wM = ActiveSheet.Next

With wR
Set R2 = .Range("Q1", .Cells(.Rows.Count, .Columns("Q:Q").Column).End(xlUp))
End With

With wM
Set r1 = .Range("Q1", .Cells(.Rows.Count, .Columns("Q:Q").Column).End(xlUp))
End With

LastRow = 1


On Error Resume Next
For Each cel1 In r1
With Application
Set cel2 = .Index(R2, .Match(cel1.Value, R2, 0)) 'find match in active page
If Err = 0 Then
copyResult cel2, LastRow 'copy result to next page
End If
Err.Clear
LastRow = LastRow + 1
End With
Next cel1

End Sub

Sub copyResult(cel As Range, row As Long)
Dim ws As Worksheet
Set ws = ActiveSheet.Next

cel.EntireRow.Copy ws.Cells(row, 1)

End Sub


I'm stuck in two places:
1. trying to find a way to trigger the next sheet for "wM" to get updated
2. getting the copyResult section to copy just part of the row - there's a note section on each tab that shouldn't get overwritten. I tried doing cel.Range("A" & cel.row:"Q" & cel.row).Copy, but it didn't work.



Open to all and any thoughts - thanks in advance!










share|improve this question






















  • For the last part cel.Range("A" & cel.row:"Q" & cel.row).Copy your syntax is wrong here. Does this work? cel.Range("A" & cel.row &":Q" & cel.row).Copy

    – Brotato
    Nov 13 '18 at 20:58











  • Possible duplicate of stackoverflow.com/questions/42902156/…

    – StoneGiant
    Nov 13 '18 at 21:05











  • to loop thru worksheets try Dim ws As Worksheet For Each ws In ActiveWorkbook.Worksheets do code Next ws

    – alowflyingpig
    Nov 14 '18 at 2:05











  • @Brotato - thanks. I tried this: cel.Range("A" & cel.row & ":Q" & cel.row).Copy ws.Cells(row, 1), and added text to row 6 on ACtiveSheet. But instead of copying from the beginning of the row and adding it to the correct row on the next tab, it copied from Q5 and pasted in A5.

    – R-R
    Nov 14 '18 at 13:54











  • @StoneGiant, it is similar, but I don't know if I can use the match logic since theirs is numeric? I also have multiple sheets to compare to the first one, I'm not sure how to loop through correctly.

    – R-R
    Nov 14 '18 at 13:57













0












0








0








hoping to get extra eyes on this - I've only dabbled with VBA on personal projects before, but have been successful in the past! This one is a doozy though, never done anything so complex. I'm working with a spreadsheet - first tab is the master list, with multiple questions on it - the following tabs are different questionnaires that use the questions in different sequences.



I'm trying to make a macro that will search through the other tabs and compare the "Q" column, which holds the question #. If a tab has a Q cell that matches to the master list, I'd like to copy A#:Q# from the master list row and paste it on that sheet.
With a lot of searching on this site, here's how far I got:



Sub Refresh()

Refresh Macro

Keyboard Shortcut: Ctrl+r
Dim wM As Worksheet, wR As Worksheet
Dim r1 As Range, R2 As Range
Dim cel1 As Range, cel2 As Range
Dim LastRow As Long
Application.ScreenUpdating = False

Set wR = ActiveSheet
Set wM = ActiveSheet.Next

With wR
Set R2 = .Range("Q1", .Cells(.Rows.Count, .Columns("Q:Q").Column).End(xlUp))
End With

With wM
Set r1 = .Range("Q1", .Cells(.Rows.Count, .Columns("Q:Q").Column).End(xlUp))
End With

LastRow = 1


On Error Resume Next
For Each cel1 In r1
With Application
Set cel2 = .Index(R2, .Match(cel1.Value, R2, 0)) 'find match in active page
If Err = 0 Then
copyResult cel2, LastRow 'copy result to next page
End If
Err.Clear
LastRow = LastRow + 1
End With
Next cel1

End Sub

Sub copyResult(cel As Range, row As Long)
Dim ws As Worksheet
Set ws = ActiveSheet.Next

cel.EntireRow.Copy ws.Cells(row, 1)

End Sub


I'm stuck in two places:
1. trying to find a way to trigger the next sheet for "wM" to get updated
2. getting the copyResult section to copy just part of the row - there's a note section on each tab that shouldn't get overwritten. I tried doing cel.Range("A" & cel.row:"Q" & cel.row).Copy, but it didn't work.



Open to all and any thoughts - thanks in advance!










share|improve this question














hoping to get extra eyes on this - I've only dabbled with VBA on personal projects before, but have been successful in the past! This one is a doozy though, never done anything so complex. I'm working with a spreadsheet - first tab is the master list, with multiple questions on it - the following tabs are different questionnaires that use the questions in different sequences.



I'm trying to make a macro that will search through the other tabs and compare the "Q" column, which holds the question #. If a tab has a Q cell that matches to the master list, I'd like to copy A#:Q# from the master list row and paste it on that sheet.
With a lot of searching on this site, here's how far I got:



Sub Refresh()

Refresh Macro

Keyboard Shortcut: Ctrl+r
Dim wM As Worksheet, wR As Worksheet
Dim r1 As Range, R2 As Range
Dim cel1 As Range, cel2 As Range
Dim LastRow As Long
Application.ScreenUpdating = False

Set wR = ActiveSheet
Set wM = ActiveSheet.Next

With wR
Set R2 = .Range("Q1", .Cells(.Rows.Count, .Columns("Q:Q").Column).End(xlUp))
End With

With wM
Set r1 = .Range("Q1", .Cells(.Rows.Count, .Columns("Q:Q").Column).End(xlUp))
End With

LastRow = 1


On Error Resume Next
For Each cel1 In r1
With Application
Set cel2 = .Index(R2, .Match(cel1.Value, R2, 0)) 'find match in active page
If Err = 0 Then
copyResult cel2, LastRow 'copy result to next page
End If
Err.Clear
LastRow = LastRow + 1
End With
Next cel1

End Sub

Sub copyResult(cel As Range, row As Long)
Dim ws As Worksheet
Set ws = ActiveSheet.Next

cel.EntireRow.Copy ws.Cells(row, 1)

End Sub


I'm stuck in two places:
1. trying to find a way to trigger the next sheet for "wM" to get updated
2. getting the copyResult section to copy just part of the row - there's a note section on each tab that shouldn't get overwritten. I tried doing cel.Range("A" & cel.row:"Q" & cel.row).Copy, but it didn't work.



Open to all and any thoughts - thanks in advance!







excel vba






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 20:51









R-RR-R

1




1












  • For the last part cel.Range("A" & cel.row:"Q" & cel.row).Copy your syntax is wrong here. Does this work? cel.Range("A" & cel.row &":Q" & cel.row).Copy

    – Brotato
    Nov 13 '18 at 20:58











  • Possible duplicate of stackoverflow.com/questions/42902156/…

    – StoneGiant
    Nov 13 '18 at 21:05











  • to loop thru worksheets try Dim ws As Worksheet For Each ws In ActiveWorkbook.Worksheets do code Next ws

    – alowflyingpig
    Nov 14 '18 at 2:05











  • @Brotato - thanks. I tried this: cel.Range("A" & cel.row & ":Q" & cel.row).Copy ws.Cells(row, 1), and added text to row 6 on ACtiveSheet. But instead of copying from the beginning of the row and adding it to the correct row on the next tab, it copied from Q5 and pasted in A5.

    – R-R
    Nov 14 '18 at 13:54











  • @StoneGiant, it is similar, but I don't know if I can use the match logic since theirs is numeric? I also have multiple sheets to compare to the first one, I'm not sure how to loop through correctly.

    – R-R
    Nov 14 '18 at 13:57

















  • For the last part cel.Range("A" & cel.row:"Q" & cel.row).Copy your syntax is wrong here. Does this work? cel.Range("A" & cel.row &":Q" & cel.row).Copy

    – Brotato
    Nov 13 '18 at 20:58











  • Possible duplicate of stackoverflow.com/questions/42902156/…

    – StoneGiant
    Nov 13 '18 at 21:05











  • to loop thru worksheets try Dim ws As Worksheet For Each ws In ActiveWorkbook.Worksheets do code Next ws

    – alowflyingpig
    Nov 14 '18 at 2:05











  • @Brotato - thanks. I tried this: cel.Range("A" & cel.row & ":Q" & cel.row).Copy ws.Cells(row, 1), and added text to row 6 on ACtiveSheet. But instead of copying from the beginning of the row and adding it to the correct row on the next tab, it copied from Q5 and pasted in A5.

    – R-R
    Nov 14 '18 at 13:54











  • @StoneGiant, it is similar, but I don't know if I can use the match logic since theirs is numeric? I also have multiple sheets to compare to the first one, I'm not sure how to loop through correctly.

    – R-R
    Nov 14 '18 at 13:57
















For the last part cel.Range("A" & cel.row:"Q" & cel.row).Copy your syntax is wrong here. Does this work? cel.Range("A" & cel.row &":Q" & cel.row).Copy

– Brotato
Nov 13 '18 at 20:58





For the last part cel.Range("A" & cel.row:"Q" & cel.row).Copy your syntax is wrong here. Does this work? cel.Range("A" & cel.row &":Q" & cel.row).Copy

– Brotato
Nov 13 '18 at 20:58













Possible duplicate of stackoverflow.com/questions/42902156/…

– StoneGiant
Nov 13 '18 at 21:05





Possible duplicate of stackoverflow.com/questions/42902156/…

– StoneGiant
Nov 13 '18 at 21:05













to loop thru worksheets try Dim ws As Worksheet For Each ws In ActiveWorkbook.Worksheets do code Next ws

– alowflyingpig
Nov 14 '18 at 2:05





to loop thru worksheets try Dim ws As Worksheet For Each ws In ActiveWorkbook.Worksheets do code Next ws

– alowflyingpig
Nov 14 '18 at 2:05













@Brotato - thanks. I tried this: cel.Range("A" & cel.row & ":Q" & cel.row).Copy ws.Cells(row, 1), and added text to row 6 on ACtiveSheet. But instead of copying from the beginning of the row and adding it to the correct row on the next tab, it copied from Q5 and pasted in A5.

– R-R
Nov 14 '18 at 13:54





@Brotato - thanks. I tried this: cel.Range("A" & cel.row & ":Q" & cel.row).Copy ws.Cells(row, 1), and added text to row 6 on ACtiveSheet. But instead of copying from the beginning of the row and adding it to the correct row on the next tab, it copied from Q5 and pasted in A5.

– R-R
Nov 14 '18 at 13:54













@StoneGiant, it is similar, but I don't know if I can use the match logic since theirs is numeric? I also have multiple sheets to compare to the first one, I'm not sure how to loop through correctly.

– R-R
Nov 14 '18 at 13:57





@StoneGiant, it is similar, but I don't know if I can use the match logic since theirs is numeric? I also have multiple sheets to compare to the first one, I'm not sure how to loop through correctly.

– R-R
Nov 14 '18 at 13:57












0






active

oldest

votes











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
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53289310%2fcopy-paste-cells-based-on-match-criteria-to-different-rows-on-multiple-sheets%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































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.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53289310%2fcopy-paste-cells-based-on-match-criteria-to-different-rows-on-multiple-sheets%23new-answer', 'question_page');

);

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







Popular posts from this blog

Use pre created SQLite database for Android project in kotlin

Darth Vader #20

Ondo