You are here:

Excel/Copy paste some subsequent rows and some fixed rows

Advertisement


Question
QUESTION: Hi Damon,

Thanks for your last answer. Below is my code and question:

From sheet: Customer I want to copy subsequent rows each time I click like A4, then A5, then A6  to "Inv_Register_Commissions" however from Sheet: Invoice I want to copy fixed rows to "Inv_register_Commisions". The below code only copies fixed rows from Customer sheet. Can you please help with this one.

Private Sub COMMANDBUTTON1_Click()
Application.ScreenUpdating = False
Dim ws1 As Worksheet, ws2 As Worksheet, ws3 As Worksheet,  ws4 As Worksheet
   Dim DestRow As Long
   On Error Resume Next
   Set ws1 = Sheets("Customer")
   Set ws2 = Sheets("Invoice")
   Set ws3 = Sheets("Inv_Register_Commissions")
   Set ws4 = Sheets("Inv")
   DestRow = ws3.Cells(Rows.Count, "A").End(xlUp).Row + 1
   DestRow = ws4.Cells(Rows.Count, "B").End(xlUp).Row + 1
   ws1.Range("A4").Copy -------- here i want to copy subsequent rows (A5, A6 on each click just one row should be copied)
   ws3.Range("A" & DestRow).PasteSpecial xlPasteValues
   ws1.Range("B4").Copy -------- here i want to copy subsequent rows (B5, B6 on each click just one row should be copied)
   ws3.Range("D" & DestRow).PasteSpecial xlPasteValues
   ws1.Range("C4").Copy -------- here i want to copy subsequent rows (C5, C6 on each click just one row should be copied)
   ws3.Range("G" & DestRow).PasteSpecial xlPasteValues
   ws2.Range("B13").Copy  ----- From this sheet I want to copy fixed row
   ws3.Range("N" & DestRow).PasteSpecial xlPasteValues
   ws2.Range("H13").Copy ----- From this sheet I want to copy fixed row
   ws3.Range("L" & DestRow).PasteSpecial xlPasteValues
   ws2.Range("I28").Copy ----- From this sheet I want to copy fixed row
   ws3.Range("J" & DestRow).PasteSpecial xlPasteValues
   ws2.Range("H15").Copy ----- From this sheet I want to copy fixed row
   ws3.Range("K" & DestRow).PasteSpecial xlPasteValues
   ws2.Range("B13").Copy
   ws4.Range("D" & DestRow).PasteSpecial xlPasteValues
   ws2.Range("B14").Copy
   ws4.Range("E" & DestRow).PasteSpecial xlPasteValues
MsgBox ("Invoice details copied to register")
Application.ScreenUpdating = True
End Sub

ANSWER: Hi again Sofia,

I have a few questions this time.

1. What do you want to happen when the button is clicked the fourth time? Should it a. go back to cell A4 and start the cycle over again, b. ignore the click, c. other?

2. Do you want it to re-copy and paste the fixed cells from ws2 to ws3 and ws4 on each click (as your code currently does)?  Or just on the first click?  It seems unnecessarily repetitive unless you are manually editing the ws2 cells being copied between each click.

Damon




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

QUESTION: Ans 1: It should copy next row: A7, C7, E7 then A8, C8, E8 and so on.

Ans2: ws2 is an invoice sheet so each time it is prepared I will click the button and it will copy fixed cells to the other sheet to ws3 and ws4 because it is in a particular format.

Each time I prepare an invoice I will also update customer sheet (It has data based on affiliates name) with affiliates name so I want to copy next copy on each click

Hope I'm able to make it clear.

Sofia

Answer
Hi Sofia,

Here is my solution to advancing the rows by one on each button click:
_______________________________

Private Sub COMMANDBUTTON1_Click()

  Application.ScreenUpdating = False
  Dim Ws1 As Worksheet, Ws2 As Worksheet, Ws3 As Worksheet, Ws4 As Worksheet
  Dim DestRow3 As Long, DestRow4 As Long
  Static iClick     As Integer  'click count index (also row index)
  
  On Error Resume Next
  Set Ws1 = Sheets("Customer")
  Set Ws2 = Sheets("Invoice")
  Set Ws3 = Sheets("Inv_Register_Commissions")
  Set Ws4 = Sheets("Inv")
  
  If iClick = 0 Then iClick = 4 Else iClick = iClick + 1
  
  DestRow3 = Ws3.Cells(Rows.Count, "A").End(xlUp).Row + 1
  DestRow4 = Ws4.Cells(Rows.Count, "B").End(xlUp).Row + 1
  
  'Click counted rows and columns
  Ws3.Cells(DestRow3, "A") = Ws1.Cells(iClick, "A")
  Ws3.Cells(DestRow3, "D") = Ws1.Cells(iClick, "B")
  Ws3.Cells(DestRow3, "G") = Ws1.Cells(iClick, "C")

  'Fixed rows
  Ws3.Cells(DestRow3, "N") = Ws2.Range("B13")
  Ws3.Cells(DestRow3, "L") = Ws2.Range("H13")
  Ws3.Cells(DestRow3, "J") = Ws2.Range("I28")
  Ws3.Cells(DestRow3, "K") = Ws2.Range("H15")
  Ws4.Cells(DestRow4, "D") = Ws2.Range("B13")
  Ws4.Cells(DestRow4, "E") = Ws2.Range("B14")

  MsgBox ("Invoice details copied to register")
  Application.ScreenUpdating = True
  
End Sub
______________________________

Some things you should know about this:

1. I changed your copy - pastespecial to simple assignments because assignments are simpler where a single cell is involved and only values are needed.

2. I introduced a separate DestRow4 variable for Ws4 to keep it from overwriting DestRow3.

3. I introduced an iClick click count variable and used this to directly count the row number.  I also changed the Range() concatenated string references to Cells() references as these are designed to directly work with integer row and column indices without the need for concatenation.  The iClick variable is declared as Static so that it will retain its value between calls. When the workbook opens its value will be initialized by Excel to zero, which is why I test to see whether it is zero.  I should caution you that if you close and re-open Excel it will not remember the value from before so will again start at row 4.  If you want it to retain the count across Excel close/re-open you would need to put this value in a cell rather than in a static variable.

4. I did not test this code because of not having the worksheets to test it with as you do.  If it does not work and you cannot see how to fix it feel free to email your workbook to me and I will debug and fix it.  My email is VBAexpert@myway.com.

Good luck.

Damon  
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


Damon Ostrander

Expertise

I have extensive experience with VBA programming in Excel 5 through Excel 2013. As a former aerospace engineer with a large aerospace corporation and consultant in a small defense technology services company, I have developed a wide range of applications in VBA, including simulations involving mixed-language programming, satellite orbit mechanics, graphics and animation, and real-time applications. I am interested in moderate to hard VBA-related questions only.

Experience

I have developed and taught several courses in Excel VBA programming and also VBA programming in Office 97, 2000, and 2007. I have developed a number of large technical applications in Excel VBA for use within the aerospace industry.

Education/Credentials
B.S. in Electrical Engineering and Computer Science, University of California, Berkeley.

©2016 About.com. All rights reserved.