Copy and Paste Macro Based on Matching Values Not Doing Anything

matt767

New Member
Joined
Apr 11, 2022
Messages
40
Office Version
  1. 365
Platform
  1. Windows
I have 2 sheets, Sheet1 and Summary. In Sheet1, the first row contains text that may or may not be contained in column C of Summary. I am trying to copy and paste the values in Summary's column D alongside column C matches to Sheet1's first row (the data in Summary is already grouped by column C) into row 7 beneath the corresponding match in Sheet1. I have succeeded in doing it individually with the following code

VBA Code:
Sub allowed()
Dim cel As Range
Dim cel2 As Range
Dim srchrng1 As Range
Dim srchrng2 As Range
Set srchrng1 = Sheets("Sheet1").Range("a1:ac1")
Set srchrng2 = Sheets("Summary").Range("c1:c80000")
For Each cel In srchrng1
For Each cel2 In srchrng2
If cel.Value = cel2.Value Then
Dim list As Range
Set list = Range(cel.Offset(0, 0), cel.Offset(1000, 0))
Dim x As Long
x = Application.WorksheetFunction.CountA(list)
cel2.Offset(0, 1).Copy
cel.Offset(x, 0).PasteSpecial
End If
Next cel2
Next cel
End Sub


but it takes roughly 30 seconds to execute and I am trying to do it faster via 2 other methods which have not returned errors but nonetheless do nothing leaving Sheet1 blank (see below).

VBA Code:
Sub allowed2()
Dim cel As Range
Dim srchrng1 As Range
Set srchrng1 = Sheets("Sheet1").Range("a1:ac1")
For Each cel In srchrng1
Dim StartRow As Long, EndRow As Long
Sheets("Summary").Activate
If InStr("C:C", cel.Text) > 0 Then
StartRow = Application.WorksheetFunction.Match(cel.Text, "C:C", 0)
EndRow = Application.WorksheetFunction.Match(cel.Text, "C:C", 0) + Application.WorksheetFunction.CountIf(Columns("C"), cel.Text) - 1
Sheets("Summary").Range(Cells(StartRow, 4), Cells(EndRow, 4)).Copy
Sheets("Sheet1").cel.Offset(6, 0).PasteSpecial
Else
cel.Offset(6, 0).Value = ""
End If
Next cel
End Sub

VBA Code:
Sub allowed3()
Dim cel As Range
Dim srchrng1 As Range
Set srchrng1 = Sheets("Sheet1").Range("a1:ac1")
Dim res As Variant
Dim res2 As Variant
For Each cel In srchrng1
Sheets("Summary").Activate
res = Application.Match(cel.Text, "C:C", 0)
res2 = Application.CountIf(Columns("C"), cel.Text)
If IsError(res) Then
cel.Offset(6, 0).Value = ""
Else
Sheets("Summary").Range(Cells(res, 4), Cells(res + res2 - 1, 4)).Copy
Sheets("Sheet1").cel.Offset(6, 0).PasteSpecial
End If
Next cel
End Sub

Please let me know why these 2 other macros run but do nothing. Thank you so much.
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
solved:

VBA Code:
Sub allowed2()
Dim cel As Range
Dim srchrng1 As Range
Set srchrng1 = Sheets("Sheet1").Range("a1:ac1")
Dim rng As Range
Set rng = Sheets("Summary").Range("C:C")
For Each cel In srchrng1
Dim StartRow As Long
Dim EndRow As Long
Sheets("Summary").Activate
If Application.WorksheetFunction.CountIf(rng, cel.Text) > 0 Then
StartRow = Application.WorksheetFunction.Match(cel.Text, rng, 0)
EndRow = Application.WorksheetFunction.Match(cel.Text, rng, 0) + Application.WorksheetFunction.CountIf(Columns("C"), cel.Text) - 1
Sheets("Summary").Range(Cells(StartRow, 4), Cells(EndRow, 4)).Copy
cel.Offset(6, 0).PasteSpecial
Else
cel.Offset(6, 0).Value = ""
End If
Next cel
End Sub

this is much faster than my first method.
 
Upvote 0
Solution

Forum statistics

Threads
1,223,885
Messages
6,175,183
Members
452,615
Latest member
bogeys2birdies

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top