Consulting

Results 1 to 3 of 3

Thread: copy and paste macro

  1. #1
    VBAX Tutor
    Joined
    Aug 2007
    Posts
    294
    Location

    copy and paste macro

    Hi (see attached file)

    Need a Copy and Past macro that will do the following as shown in worksheet 2 i.e take the name Austria from column B row 2 and then the tasks in column A13:A59 (which are the same for all countries as list in row 5 columns B:AD)

    and also the data that relates to Austria (see column B13:B59) and insert it into worksheet 2 as shown in the example...................

    repeat steps for all countries in row 5 worksheet 1......and arrange in order A to Z

    The highlighted tasks in column B worksheet 2 for each country remain the same i.e Austria = column b4:b50 and belgium is column b52:b98 as shown in the example....

  2. #2
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,199
    Location
    Can you not just link cells or do the countries change all the time?
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved
    Click here for a guide on how to upload a file with your post

    Excel 365, Version 2403, Build 17425.20146

  3. #3
    VBAX Expert
    Joined
    Feb 2005
    Location
    Nanaimo, British Columbia, Cananda
    Posts
    568
    Location
    hi Pete,

    Here's a version. The Country names were in order so I didn't sort anything.

    [VBA]
    Option Explicit
    Sub CopyCountry()
    Dim ErrMsg As Long
    Dim FirstRow As Long
    Dim LastRow As Long
    Dim FirstCol As Long
    Dim LastCol As Long
    Dim Destrow As Long
    Dim iDestrow As Long
    Dim ColCount As Long
    Dim DestSht As Worksheet
    Dim SourceSht As Worksheet
    'Create objects
    Set DestSht = Sheet2
    Set SourceSht = Sheet1

    'Speed
    Application.ScreenUpdating = False

    'Set first row of Source data
    FirstRow = 13

    'Get last row of Source data
    LastRow = SourceSht.Range("A65536").End(xlUp).Row

    'Set first Col of Source data
    FirstCol = 2

    'Get last Col of Source data NOTE: Row 5
    LastCol = SourceSht.Range("IV5").End(xlToLeft).Column

    'Initialize
    Destrow = 2

    'Save for loop
    iDestrow = Destrow

    'Set up destination sheet
    With DestSht
    .Range("A:A").ColumnWidth = 66.14
    .Range("B:B").ColumnWidth = 15.14
    End With

    With SourceSht
    'Do all Columns
    For ColCount = FirstCol To LastCol
    'Copy 'steps'
    .Range("A" & FirstRow & ":A" & LastRow).Copy DestSht.Range("A" & Destrow)
    'Copy Country (Row 5)
    .Cells(5, ColCount).Copy DestSht.Cells(Destrow - 1, 2)
    'Copy Country info
    .Range(Cells(FirstRow, ColCount).Address, Cells(LastRow, ColCount).Address).Copy
    'Paste values and formats
    With DestSht.Cells(Destrow, 2)
    .PasteSpecial Paste:=xlPasteValues
    .PasteSpecial Paste:=xlPasteFormats
    End With
    'Increment based on size of list of 'steps' and first destination row number
    Destrow = Destrow + (LastRow - FirstRow) + iDestrow
    Next ColCount
    End With

    'Destroy objects
    Set DestSht = Nothing
    Set SourceSht = Nothing
    'Reset
    Application.ScreenUpdating = True

    'Normal exit
    Exit Sub

    'Error exit
    endo:

    'Destroy objects
    Set DestSht = Nothing
    Set SourceSht = Nothing
    'Reset
    Application.ScreenUpdating = True
    'Inform User
    ErrMsg = MsgBox("Error " & Err.Number & ". " & Err.Description)
    End Sub

    [/VBA]
    Cheers,

    dr

    "Questions, help and advice for free, small projects by donation. large projects by quote"

    http:\\www.ExcelVBA.joellerabu.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •