You are here:

Excel/Excel VBA: Copy and Paste 2

Advertisement


Question
Hi Damon,

I have been searching for the right code to do the job and thank you so much for writing this up short and sweet. The only problem is that I have several source of worksheets in different names, e.g. Region A, Region B etc.. worksheets which i need to extract all the "Yes" related info and transferred to another sheet. The result would show a list of all the combined region "Yes" subscriber customer info.  The previous code you have written, I have modified alittle:

Sub CopyYes()

 'Copy cells of cols A,B,C,D,E,F,G from rows containing "Yes" in
 'col H of the active worksheet (source sheet) to cols
 'A,B,C,D,E,F,G of Sheet2 (destination sheet)
 Dim DestSheet        As Worksheet
 Set DestSheet = Worksheets("Sheet2")
 
 Dim sRow       As Long     'row index on source worksheet
 Dim dRow       As Long     'row index on destination worksheet
 Dim sCount     As Long
 sCount = 0
 dRow = 1

 For sRow = 1 To Range("D65536").End(xlUp).Row
    'use pattern matching to find "Yes" anywhere in cell
    If Cells(sRow, "H") Like "*Yes*" Then
       sCount = sCount + 1
       dRow = dRow + 1
       'copy cols A,B,C,D,E,F,G& H
       Cells(sRow, "A").Copy Destination:=DestSheet.Cells(dRow, "A")
       Cells(sRow, "B").Copy Destination:=DestSheet.Cells(dRow, "B")
       Cells(sRow, "C").Copy Destination:=DestSheet.Cells(dRow, "C")
       Cells(sRow, "D").Copy Destination:=DestSheet.Cells(dRow, "D")
       Cells(sRow, "E").Copy Destination:=DestSheet.Cells(dRow, "E")
       Cells(sRow, "F").Copy Destination:=DestSheet.Cells(dRow, "F")
       Cells(sRow, "G").Copy Destination:=DestSheet.Cells(dRow, "G")
       Cells(sRow, "H").Copy Destination:=DestSheet.Cells(dRow, "H")
    End If
 Next sRow
 
 MsgBox sCount & " Subscriber rows copied", vbInformation, "Transfer Done"

End Sub

Would be much grateful if you would help out alittle here.

Thank you very much  

Answer
Hi VaC,

I assume you mean that you want to loop through all the region sheets, performing the same operation with each sheet, appending the data to the destination sheet.  I have added such a loop to the code below.  Note that I used a For Each loop and an Array() function to define the collection of items.  You can put the list of region sheet names in this Array list, each name in quotes and separated by commas as I have done.  I believe this does what you want.

Sub CopyYes()

'Copy cells of cols A,B,C,D,E,F,G from rows containing "Yes" in
'col H of the region worksheet (source sheet) to cols
'A,B,C,D,E,F,G of Sheet2 (destination sheet)
Dim DestSheet  As Worksheet
Set DestSheet = Worksheets("Sheet2")
Dim Region     As Variant

Dim sRow As Long 'row index on source worksheet
Dim dRow As Long 'row index on destination worksheet
Dim sCount As Long
sCount = 0
dRow = 1

For Each Region In Array("Region A", "Region B", "Region C")

  With Worksheets(Region)
  
     For sRow = 1 To .Range("D65536").End(xlUp).Row
        'use pattern matching to find "Yes" anywhere in cell
        If .Cells(sRow, "H") Like "*Yes*" Then
           sCount = sCount + 1
           dRow = dRow + 1
           'copy cols A,B,C,D,E,F,G& H
           .Cells(sRow, "A").Copy Destination:=DestSheet.Cells(dRow, "A")
           .Cells(sRow, "B").Copy Destination:=DestSheet.Cells(dRow, "B")
           .Cells(sRow, "C").Copy Destination:=DestSheet.Cells(dRow, "C")
           .Cells(sRow, "D").Copy Destination:=DestSheet.Cells(dRow, "D")
           .Cells(sRow, "E").Copy Destination:=DestSheet.Cells(dRow, "E")
           .Cells(sRow, "F").Copy Destination:=DestSheet.Cells(dRow, "F")
           .Cells(sRow, "G").Copy Destination:=DestSheet.Cells(dRow, "G")
           .Cells(sRow, "H").Copy Destination:=DestSheet.Cells(dRow, "H")
           'DestSheet.Cells(dRow, "I") = Region   'Uncomment this line to add region in column I
        End If
     Next sRow
  
  End With
  
Next Region

MsgBox sCount & " Subscriber rows copied", vbInformation, "Transfer Done"

End Sub

Note my use of the With statement and the "." in front of the source worksheet range references because the source sheet is no longer assumed to be the active worksheet.  

Note also that I added a commented-out line to add the Region name to column I in the destination sheet.  Simply remove the first apostrophe to un-comment it if you want to do this.

Keep Excelling.

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 2007. I am an consultant in a small defense technology services company, and 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.

©2012 About.com, a part of The New York Times Company. All rights reserved.