+ Reply to Thread
Results 1 to 4 of 4

cut and paste with a macro

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    07-10-2013
    Location
    Veendam
    MS-Off Ver
    Excel 2007
    Posts
    100

    cut and paste with a macro

    Hi,

    I have a file with two sheets "main" and "format". Now I'm always cut and paste to get it in the correct format (see the sheet format).

    Is this also possible with a VBA/Macro code?

    See my example.

    Greetings,

    Danielle

    test.xlsx

  2. #2
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: cut and paste with a macro

    Maybe:

    Sub Danielle22yy()
    Dim i As Long
    Dim y As Long
    Dim rcell As Range
    With Sheets("Sheet1")
        .Cells(1, 1) = "Generators"
        .Cells(1, 2) = "groups"
        .Cells(1, 3) = "sort"
        .Cells(1, 4) = "time"
    End With
    Sheets("Main").Activate
    For i = 4 To Range("A" & Rows.count).End(3).Row
    y = Sheets("Sheet1").Range("D" & Rows.count).End(3)(2).Row
    If Left(Range("A" & i), 3) = "Gen" Then
    Range("A" & i).Copy Sheets("Sheet1").Range("A" & y)
    Range(Cells(1, "B"), Cells(1, "M")).Copy
    Sheets("Sheet1").Range("B" & y).PasteSpecial Transpose:=True
    Range(Cells(2, "B"), Cells(2, "M")).Copy
    Sheets("Sheet1").Range("C" & y).PasteSpecial Transpose:=True
    Range(Cells(i, "B"), Cells(i, "M")).Copy
    Sheets("Sheet1").Range("D" & y).PasteSpecial Transpose:=True
    Else
    Range("A" & i).Copy Sheets("Sheet1").Range("C" & y)
    Range(Cells(1, "B"), Cells(1, "M")).Copy
    Sheets("Sheet1").Range("B" & y).PasteSpecial Transpose:=True
    Range(Cells(i, "N"), Cells(i, "Y")).Copy
    Sheets("Sheet1").Range("D" & y).PasteSpecial Transpose:=True
    End If
    Next i
    Sheets("Sheet1").Activate
        Range("A1:D" & Range("D" & Rows.count).End(3).Row).SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=R[-1]C"
    For Each rcell In Range("C2:C" & Range("B" & Rows.count).End(3).Row)
        If rcell.Value <> "testing" Then
            Range(Cells(rcell.Row, rcell.Column - 1), Cells(rcell.Row, rcell.Column)).Interior.ColorIndex = 44
        End If
    Next rcell
    End Sub

  3. #3
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: cut and paste with a macro

    Edited:

    Sub Danielle22yy()
    Dim i As Long
    Dim y As Long
    Dim rcell As Range
    With Sheets("format")
        .Cells(1, 1) = "Generators"
        .Cells(1, 2) = "groups"
        .Cells(1, 3) = "sort"
        .Cells(1, 4) = "time"
    End With
    Sheets("Main").Activate
    For i = 4 To Range("A" & Rows.count).End(3).Row
    y = Sheets("format").Range("D" & Rows.count).End(3)(2).Row
    If Left(Range("A" & i), 3) = "Gen" Then
    Range("A" & i).Copy Sheets("format").Range("A" & y)
    Range(Cells(1, "B"), Cells(1, "M")).Copy
    Sheets("format").Range("B" & y).PasteSpecial Transpose:=True
    Range(Cells(2, "B"), Cells(2, "M")).Copy
    Sheets("format").Range("C" & y).PasteSpecial Transpose:=True
    Range(Cells(i, "B"), Cells(i, "M")).Copy
    Sheets("format").Range("D" & y).PasteSpecial Transpose:=True
    Else
    Range("A" & i).Copy Sheets("format").Range("C" & y)
    Range(Cells(1, "B"), Cells(1, "M")).Copy
    Sheets("format").Range("B" & y).PasteSpecial Transpose:=True
    Range(Cells(i, "N"), Cells(i, "Y")).Copy
    Sheets("format").Range("D" & y).PasteSpecial Transpose:=True
    End If
    Next i
    Sheets("format").Activate
        Range("A1:D" & Range("D" & Rows.count).End(3).Row).SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=R[-1]C"
    For Each rcell In Range("C2:C" & Range("B" & Rows.count).End(3).Row)
        If rcell.Value <> "testing" Then
            Range(Cells(rcell.Row, rcell.Column - 1), Cells(rcell.Row, rcell.Column)).Interior.ColorIndex = 44
        End If
    Next rcell
    End Sub

  4. #4
    Forum Contributor
    Join Date
    07-10-2013
    Location
    Veendam
    MS-Off Ver
    Excel 2007
    Posts
    100

    Re: cut and paste with a macro

    Thanks a lot

    Greetings

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Auto run macro from paste or paste special in to specific cell
    By Craig Muir in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-27-2014, 10:00 AM
  2. [SOLVED] Simple copy and paste macro- Paste special help needed.
    By hernancrespo in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-20-2012, 07:02 AM
  3. RECORDED MACRO PASTE'S DATE DIFFERENTLY TO MANUAL PASTE
    By Pauldecan in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2005, 12:05 AM

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