• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Modification of copy paste vba code

Code:
Const START_COL_ORIG As Integer = 1
Const START_COL_DEST As Integer = 2
Const SHEET_ORIG As String = "Sheet1"
Const SHEET_DEST As String = "Sheet2"

Public Sub STEP3()
   Dim shOrig As Worksheet, shDest As Worksheet
  
   Application.ScreenUpdating = False
  
   Set shOrig = Worksheets(SHEET_ORIG)
   Set shDest = Worksheets(SHEET_DEST)
   shOrig.Select
   Application.Union(shOrig.Columns(START_COL_ORIG), shOrig.Columns(START_COL_ORIG + 1)).Select
   Selection.Copy
   shDest.Activate
   shDest.Cells(1, START_COL_DEST).Select
   ActiveSheet.Paste
   Application.CutCopyMode = False
  
   Application.ScreenUpdating = True
End Sub



This vba codes copy column A & B from sheet1 and paste to column B & C in sheet2
what i need in this is copy column A,B,C,D,E from sheet1 and paste to sheet2 in column B,C,D,E,F
 
Last edited by a moderator:
Try this
Code:
Const START_COL_ORIG As Integer = 1
Const START_COL_DEST As Integer = 2
Const SHEET_ORIG As String = "Sheet1"
Const SHEET_DEST As String = "Sheet2"
Public Sub STEP3()
Dim shOrig As Worksheet, shDest As Worksheet
Application.ScreenUpdating = False
Set shOrig = Worksheets(SHEET_ORIG)
Set shDest = Worksheets(SHEET_DEST)
With shOrig
Application.Union(.Columns(START_COL_ORIG), .Columns(START_COL_ORIG + 1), .Columns(START_COL_ORIG + 2) _
, .Columns(START_COL_ORIG + 3), .Columns(START_COL_ORIG + 4)).Copy
End With
shDest.Activate
shDest.Cells(1, START_COL_DEST).Select
ActiveSheet.Paste
With Application
.CutCopyMode = False
.ScreenUpdating = True
End With
End Sub
 
Code:
Const START_COL_ORIG As Integer = 1
Const START_COL_DEST As Integer = 2
Const SHEET_ORIG As String = "Sheet1"
Const SHEET_DEST As String = "Sheet2"

Public Sub STEP3()
Dim shOrig As Worksheet, shDest As Worksheet

Application.ScreenUpdating = False

Set shOrig = Worksheets(SHEET_ORIG)
Set shDest = Worksheets(SHEET_DEST)

shOrig.Range("A:E").Copy shDest.Range("B1")

Application.ScreenUpdating = True
End Sub

In such a short macro, thre's really no need to assign the sheets to variables; this is a bit more efficient:
Code:
Public Sub STEP3()
Application.ScreenUpdating = False
Worksheets(SHEET_ORIG).Range("A:E").Copy Worksheets(SHEET_DEST).Range("B1")
Application.ScreenUpdating = True
End Sub

And using your constants:
Code:
Public Sub STEP3()
Application.ScreenUpdating = False
  Worksheets(SHEET_ORIG).Columns(START_COL_ORIG).Resize(, 5).Copy Worksheets(SHEET_DEST).Cells(1, START_COL_DEST)
Application.ScreenUpdating = True
End Sub
 
Last edited:
Sorry for that i will use code tags in future
Thnx p45cal,Belleke,bobhc for giving ur precious time and great support to this post
 
Code tags are used like this:

upload_2018-7-30_12-29-26.png
 

Attachments

  • upload_2018-7-30_12-28-1.png
    upload_2018-7-30_12-28-1.png
    2.4 KB · Views: 6
Back
Top