paste entire row problem

lorenzo79

New Member
Joined
Feb 18, 2022
Messages
11
Office Version
  1. 2019
dear all ,

i need to find a cell value in another workbook.
when the value in column B is found i than need to paste there the entire row (starting from column A ) of the previous cell but i get an error.
tried different way but still not working .
tks if someone can help


VBA Code:
Sub copiaincolla()

Dim foundrange As Range
Dim test1
test1 = ActiveCell.Offset(0, -18)
ActiveCell.EntireRow.Copy
Workbooks.Open Filename:="X:\CargoluxItalia\GROUND OPS\riskregister.xlsx", Ignorereadonlyrecommended:=True
Workbooks("riskregister").Unprotect

Set foundrange = Workbooks("riskregister").Sheets("sheet1").Range("b:b").Find(test1, , xlValues, xlWhole, xlByRows, xlNext, False, False, False)
If Not foundrange Is Nothing Then

Workbooks("riskregister").Sheets("Sheet1").Select
[COLOR=rgb(226, 80, 65)]foundrange.Offset(0, -1).PasteSpecial[/COLOR]

End If
End Sub
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Please provide a screenshot of the error and what line is highlighted when you click on debug ?
 
Upvote 0
now i realised that i get an error only when i protect the sheet and i try to unprotect in the macro , if i unprotect the sheet and remove the line "Workbooks("riskregister").Unprotect than the macro works.
as i still need to keep the sheet protected can you help me to sort it out please ..
in the screenshot in red the error i get when i have the sheet protected.
 

Attachments

  • Capture.PNG
    Capture.PNG
    17.2 KB · Views: 4
Upvote 0
You should only need to unprotect the Sheet not the workbook.
Does your protection have a password ?

I would be expecting something like this:-
VBA Code:
Workbooks("riskregister").Sheets("Sheet1").Unprotect "Password"       ' <---- Enter password inside the quotes
 
Upvote 0
I haven't tested this but try this:-
VBA Code:
Sub copiaincolla()

    Dim foundrange As Range
    Dim test1
    Dim wbRReg As Workbook
    Dim shtRReg As Worksheet
   
    test1 = ActiveCell.Offset(0, -18)
    ActiveCell.EntireRow.Copy
    Workbooks.Open Filename:="X:\CargoluxItalia\GROUND OPS\riskregister.xlsx", Ignorereadonlyrecommended:=True
    Set wbRReg = Workbooks("riskregister.xlsx")
    Set shtRiskReg = wbRReg.Worksheets("Sheet1")
    shtRReg.Unprotect
   
    Set foundrange = shtRReg.Range("b:b").Find(test1, , xlValues, xlWhole, xlByRows, xlNext, False, False, False)
    If Not foundrange Is Nothing Then
   
        shtRReg.Select
        foundrange.Offset(0, -1).PasteSpecial
   
    End If

    shtRReg.Protect
End Sub

Also since you are pasting the entire row it might be safer to do something like, instead of using offset -1:
VBA Code:
        shtRReg.Range("A" & foundrange.row).PasteSpecial
 
Upvote 0
Oops change this
VBA Code:
Set shtRiskReg = wbRReg.Worksheets("Sheet1")

to this
VBA Code:
Set shtRReg = wbRReg.Worksheets("Sheet1")
 
Upvote 0
tks, unfortunately still get the error here attchd
 

Attachments

  • Capture.PNG
    Capture.PNG
    20.1 KB · Views: 4
Upvote 0
What is the error message ?
Add this line before it.
VBA Code:
Msgbox shtRReg.Range("A" & foundrange.row).address(0,0,xlA1,1)
and see if it makes sense.

I am login off for the night.
 
Upvote 0

Forum statistics

Threads
1,214,834
Messages
6,121,874
Members
449,056
Latest member
ruhulaminappu

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