+ Reply to Thread
Results 1 to 5 of 5

Thread: VBA: Copy cell if specific column meets 2 conditions

  1. #1
    Registered User
    Join Date
    01-23-2012
    Location
    UK
    MS-Off Ver
    Excel 2007
    Posts
    2

    VBA: Copy cell if specific column meets 2 conditions

    Hi,

    Would very much appreciate your help with this VBA excel query.
    I am using Excel 2007.

    Looking for VBA code that will progress through many rows to check if 2 conditions are met:

    1) Specific column is NOT blank
    AND
    2) Specific column contains an integer i such that 0 < i < 1

    if so, copy certain cells from that row to a second worksheet, repeating until row containing "THE END"

    I have broken down the 'logic' as follows.

    1) Popup a Textbox, Input text = ColumnLetter ("Enter Column Letter. e.g. AA")
    2) Popup a Textbox, Input text = EndRow ("Enter End Row Number. e.g. 2000")
    3) Set ColumnLetter as a string
    4) Set EndRow as a string
    5) Define the InputSheet as "InputSheetName"
    6) Define the OutputSheet as "OutputSheetName"
    7) If Column J is NOT blank AND "ColumnLetter" is integer 0 < i < 1.0

    Then copy Column J on InputSheet to Column B on OutputSheet
    Then copy Column C on InputSheet to Column C on OutputSheet
    Then copy Column F on InputSheet to Column D on OutputSheet
    Then copy Column E on InputSheet to Column E on OutputSheet
    Then copy "ColumnLetter" on InputSheet to Column F on OutputSheet

    Repeat from Row 4 to "EndRow"

    I have also attached a much simpler sample worksheet.
    Appreciate that autofilter would work for a simple example, however the actual data sheet has ~ 50 columns and 5000 rows

    Many thanks for any advice or assistance.
    Attached Files Attached Files

  2. #2
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Cochrane,Alberta
    MS-Off Ver
    XL 2003,2007,2010
    Posts
    6,843

    Re: VBA: Copy cell if specific column meets 2 conditions

    Hi Aakron,
    Your example workbook should match your question.
    The workbook should show your original layout and then your desired results, 20 or so rows would be sufficient for somebody to figure out what you are looking for.

  3. #3
    Valued Forum Contributor
    Join Date
    09-06-2011
    Location
    Indiana, USA
    MS-Off Ver
    Excel 2003, 2007, 2010
    Posts
    380

    Re: VBA: Copy cell if specific column meets 2 conditions

    Possibly...

    Sub Copy_on_2_Conditions()
    
    '3) Set ColumnLetter as a string
    '4) Set EndRow as a string
        Dim sColLetter As String, sEndRow As String
        Dim wksI As Worksheet, wksO As Worksheet
        Dim x           As Long
    
        '5) Define the InputSheet as "InputSheetName"
        Set wksI = Worksheets("InputSheet")
        '6) Define the OutputSheet as "OutputSheetName"
        Set wksO = Worksheets("OutputSheet")
    
        '1) Popup a Textbox, Input text = ColumnLetter ("Enter Column Letter. e.g. AA")
        sColLetter = InputBox("Enter Column Letter. e.g. AA")
        If Len(sColLetter) = 0 Then Exit Sub
    
        '2) Popup a Textbox, Input text = EndRow ("Enter End Row Number. e.g. 2000")
        sEndRow = InputBox("Enter End Row Number. e.g. 2000")
    
        With wksI
            'Repeat from Row 4 to "EndRow"
            For x = 4 To sEndRow
                '7) If Column J is NOT blank AND "ColumnLetter" is integer 0 < i < 1.0
                If Not IsEmpty(.Range("J" & x)) And _
                   .Range(sColLetter & x) > 0 And _
                   .Range(sColLetter & x) < 1 Then
    
                    'Then copy Column J on InputSheet to Column B on OutputSheet
                    wksO.Range("B" & x) = .Range("J" & x)
    
                    'Then copy Column C on InputSheet to Column C on OutputSheet
                    wksO.Range("C" & x) = .Range("C" & x)
    
                    'Then copy Column F on InputSheet to Column D on OutputSheet
                    wksO.Range("D" & x) = .Range("F" & x)
    
                    'Then copy Column E on InputSheet to Column E on OutputSheet
                    wksO.Range("E" & x) = .Range("E" & x)
    
                    'Then copy "ColumnLetter" on InputSheet to Column F on OutputSheet
                    wksO.Range("F" & x) = sColLetter
                End If
            Next x
        End With
    End Sub
    Last edited by dangelor; 01-24-2012 at 04:52 AM.

  4. #4
    Registered User
    Join Date
    01-23-2012
    Location
    UK
    MS-Off Ver
    Excel 2007
    Posts
    2

    Re: VBA: Copy cell if specific column meets 2 conditions

    Hi dangelor,

    That's really awesome, thanks very much.
    Had to make a few minor changes to correct my own oversight in the 'logic'
    This is my current version

    Sub Copy_on_2_Conditions()
    
    	Dim sColLetter As String, sEndRow As Integer
     	Dim wksI As Worksheet, wksO As Worksheet
        Dim FirstCellOfMerge As Range
        Dim x           As Long
    
        ' Define the InputSheet as "InputSheetName"
        Set wksI = Worksheets("InputSheet")
        ' Define the OutputSheet as "OutputSheetName"
        Set wksO = Worksheets("OutputSheet")
        
        Application.ScreenUpdating = False
        ' Popup a Textbox, Input text = ColumnLetter ("Enter Column Letter. e.g. AA")
        sColLetter = InputBox("Enter Column Letter. e.g. AA")
        If Len(sColLetter) = 0 Then Exit Sub
    
        ' Popup a Textbox, Input text = EndRow ("Enter End Row Number. e.g. 2000")
        sEndRow = InputBox("Enter End Row Number. e.g. 2000")
    
        With wksI
            'Repeat from Row 4 to "EndRow"
            For x = 4 To sEndRow
                ' If Column J is NOT blank AND "ColumnLetter" is integer 0 < i < 1.0 AND Ignore subtotal "Total Seats" row
                If Not IsEmpty(.Range("J" & x)) And _
                   .Range("J" & x) <> "Total Seats" And _
                   .Range(sColLetter & x) > 0 And _
                   .Range(sColLetter & x) <= 1 Then
    
                    'Then copy Column J on InputSheet to Column B on OutputSheet
                    wksO.Range("B" & x) = .Range("J" & x)
    
                    'Then copy Column C on InputSheet to Column C on OutputSheet
    				'Takes first value where Merged Cells are used
                    Set FirstCellOfMerge = Range("C" & x).MergeArea.Cells(1, 1)
                    wksO.Range("C" & x) = FirstCellOfMerge
    
                    'Then copy Column F on InputSheet to Column D on OutputSheet
                    wksO.Range("D" & x) = .Range("F" & x)
    
                    'Then copy Column E on InputSheet to Column E on OutputSheet
                    wksO.Range("E" & x) = .Range("E" & x)
    
                    'Then copy "ColumnLetter" on InputSheet to Column F on OutputSheet
                    wksO.Range("F" & x) = .Range(sColLetter & x)
                End If
            Next x
        End With
        wksO.Columns("A:F").EntireColumn.AutoFit
    End Sub
    Currently the 'Copy column to OutputSheet' keeps the x row number the same, this means that Sheet 2 can have lots of blanks
    I'm trying to sort out an autofilter on OutputSheet, range B2 to the last populated cell in column F
    Is there a neat way to define that range?

  5. #5
    Valued Forum Contributor
    Join Date
    09-06-2011
    Location
    Indiana, USA
    MS-Off Ver
    Excel 2003, 2007, 2010
    Posts
    380

    Re: VBA: Copy cell if specific column meets 2 conditions

    The way I prefer is to sort the Out list, sending the blank rows to the bottom or use .Specialcells (xlCellTypeBlanks) and delete entire rows.

    HTH
    -Rich

  6. #6
    Valued Forum Contributor
    Join Date
    09-06-2011
    Location
    Indiana, USA
    MS-Off Ver
    Excel 2003, 2007, 2010
    Posts
    380

    Re: VBA: Copy cell if specific column meets 2 conditions

    Quote Originally Posted by Aakron View Post
    Is there a neat way to define that range?
    Try...
    Range ("a4:f" & Range("f" & Rows.Count).End(xlUp).Row)

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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