Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Macros for Copy, paste and Cut, paste

0
Hi Experts,

Hope you all are doing great.

I have tried to write macros, but finding some error on the same.

I am trying to write below code of 1st time copy paste through macros and than 2nd time cut paste through macros to the attached excel sheet.

Hence, Please help me for solution.

Option Explicit

Sub Macro13()

'

' Macro13 Macro

'

' Keyboard Shortcut: Ctrl+Shift+R

'

Dim LstRw As Long, RepLst As Long 'varialble for last in Sheet 1 and Report

'Delete 4 rows after row 2 in Report first:

With Sheets("Report")

  '  .Range("A3:A6").EntireRow.Delete

    RepLst = WorksheetFunction.Max(.Range("A" & .Rows.Count).End(xlUp).Row, 3) 'pick row 3 or more

    ' copy from Sheet 1

    Sheets("Sheet1").Range("A3:C6").Copy

    ' paste to same rows in Report

    .Range("A" & (RepLst + 1)).PasteSpecial

Sheets("Report").Range("A7:C38").Cut

.Worksheets("Report").Range("A3:C34").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _

:=False, Transpose:=False

    Application.CutCopyMode = False

End With

'MsgBox "Data added to Report"

ActiveWorkbook.Save

End Sub

Regards,

Answer
Discuss

Answers

0
Selected Answer

Sunil

Try this instead (main changes in bold):

Option Explicit

Sub Macro13()
'
' Macro13 Macro
'
' Keyboard Shortcut: Ctrl+Shift+R
'
Dim LstRw As Long, RepLst As Long 'variable for last in Sheet 1 and Report


With Sheets("Report")
    'Delete 4 rows after row 2 in Report first:
    .Range("A3:A6").EntireRow.Delete

    RepLst = WorksheetFunction.Max(.Range("A" & .Rows.Count).End(xlUp).Row, 3) 'pick row 3 or more

    ' copy from Sheet 1
    Sheets("Sheet1").Range("A3:C6").Copy

    ' paste the same rows in Report
    .Range("A" & (RepLst + 1)).PasteSpecial

    ' cut some cells then paste at start
    .Range("A7:C38").Cut Destination:=.Range("A3")

    Application.CutCopyMode = False

End With

'MsgBox "Data added to Report"

ActiveWorkbook.Save

End Sub
but note that the Cut method may create a gap in your data. You could use this line instead: 
 ' cut all cells then paste at start

    .Range("A7:C" & RepLst).Cut Destination:=.Range("A3")
but don't forget to recalculate RepLast (last row) before using that line.

Seems to me that your code is doing somewhat contradictory thing (I assume you are playing) but you could add shift the data by doing gthis:1) delete the four rows 2) calculate the last row, RepLast then 3) paste the four new rows from sheet 1 after that.

Discuss

Discussion

Hi John,
Thank you for your quick response. Very much Appreciated.

Further, Inspite of using the below code, i still receiving gap in data. Please help.
.Range("A7:C" & RepLst).Cut Destination:=.Range("A3") 

I am looking for below result:
1. From "Sheet 1", The Range of (A3:C6) to copy  and paste in "Report" (sheet) below last filled Row.
2. From "Report", The Range of A7:Last fill cell (Including the cell which is pasted from "Sheet 1" to "Report". 

For Example:
1. Copy Range (A3:A6) from "Sheet 1" and paste below to last fill cell in "Report" (sheet)
2. Cut Range (A7:Lst fill cell (including the range which is already copied from Sheet1 and pasted below last filled cell).
* I am looking for cut Range and Pastespecial rather just cut and paste.

Hope you understand my concern.
Request you please help for solution.
Regards,
SunilA (rep: 58) Aug 11, '21 at 2:53 am
Sunil

When I warned of gaps and suggested you use RepLast, I thought you would realise it needed recalculating before that operation.

I'm not sure what's you're trying to do here. Seems to me that you could `1) delete three rows 2) calculate RepLast, paste the rows from sheet 1 after that.

That is the purpose of PasteSpecial here?
John_Ru (rep: 6142) Aug 11, '21 at 3:47 am
Thanks for your response.
SunilA (rep: 58) Aug 11, '21 at 5:04 am
Sunil

Thanks for selecting my Answer (though I'm not sure it does what you want since that is still confusing to me).

Note that I've just modified the Answer so others aren't too confused by it.
John_Ru (rep: 6142) Aug 11, '21 at 6:21 am
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login