You are here:

Excel/Copy and paste macro

Advertisement


Question
Repairer Database sheet
Repairer Database shee  
The Post Adjustment Sheet
The Post Adjustment Sh  
QUESTION: Hi
Im trying to write a macro that will copy and paste between 2 sheets using names as a lookup. The main sheet repairer database will be store all the relevant codes, the pre adjustment sheet and post adjustment sheets are the the editing sheets. Once the names have been selected in these sheets from the drop down the macro once activated will copy all the codes that correspond to those names from the repairer databse into these sheets.
I have written one that does the opposite and copies from the Post adjustment sheet into the repairer database sheet but the problem is it needs to clear the row prior to pasting. I've copied this example below

Suppose Im asking 2 questions, can the below macro have an added bit to clear the target row before pasting and a macro that which will copy and paste in the opposite direction. Im calling them the setup macro and reset macro

Sub Reset()

If MsgBox("Have you made the necessary updates?", vbYesNo) = vbNo Then Exit Sub

  Dim oCell As Range
  Dim oTarget As Range

  For Each oCell In Worksheets("Post Adjustment").Range("A8:G8")
      If Len(oCell.Value) > 0 Then
        Set oTarget = Nothing
        Set oTarget = Worksheets("Repairer Database").UsedRange.Columns(1).Find(oCell.Value)
        If Not oTarget Is Nothing Then
        If Application.CountA(oCell.EntireColumn) > 1 Then
        oCell.Offset(2).Resize(Application.CountA(oCell.EntireColumn)).Copy
        oTarget.Offset(, 5).PasteSpecial xlValues, , , True
        
        End If
        End If
      End If
  Next
  
   Sheets("Pre Adjustment").Range("A10:A224").ClearContents
   Sheets("Post Adjustment").Range("A10:A224").ClearContents
   
   Sheets("Pre Adjustment").Range("C10:C224").ClearContents
   Sheets("Post Adjustment").Range("C10:C224").ClearContents
   
   Sheets("Pre Adjustment").Range("E10:E224").ClearContents
   Sheets("Post Adjustment").Range("E10:E224").ClearContents
   
   Sheets("Pre Adjustment").Range("G10:G224").ClearContents
   Sheets("Post Adjustment").Range("G10:G224").ClearContents
   
   Sheets("Pre Adjustment").Range("I10:I224").ClearContents
   Sheets("Post Adjustment").Range("I10:I224").ClearContents
   
   Sheets("Pre Adjustment").Range("K10:K224").ClearContents
   Sheets("Post Adjustment").Range("K10:K224").ClearContents
   
   Sheets("Pre Adjustment").Range("M10:M224").ClearContents
   Sheets("Post Adjustment").Range("M10:M224").ClearContents
   
   Sheets("Pre Adjustment").Range("O10:O224").ClearContents
   Sheets("Post Adjustment").Range("O10:O224").ClearContents
   
   Sheets("Pre Adjustment").Range("Q10:Q224").ClearContents
   Sheets("Post Adjustment").Range("Q10:Q224").ClearContents
   
   Sheets("Pre Adjustment").Range("S10:S224").ClearContents
   Sheets("Post Adjustment").Range("S10:S224").ClearContents
   
   Sheets("Pre Adjustment").Range("U10:U224").ClearContents
   Sheets("Post Adjustment").Range("U10:U224").ClearContents
   
   Sheets("Pre Adjustment").Range("W10:W224").ClearContents
   Sheets("Post Adjustment").Range("W10:W224").ClearContents
   
   Sheets("Pre Adjustment").Range("Y10:Y224").ClearContents
   Sheets("Post Adjustment").Range("Y10:Y224").ClearContents
   
   Sheets("Pre Adjustment").Range("AA10:AA224").ClearContents
   Sheets("Post Adjustment").Range("AA10:AA224").ClearContents
   
   Sheets("Pre Adjustment").Range("AC10:AC224").ClearContents
   Sheets("Post Adjustment").Range("AC10:AC224").ClearContents
   
   Sheets("Pre Adjustment").Range("AE10:AE224").ClearContents
   Sheets("Post Adjustment").Range("AE10:AE224").ClearContents
   
   Sheets("Pre Adjustment").Range("AG10:AG224").ClearContents
   Sheets("Post Adjustment").Range("AG10:AG224").ClearContents
   
   Sheets("Pre Adjustment").Range("AI10:AI224").ClearContents
   Sheets("Post Adjustment").Range("AI10:AI224").ClearContents
   
   Sheets("Pre Adjustment").Range("AK10:AK224").ClearContents
   Sheets("Post Adjustment").Range("AK10:AK224").ClearContents
   
   Sheets("Pre Adjustment").Range("AM10:AM224").ClearContents
   Sheets("Post Adjustment").Range("AM10:AM224").ClearContents
   
   Sheets("Pre Adjustment").Range("AO10:AO224").ClearContents
   Sheets("Post Adjustment").Range("AO10:AO224").ClearContents
   
   Sheets("Pre Adjustment").Range("AQ10:AQ224").ClearContents
   Sheets("Post Adjustment").Range("AQ10:AQ224").ClearContents
   
   Sheets("Pre Adjustment").Range("AS10:AS224").ClearContents
   Sheets("Post Adjustment").Range("AS10:AS224").ClearContents
   
   Sheets("Pre Adjustment").Range("AU10:AU224").ClearContents
   Sheets("Post Adjustment").Range("AU10:AU224").ClearContents
   
   Sheets("Pre Adjustment").Range("AW10:AW224").ClearContents
   Sheets("Post Adjustment").Range("AW10:AW224").ClearContents
  
End Sub

ANSWER: (question found in question pool).

You may wish to move away from copy and paste - whilst this is what you have to do manually, excel doesn't need to do this in VB - though it's a little difficult to follow from pictures and the macro exactly what you are trying to do - range("a1:a20").value=sheets("other_sheet").range("B1:b20").value for instance would do the same as a pastespecial, values.  It may help to see the sheet you are working with - my direct email is aidan.heritage@virgin.net



---------- FOLLOW-UP ----------

Repairer Database
Repairer Database  

Post Adjustment Tab
Post Adjustment Tab  
QUESTION: Hi Aidan

Sorry I've reattached some larger images, I tried using Vlookup but because of how the formula on the rest of the sheet works it has to be values not formula in the cells. I've managed to tweak it so the copy and paste function works ok, the only issue Im having is I need to clear the target range before it pastes the data because if the new data range is shorter than the previous then the 2 get mixed up, is there anyway of doing this and only targeting the rows that it is about to paste over?


Sub Reset()

If MsgBox("Have you made the necessary updates?", vbYesNo) = vbNo Then Exit Sub

  Dim oCell As Range
  Dim oTarget As Range

  For Each oCell In Worksheets("Post Adjustment").Range("A8:G8")
      If Len(oCell.Value) > 0 Then
        Set oTarget = Nothing
        Set oTarget = Worksheets("Repairer Database").Columns(1).Find(oCell.Value)
        If Not oTarget Is Nothing Then
        If Application.CountA(oCell.EntireColumn) > 1 Then
        oCell.Offset(2).Resize(Application.CountA(oCell.EntireColumn)).Copy
        oTarget.Offset(, 5).PasteSpecial xlValues, , , True
        End If
        End If
      End If
  Next
End Sub

Answer
I find it difficult to see the data in images on this system- probably just my advancing years!  As I said yesterday, I personally don't like using copy and paste within macros, not least because on macros that take a while to run, users are likely to be using other applications and copying and pasting themselves - I prefer to set the value of cells to the value of other ones - this would have the advantage of being able to set a value, then clear the contents - by using copy and paste you have to copy, then paste, with no action in between.  IF you need to use copy and paste with an action in between, I'd suggest saving the data to a temporary location as values, clearing the source data, then pasting the temporary data, but it's no where near as tidy.  Also, thinking about it, you could easily clear the target range BEFORE doing the copy - simply clear at least as many cells as you are going to have data - so for example          
Worksheets("Repairer Database").Range("F" & oTarget.Row & ":AA" & oTarget.Row).Clear
would clear the data in columns F to AA of the appropriate row.
About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

Excel

All Answers


Answers by Expert:


Ask Experts

Volunteer


Aidan Heritage

Expertise

I have provided first hand support since `95 for Microsoft Office majoring in Word and Excel - support for all versions from 2 onwards. I'm based in the UK, so please allow for time differences when asking me questions from other parts of the world!

Experience

My background is in the insurance industry and call centre areas, but have been called upon to provide many varied solutions.

Education/Credentials
I'm educated to UK A level standard, but as I left school some 30 years ago that is rather irrelevent - university of life has provided more of a background!

©2016 About.com. All rights reserved.