+ Reply to Thread
Results 1 to 3 of 3

Want to copy coulumns cells only if another cell in same row is equal to a value.

Hybrid View

  1. #1
    Registered User
    Join Date
    07-18-2013
    Location
    Cincinnati
    MS-Off Ver
    2010
    Posts
    35

    Want to copy coulumns cells only if another cell in same row is equal to a value.

    I need to modify this code I used in another workbook, to copy and paste columns, but leave out rows that do not contain "Yes" in a different column in the same row.

    Sub Button1_Click()
       '
       ' CopyOpenItems Macro
       ' Copy open items to sheet.
       '
       ' Keyboard Shortcut: Ctrl+Shift+O
       '
       Dim fDialog As Office.FileDialog
       Dim varFile As Variant
       
       Dim wbExport            As Workbook 'workbook from where the data is to Copied
       Dim wbImport            As Workbook 'workbook where the data is to be Pasted
       
       Dim wsExport            As Worksheet 'worksheet from where the data is to Copied
       Dim wsImport            As Worksheet 'workbook where the data is to be Pasted
       
       Dim strExportName             As String   'name of the workbook from where data is copied
       Dim strImportName             As String   'name of the workbook where the data is to be Pasted
       
       Dim lngLastRow As Long
       Dim lngLastRow2 As Long
       Dim intColNum As Integer
       Dim objTable1 As ListObject
    
        
        
       'open a workbook that has same name as the sheet name
       Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
       
       With fDialog
          If .Show = True Then
     
             'Add file chosen's name to a string.
             For Each varFile In .SelectedItems
                strExportName = varFile
             Next
     
          Else
             MsgBox "You clicked Cancel in the file dialog box."
             Exit Sub
          End If
       End With
       
       Set wbExport = Workbooks.Open(strExportName)
       Set wsExport = wbExport.Sheets(1)
       
       With wsExport
          lngLastRow = .Range("A" & .Rows.Count).End(xlUp).Row
       End With
       
       Set wbImport = ThisWorkbook
       Set wsImport = wbImport.ActiveSheet
       
       intColNum = WorksheetFunction.Match("FLEET_ID", wsExport.Range("1:1"), 0)
       
       wsExport.Range(XL_ColToLetter(intColNum) & "2:" & XL_ColToLetter(intColNum) & lngLastRow).Copy
       wsImport.Range("C4:C" & lngLastRow).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
    
       With wsImport
          lngLastRow2 = .Range("C" & .Rows.Count).End(xlUp).Row   'How many rows for table resize?
       End With
       
       Set objTable1 = wsImport.ListObjects(1)
     
       objTable1.Resize Range("A3:BY" & lngLastRow2)
    
       wbExport.Close
    End Sub
    Public Function XL_ColToLetter(ColumnNumber As Integer) As String
        If ColumnNumber < 27 Then
             ' Columns A-Z
            XL_ColToLetter = Chr(ColumnNumber + 64)
        Else
            XL_ColToLetter = Chr(Int((ColumnNumber - 1) / 26) + 64) & _
            Chr(((ColumnNumber - 1) Mod 26) + 65)
        End If
    End Function
    Basically I want it to take each cell in a column, and copy and paste if in another column, same row has "Yes".

    Example:

    ID-----------Successful?
    Fred---------Yes
    Bob----------No
    John---------No
    Jake---------Yes

    So it would copy Fred and Jake and paste those values into a worksheet on another workbook.

    I am guessing I need to use some sort of for statement?
    Last edited by Jhail83; 01-13-2014 at 02:23 PM.

  2. #2
    Registered User
    Join Date
    07-18-2013
    Location
    Cincinnati
    MS-Off Ver
    2010
    Posts
    35

    Re: Want to copy coulumns cells only if another cell in same row is equal to a value.

    For the Copying and Pasting I have this:

    
    For i = 1 To wsExport.Cells(Rows.Count, "A").End(xlUp).Row
                If wsExport.Cells(i, intColNum).Value = "Y" Then
                    intColNum2 = WorksheetFunction.Match("FLEET_ID", wsExport.Range("1:1"), 0)
                    wsExport.Cells(i, intColNum2).Copy
                    intColNum2 = WorksheetFunction.Match("FLEET_ID", wsImport.Range("1:1"), 0)
                    wsImport.Cells(intRowNum, 3).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
                    intRowNum = intRowNum + 1
                End If
        Next i
    I am unsure to as why it will not get the Column Index number for this line:
    intColNum2 = WorksheetFunction.Match("FLEET_ID", wsExport.Range("1:1"), 0)

    but not for this line:
    intColNum2 = WorksheetFunction.Match("FLEET_ID", wsImport.Range("1:1"), 0)

    wsImport is set to the active sheet. But it says "Run time error 1004. Unable to get the Match Function of the WorksheetProperty Class"

    I'd prefer not to hardcode the Index in, as there may be columns added. It is unlikely that the Column names will change.
    Last edited by Jhail83; 01-13-2014 at 03:18 PM.

  3. #3
    Registered User
    Join Date
    07-18-2013
    Location
    Cincinnati
    MS-Off Ver
    2010
    Posts
    35

    Re: Want to copy coulumns cells only if another cell in same row is equal to a value.

    Sorry for the triple post, but I actually want to refine my request for help.

    Copying cell by cell is slow, so I was wondering, could I just filter the "Success" column to only show rows with a "Y" value through VBA and copy and paste the remaining fields in the columns I want? What if I wanted to filter by that, and then filter by a date column for dates in a given year?
    Last edited by Jhail83; 01-13-2014 at 04:19 PM.

+ 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. [SOLVED] Copy entire row from one sheet to another if cells in column A are equal
    By Scal in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 06-12-2013, 12:27 PM
  2. Compare 2 cells if equal then copy...
    By stimea in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-08-2012, 06:14 AM
  3. Replies: 8
    Last Post: 10-19-2011, 06:17 PM
  4. Check if two cells are equal and copy,paste cell if true
    By solomeros in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-29-2011, 01:22 AM
  5. Check if two cells are equal, copy/paste adiacent cell
    By Macuil0101 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-15-2011, 12:40 PM

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