Copy selection range and paste in a specific sheet

Unexpc

Active Member
Joined
Nov 12, 2020
Messages
496
Office Version
  1. 2019
Platform
  1. Windows
Hi guys
i want when i select a range, after that with run a macro, doing this 3 steps
Paste in a sheet that named Sheet from cell A3
after that print from a sheet that named Print
after that clear contents from A3 until rows of sheet that named Sheet be ended...
 
I don't see what line Highlighted (Take Yellow Background when show error). only upload that Line Not Complete Code
you don't modify my last recommendations to your code also.
Why you have two End Sub at your code. Thus the codes after First End Sub not run.
Also Add this line for ClearContents of Green cell Pasted Before Last line:
VBA Code:
ShP.Range("A1").ClearContents
this line highlighted after i copy print code and paste after line you said
 

Attachments

  • Screenshot 2021-03-08 093029.png
    Screenshot 2021-03-08 093029.png
    41 KB · Views: 10
  • Screenshot 2021-03-08 093024.png
    Screenshot 2021-03-08 093024.png
    32.4 KB · Views: 10
Upvote 0

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
this line highlighted after i copy print code and paste after line you said
i think i don't correct compile toghether
this is print code you send before
VBA Code:
Sub PageForPrint()
Dim MyRange As Range
Dim ws As Worksheet
Dim Lastrow As Long
Dim i As Long
Dim n As Long
Dim j As Long
Dim PrintArea As String
Application.ScreenUpdating = False
Set ws = ActiveSheet
j = ActiveSheet.Index
Sheets(j + 1).Visible = True
Sheets(j + 1).Select
Debug.Print ws.Range("A1").Value
For i = 1 To 30840
If Cells(34 * i + 8, 1).Value <> "" Then
n = i + 1
Else
GoTo Printing
End If
Next i


Printing:
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:="Print" & (j + 1) / 2 _
    , Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
    :=False, OpenAfterPublish:=True


Sheets(j + 1).Visible = True
Sheets(j).Select
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Delete That line Completely, If you translate it you Know what it say:
VBA Code:
Dim i As Long
And this is complete version:
VBA Code:
Sub CopyPaste()
Dim ShP As Worksheet, SrRange As Range, Cell As Range, i As Long, L as Long
Dim MyRange As Range, ws As Worksheet, Lastrow As Long, n As Long, j As Long
Dim PrintArea As String
Application.ScreenUpdating = False
Set ShP = Worksheets("Sheet")
Set SrRange = Selection
Debug.Print SrRange.Address
For Each Cell In SrRange
If Cell.Interior.Color = 4697456 And Cell.Value <> "" Then
ShP.Range("A1").Value = Cell.Value
ElseIf Cell.Value <> "" Then
ShP.Range("B" & 3 + i).Value = Cell.Value
i = i + 1
End If
Next Cell
L = i
Set ws = ActiveSheet
j = ActiveSheet.Index
Sheets(j + 1).Visible = True
Sheets(j + 1).Select
Debug.Print ws.Range("A1").Value
For i = 1 To 30840
If Cells(34 * i + 8, 1).Value <> "" Then
n = i + 1
Else
GoTo Printing
End If
Next i

Printing:
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:="Print" & (j + 1) / 2 _
    , Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
    :=False, OpenAfterPublish:=True


Sheets(j + 1).Visible = True
Sheets(j).Select
ShP.Range("A1").ClearContents
ShP.Range("B3:B" & L + 2 ).ClearContents
Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0
Delete That line Completely, If you translate it you Know what it say:
VBA Code:
Dim i As Long
And this is complete version:
VBA Code:
Sub CopyPaste()
Dim ShP As Worksheet, SrRange As Range, Cell As Range, i As Long, L as Long
Dim MyRange As Range, ws As Worksheet, Lastrow As Long, n As Long, j As Long
Dim PrintArea As String
Application.ScreenUpdating = False
Set ShP = Worksheets("Sheet")
Set SrRange = Selection
Debug.Print SrRange.Address
For Each Cell In SrRange
If Cell.Interior.Color = 4697456 And Cell.Value <> "" Then
ShP.Range("A1").Value = Cell.Value
ElseIf Cell.Value <> "" Then
ShP.Range("B" & 3 + i).Value = Cell.Value
i = i + 1
End If
Next Cell
L = i
Set ws = ActiveSheet
j = ActiveSheet.Index
Sheets(j + 1).Visible = True
Sheets(j + 1).Select
Debug.Print ws.Range("A1").Value
For i = 1 To 30840
If Cells(34 * i + 8, 1).Value <> "" Then
n = i + 1
Else
GoTo Printing
End If
Next i

Printing:
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:="Print" & (j + 1) / 2 _
    , Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
    :=False, OpenAfterPublish:=True


Sheets(j + 1).Visible = True
Sheets(j).Select
ShP.Range("A1").ClearContents
ShP.Range("B3:B" & L + 2 ).ClearContents
Application.ScreenUpdating = True
End Sub
i have 3 problem:
1- that is print the sheet Paper after sheet Work i have, i want like before you written this code, print sheets their have names Print
2- after i run this code, that is erroring not do that merged cell and highlight this line ShP.Range("A1").ClearContents
3- still, the copy/paste not doing correctly in cells in the Sheet and just in a column with wrong number
 
Upvote 0
2. VBA codes has problems with Merged cells, you should not merge Cells.
3. What is your first Cell you want Paste Selected Data Without Green Color Cell?
 
Upvote 0
2. VBA codes has problems with Merged cells, you should not merge Cells.
3. What is your first Cell you want Paste Selected Data Without Green Color Cell?
2. anyway other solution? this should merged
3.B3
 
Upvote 0
1. I test on my PC . it run without error at all ( I add merged cell).
2. For me when Paste Data at Column B it Start Pasting From Cell B3 without Counting Green Selected Cells. Are you count green selected cell when you paste at column B. Thus when You Select Cells B6:B9 at Sheet1 and First cell is Green, you want to paste From Cell B4 (Or B5) Not B3.
 
Upvote 0
1. I test on my PC . it run without error at all ( I add merged cell).
2. For me when Paste Data at Column B it Start Pasting From Cell B3 without Counting Green Selected Cells. Are you count green selected cell when you paste at column B. Thus when You Select Cells B6:B9 at Sheet1 and First cell is Green, you want to paste From Cell B4 (Or B5) Not B3.
i think that is my wrong
1. with your code before i give your print code, it could clear contents and after that i have this problem
2. i select not just column B, for example i select B120:G139 and after add select green cell or not, but i want add this in select
and another thing, the print code that way it works not like before, for example when i select that data i said and run code, it print sheet after that i select cell (in my sheet i select data in sheet that named Work and when i run macro, it prints sheet after that it named Paper while this print code could print all of sheet that have named Print when i run print code in all of sheet that have name Sheet, you know it
 
Upvote 0
2. this is why code not working. Thus you want Paste Total Selected Rows (Example Column B:G) to your new Sheet without Green Part.
3. And you want to all cell in one row Paste exact at same row. Is this correct?
4. Are your all selected cell at column B have data or can be empty also?

AND Please Paste image of Sheet orders & show exactly what Sheet you want Print After Pasted Your Code to I correct the Code.
 
Upvote 0
2. this is why code not working. Thus you want Paste Total Selected Rows (Example Column B:G) to your new Sheet without Green Part.
3. And you want to all cell in one row Paste exact at same row. Is this correct?
4. Are your all selected cell at column B have data or can be empty also?

AND Please Paste image of Sheet orders & show exactly what Sheet you want Print After Pasted Your Code to I correct the Code.
2&3 - Yes, two sheet have same rows and column, but one thing is When select in another sheet and start at each cell and copy a range, when run code paste start at B3 in Sheet and continue cells
4.in column B and C All cells have data but in D,E,F,G have empty cells too.
https://file.io/gcnp2tlkTU7N
this is file that have two sheet, Sheet is data pasted on it and Print link to Sheet data and this sheet should print
 
Upvote 0

Forum statistics

Threads
1,223,943
Messages
6,175,552
Members
452,652
Latest member
eduedu

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