+ Reply to Thread
Results 1 to 5 of 5

Macro to copy, edit and paste data

Hybrid View

  1. #1
    Registered User
    Join Date
    06-18-2012
    Location
    DK
    MS-Off Ver
    Excel 2010
    Posts
    6

    Macro to copy, edit and paste data

    Hi all

    I am in need of a macro that can copy, edit and paste data from one sheet to another.
    Here are the details:

    Sheet 1 contains data in column 1 (variuos numbers of rows e.g. from A1:A3 or A1:A12).
    The text in A1 should then be copied to sheet 2 to field A1 and have the letter "A" added to the text.
    Again the data from A1 should be copied to sheet 2 to field A2 and have the letter "B" added to the text.
    The text in field A2 (sheet 1) should be copied to sheet 2 field A3 with the letter "A" after and to field A4 with the letter "B" after.
    and so on...
    Once the macro encounters a blank field in sheet 1 it should stop.

    Could anyone please help?

  2. #2
    Forum Expert
    Join Date
    06-09-2010
    Location
    Australia
    MS-Off Ver
    Excel 2013
    Posts
    1,714

    Re: Macro to copy, edit and paste data

    Hi try this macro
    it assumes that the data to copy start in row 1, the sheet to copy from is "Sheet1" and the sheet to copy to is "Sheet2" - change these variables if needed

    Sub copy_values()
    Dim sn As Long, dn As Long, SourceSheet As String, DestSheet As String
    SourceSheet = "Sheet1" 'sheet to copy from
    DestSheet = "Sheet2"  'sheet to copy to
    sn = 1 'first row of data to copy
    dn = 1 'first row to copy to on DestSheet
    
    Do While Sheets(SourceSheet).Columns(1).Rows(sn).Value <> ""
        Sheets(DestSheet).Columns(1).Rows(dn).Value = "A" & Sheets(SourceSheet).Columns(1).Rows(sn).Value
        Sheets(DestSheet).Columns(1).Rows(dn + 1).Value = "B" & Sheets(SourceSheet).Columns(1).Rows(sn).Value
        sn = 1 + sn
        dn = 2 + dn
    Loop
    End Sub

  3. #3
    Registered User
    Join Date
    06-18-2012
    Location
    DK
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: Macro to copy, edit and paste data

    Hi NickyC

    Your macro works fine but I need the text from sheet1 to be followed by the letter so the result in sheet2 looks like this 1A, 1B, 2A and so on.

  4. #4
    Forum Expert
    Join Date
    06-09-2010
    Location
    Australia
    MS-Off Ver
    Excel 2013
    Posts
    1,714

    Re: Macro to copy, edit and paste data

    ok try this:

    Sub copy_values()
    Dim sn As Long, dn As Long, SourceSheet As String, DestSheet As String
    SourceSheet = "Sheet1" 'sheet to copy from
    DestSheet = "Sheet2"  'sheet to copy to
    sn = 1 'first row of data to copy
    dn = 1 'first row to copy to on DestSheet
    
    Do While Sheets(SourceSheet).Columns(1).Rows(sn).Value <> ""
        Sheets(DestSheet).Columns(1).Rows(dn).Value = Sheets(SourceSheet).Columns(1).Rows(sn).Value & "A"
        Sheets(DestSheet).Columns(1).Rows(dn + 1).Value = Sheets(SourceSheet).Columns(1).Rows(sn).Value & "B"
        sn = 1 + sn
        dn = 2 + dn
    Loop
    End Sub

  5. #5
    Registered User
    Join Date
    06-18-2012
    Location
    DK
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: Macro to copy, edit and paste data

    Thank you very much, it works perfectly.
    Tina

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1