+ Reply to Thread
Results 1 to 7 of 7

Thread: Copying & Pasting cells based on some other cells

  1. #1
    Registered User
    Join Date
    12-27-2011
    Location
    Mumbai,India
    MS-Off Ver
    Excel 2007
    Posts
    4

    Copying & Pasting cells based on some other cells

    HI,
    I have the following data :

    India Australia Europe CMC
    ABC 687 675 -9 -93
    EFG -98 429 876 284
    HIJ 98 257 479 424
    LMN 709 -0.07 78248 -4892
    OPQ 180 986 3910 78943

    I want pick the columns heads & row heads where the intersection value is negative & paste it in different sheet. Is it possible to do through macros. I want the final output as:
    A B
    India EFG
    Australia LMN
    Europe ABC
    CMC ABC
    CMC LMN

    Thanks
    Last edited by rohit1086; 12-27-2011 at 03:50 PM.

  2. #2
    Forum Guru jaslake's Avatar
    Join Date
    02-21-2009
    Location
    mineral city, ohio
    MS-Off Ver
    Excel 2007; Excel 2000
    Posts
    4,004

    Re: Copying & Pasting cells based on some other cells

    Hi rohit1086
    Welcome to the Forum!
    This code is in the attached and appears to do as you require
    Option Explicit
    Sub test()
        Dim LR As Long
        Dim LC As Long
        Dim NewLR As Long
        Dim i As Long
        Dim j As Long
    
        Application.ScreenUpdating = False
        Sheets("Sheet1").Cells.Copy
        Sheets("Sheet2").Range("A1").PasteSpecial
        With Sheets("Sheet2")
            .Activate
            LR = .Cells.Find("*", .Cells(Rows.Count, .Columns.Count), SearchOrder:=xlByRows, _
                    SearchDirection:=xlPrevious).Row
            LC = .Cells(1, .Columns.Count).End(xlToLeft).Column
            .Range("A2:A" & LR).Cut
            .Cells(2, LC + 2).Select
            ActiveSheet.Paste
            .Range("A2:A" & LR).Delete shift:=xlToLeft
        End With
        With Sheets("Sheet2")
            NewLR = LR + 2
            For i = 1 To LC
                For j = 2 To LR
                    If Cells(j, i).Value < 1 Then
                        .Range("A" & NewLR).Value = Cells(i).Value
                        .Range("B" & NewLR).Value = Cells(j, LC + 1).Value
                        NewLR = NewLR + 1
                    End If
                Next j
            Next i
            .Range("A1:A" & LR + 1).EntireRow.Delete
        End With
        Application.ScreenUpdating = True
    End Sub
    Attached Files Attached Files
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  3. #3
    Registered User
    Join Date
    12-27-2011
    Location
    Mumbai,India
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: Copying & Pasting cells based on some other cells

    Thanks a ton John. Just one issue i have here. In the data i have provided, the cell A1 is blank. The column heads start from B1. In this case what should be the code?

  4. #4
    Forum Guru jaslake's Avatar
    Join Date
    02-21-2009
    Location
    mineral city, ohio
    MS-Off Ver
    Excel 2007; Excel 2000
    Posts
    4,004

    Re: Copying & Pasting cells based on some other cells

    Hi rohit1086
    I created a worksheet from data you provided and the code was based on my perception of that. I don't know what your data looks like (I don't get the picture). I'll need to see what your data looks like to change the code. Please put it on a worksheet then attach the file. I'll be glad to look at it.
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  5. #5
    Registered User
    Join Date
    12-27-2011
    Location
    Mumbai,India
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: Copying & Pasting cells based on some other cells

    Hi jaslake,

    I have attached the data in the worksheet below. The first sheet in the attachement is the original data. The second worksheet is the desired output.

    Thanks
    Attached Files Attached Files
    Last edited by rohit1086; 12-28-2011 at 05:18 AM.

  6. #6
    Forum Guru jaslake's Avatar
    Join Date
    02-21-2009
    Location
    mineral city, ohio
    MS-Off Ver
    Excel 2007; Excel 2000
    Posts
    4,004

    Re: Copying & Pasting cells based on some other cells

    Hi rohit1086
    This code is in the attached and appears to do as you require
    Option Explicit
    Sub test()
        Dim LR As Long
        Dim LC As Long
        Dim NewLR As Long
        Dim i As Long
        Dim j As Long
    
        Application.ScreenUpdating = False
        Sheets("Sheet1").Cells.Copy
        Sheets("Sheet2").Range("A1").PasteSpecial
        With Sheets("Sheet2")
            .Activate
            .Cells.UnMerge
            LR = .Cells.Find("*", .Cells(Rows.Count, .Columns.Count), SearchOrder:=xlByRows, _
                    SearchDirection:=xlPrevious).Row
        .Range("A2:A" & LR).AutoFilter Field:=1, Criteria1:=Array( _
            "ASSETS", "LIABILITIES", "="), Operator:=xlFilterValues
        .UsedRange.Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
        .AutoFilterMode = False
            LC = .Cells(2, .Columns.Count).End(xlToLeft).Column
            .Range("A2:A" & LR).Cut
            .Cells(2, LC + 1).Select
            ActiveSheet.Paste
            .Columns("A:B").Delete Shift:=xlToLeft
            LR = .Cells.Find("*", .Cells(Rows.Count, .Columns.Count), SearchOrder:=xlByRows, _
                    SearchDirection:=xlPrevious).Row
            LC = .Cells(2, .Columns.Count).End(xlToLeft).Column
        End With
        With Sheets("Sheet2")
            NewLR = LR + 4
            For i = 1 To LC
                For j = 2 To LR
                    If Cells(j, i).Value < 0 Then
                        .Range("A" & NewLR).Value = Cells(2, i).Value
                        .Range("B" & NewLR).Value = Cells(j, LC).Value
                        NewLR = NewLR + 1
                    End If
                Next j
            Next i
            .Range("A1:A" & LR).EntireRow.Delete
        End With
        Application.ScreenUpdating = True
    End Sub
    Let me know of issues.
    Attached Files Attached Files
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  7. #7
    Registered User
    Join Date
    12-27-2011
    Location
    Mumbai,India
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: Copying & Pasting cells based on some other cells

    Thanks jaslake. This code solves the issue.

+ 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.2.0