+ Reply to Thread
Results 1 to 9 of 9

if cell = x then copy paste info in K1 or k2, etc..If cell = y, then copy info in D1,d2, e

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    04-26-2013
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    167

    if cell = x then copy paste info in K1 or k2, etc..If cell = y, then copy info in D1,d2, e

    Hi,

    I would like to do the following:

    Column D has 4 categories. When lets say, D1 is written "Matched". I would like the info in K1 to be copied in another sheet.

    When Column D give me the categories "No Match" (Let's say D5 is a no match) I would like the info in G5 to be copied in another sheet.


    Easy enough?...i cant do it ...*cry*

  2. #2
    Valued Forum Contributor tehneXus's Avatar
    Join Date
    04-12-2013
    Location
    Hamburg, Germany
    MS-Off Ver
    Work: MS-Office 2010 32bit @ Win8 32bit / Home: MS-Office 2016 32bit @ Win10 64bit
    Posts
    944

    Re: if cell = x then copy paste info in K1 or k2, etc..If cell = y, then copy info in D1,d

    Hi,

    adapt the name of the target sheet. The values will be copied into column A:
    Sub test()
        Dim xlWs As Worksheet
        Dim i As Long, lngNextRow As Long
        
        On Error GoTo test_ErrorHandler
        Application.ScreenUpdating = False
        
        Set xlWs = Worksheets("Another Sheet")  'adapt sheetname for target sheet
        With xlWs
            lngNextRow = .Cells(.Rows.Count, 1).End(xlUp).Row + 1   'will be pasted into next free row in target sheet
        End With
        With ActiveSheet
            For i = 2 To .Cells(.Rows.Count, 4).End(xlUp).Row
                Select Case .Cells(i, 4).Value
                    Case "Matched"
                        xlWs.Cells(lngNextRow, 1).Value = .Cells(i, 11).Value   'from K
                        lngNextRow = lngNextRow + 1
                    Case "No Match"
                        xlWs.Cells(lngNextRow, 1).Value = .Cells(i, 8).Value    'from G
                        lngNextRow = lngNextRow + 1
                    Case Else
                        
                End Select
            Next i
        End With
    
    test_Proc_Exit:
        Application.ScreenUpdating = True
        Exit Sub
    test_ErrorHandler:
        MsgBox "Error: " & Err.Number & " (" & Err.Description & ") in Sub 'test' of Module 'Module1'.", vbOKOnly + vbCritical, "Error"
        Resume test_Proc_Exit
    End Sub
    Please use [CODE]-TAGS
    When your problem is solved mark the thread SOLVED
    If an answer has helped you please click to give reputation
    Read the FORUM RULES

  3. #3
    Forum Contributor
    Join Date
    04-26-2013
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    167

    Re: if cell = x then copy paste info in K1 or k2, etc..If cell = y, then copy info in D1,d

    macro doesnt work...It doesnt do anything!!!! There is no change recorded

  4. #4
    Valued Forum Contributor tehneXus's Avatar
    Join Date
    04-12-2013
    Location
    Hamburg, Germany
    MS-Off Ver
    Work: MS-Office 2010 32bit @ Win8 32bit / Home: MS-Office 2016 32bit @ Win10 64bit
    Posts
    944

    Re: if cell = x then copy paste info in K1 or k2, etc..If cell = y, then copy info in D1,d

    Hi,

    I wrote it according to your instructions in post #1, could you please upload a sample workbook?

  5. #5
    Forum Contributor
    Join Date
    04-26-2013
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    167

    Re: if cell = x then copy paste info in K1 or k2, etc..If cell = y, then copy info in D1,d

    Book1.xlsmHi,

    I would like to do the following:

    Column D has 2 categories (match and not match). When lets say, D2 is written "Matched". I would like the info in K2 to be copied in result sheet at a specific place but only if the number is not zero. If there is a match and the number in K2 is 0. I would like to copy the number from G2 instead.

    When Column D give me the categories "No Match" (Let's say D5 is a no match) I would like the info in G5 to be copied in result sheet.



    And this, I would like to create a loop that will go trough a list. I put the file in attachBook1.xlsmment

  6. #6
    Forum Contributor
    Join Date
    04-26-2013
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    167

    Re: if cell = x then copy paste info in K1 or k2, etc..If cell = y, then copy info in D1,d

    sorry for the confusion..and thanks for the help

  7. #7
    Valued Forum Contributor tehneXus's Avatar
    Join Date
    04-12-2013
    Location
    Hamburg, Germany
    MS-Off Ver
    Work: MS-Office 2010 32bit @ Win8 32bit / Home: MS-Office 2016 32bit @ Win10 64bit
    Posts
    944

    Re: if cell = x then copy paste info in K1 or k2, etc..If cell = y, then copy info in D1,d

    Hi

    I assume the headings in the Result sheet correspond to the values in column B of the source sheet? if this is true try:
    Option Explicit
    
    Sub test()
        Dim xlWs As Worksheet
        Dim xlRng As Range
        Dim i As Long, lngNextRow As Long
        
        On Error GoTo test_ErrorHandler
        Application.ScreenUpdating = False
        
        Set xlWs = Worksheets("Result")  'adapt sheetname for target sheet
        With ActiveSheet
            For i = 2 To .Cells(.Rows.Count, 4).End(xlUp).Row
                Select Case .Cells(i, 4).Value
                    Case "MATCH"    'from K
                        If .Cells(i, 11).Value <> 0 Then
                            Set xlRng = xlWs.Rows(1).Find(WHat:=.Cells(i, 2).Value, LookIn:=xlValues, lookat:=xlWhole)
                            If Not xlRng Is Nothing Then
                                xlWs.Cells(xlWs.Cells(xlWs.Rows.Count, xlRng.Column).End(xlUp).Row + 1, xlRng.Column).Value = .Cells(i, 11).Value
                            Else
                                Err.Raise vbObjectError + 512
                            End If
                        End If
                    Case "NO MATCH"     'from G
                        If .Cells(i, 8).Value <> 0 Then
                            Set xlRng = xlWs.Rows(1).Find(WHat:=.Cells(i, 2).Value, LookIn:=xlValues, lookat:=xlWhole)
                            If Not xlRng Is Nothing Then
                                xlWs.Cells(xlWs.Cells(xlWs.Rows.Count, xlRng.Column).End(xlUp).Row + 1, xlRng.Column).Value = .Cells(i, 8).Value
                            Else
                                Err.Raise vbObjectError + 512
                            End If
                        End If
                    Case Else
                        
                End Select
            Next i
        End With
    
    test_Proc_Exit:
        Application.ScreenUpdating = True
        Exit Sub
    test_ErrorHandler:
        MsgBox "Error: " & Err.Number & " (" & Err.Description & ") in Sub 'test' of Module 'Module1'.", vbOKOnly + vbCritical, "Error"
        Resume test_Proc_Exit
    End Sub

  8. #8
    Forum Contributor
    Join Date
    04-26-2013
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    167

    Re: if cell = x then copy paste info in K1 or k2, etc..If cell = y, then copy info in D1,d

    codding send me to error!!

  9. #9
    Valued Forum Contributor tehneXus's Avatar
    Join Date
    04-12-2013
    Location
    Hamburg, Germany
    MS-Off Ver
    Work: MS-Office 2010 32bit @ Win8 32bit / Home: MS-Office 2016 32bit @ Win10 64bit
    Posts
    944

    Re: if cell = x then copy paste info in K1 or k2, etc..If cell = y, then copy info in D1,d

    Hi,

    1. Does the error occur in the sample file from post #5?
    2. What is the error message?
    3. Was there anything copied?

+ 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