Error on Copy and Paste MACRO - HELP

robersha

New Member
Joined
Nov 7, 2013
Messages
27
I am trying to copy and paste the value of a certain cell (D16) onto another sheet. This value changes every hour. I have the below code but I am getting an error message at frng.Offset(1,0).PasteSpecial (xlPasteValues). Its telling me Run-time error '91': Object variable or With Block variable not set.

Any idea whats causing this issue? Recommend any changes?

Code:
Sub CopyPaste2()
    Dim ws1 As Worksheet, ws2 As Worksheet, rng As Range, frng As Range

    Set ws1 = Worksheets("Sheet2")
    Set ws2 = Worksheets("Sheet3")
    Set rng = ws1.Range("D16")
    Set frng = ws2.Rows(1).Find(What:=Time, After:=Range("A1"), LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
             , SearchFormat:=False)

    rng.Copy
    frng.Offset(1, 0).PasteSpecial (xlPasteValues)
    Application.CutCopyMode = 0

End Sub
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
after rng.Copy, put ws2.activate. See if that solves it.
 
Upvote 0
Thanks C Moore.

I did try that and received an error. I noticed that it is finding the system time. What i need it to do is match cell A2 on Sheet2 to the same time in ROw 1 on Sheet 3. then paste the data from D16 under that matched time frame.

Would you be able to help out on this? I have been stuck for over an hour.

Much Appreciated!
 
Upvote 0
So I have updated the code to reference the proper cells. I still though am receiving an error message at the same part as before. I tried your suggestion of putting ws2.activate and it did not resolve the issue.

Code:
Sub CopyPaste()
    Dim ws1 As Worksheet, ws2 As Worksheet, rng As Range, frng As Range

    Set ws1 = Worksheets("Sheet2")
    Set ws2 = Worksheets("Sheet3")
    Set rng = ws1.Range("D16")
    Set frng = ws2.Rows(1).Find(What:=Range("Sheet2!A2"), After:=Range("Sheet3!A1"), LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
             , SearchFormat:=False)

    rng.Copy
    frng.PasteSpecial (xlPasteValues)
    Application.CutCopyMode = 0

End Sub

I am soooo close I can feel it. Any suggestions anyone?
 
Upvote 0
After playing around some and asking another site, I was able to accomplish what I needed to do. Here is the final code:

Code:
Sub CopyPaste2()
    Dim ws1 As Worksheet, ws2 As Worksheet, rng As Range, frng As Range

    Set ws1 = Worksheets("Sheet2")
    Set ws2 = Worksheets("Sheet3")
    Set rng = ws1.Range("D16")
    Set frng = ws2.Rows(1).Find(What:=ws1.Range("A2"), LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
             , SearchFormat:=False)

    rng.Copy
    frng.Offset(1, 0).PasteSpecial (xlPasteValues)
    Application.CutCopyMode = 0

End Sub

Thanks everyone!
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,176
Members
451,543
Latest member
cesymcox

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