+ Reply to Thread
Results 1 to 8 of 8

Concatenate with several criteria and looping

Hybrid View

  1. #1
    Registered User
    Join Date
    08-08-2013
    Location
    Texas
    MS-Off Ver
    Excel 2010
    Posts
    46

    Concatenate with several criteria and looping

    Hey all,

    So I was given a somewhat small data set of 3500 rows by 10 columns.
    The data set is very dirty. I need to clean it up as it is a mailing list.

    So here is what I want to do

    if columns B:G have values then concatenate columns (b," ","and",e," ", G) in cell __

    if columns B,C,D have valuees, but columns e,f,g are blank then concatenate b,c,d in cell ___

    if I can understand syntax for the above I can create the rest of the conditions on my own

    Then I need to loop this code down each row one at a time.

    I really want to understand the syntax so that I don't have to bother anyone in the future for conditional syntax.

    Thank you in advance for your help,
    Martinez

  2. #2
    Forum Expert Solus Rankin's Avatar
    Join Date
    05-24-2013
    Location
    Hollywood, CA
    MS-Off Ver
    Win7 Office 2010 VS Express 2012
    Posts
    2,655

    Re: Concatenate with several criteria and looping

    if columns B:G have values then concatenate columns (b," ","and",e," ", G) in cell __
    Columns B & G or B thru G? What are you trying concatenate? and which cell is it supposed to go into?
    Thanks,
    Solus


    Please remember the following:

    1. Use [code] code tags [/code]. It keeps posts clean, easy-to-read, and maintains VBA formatting.
    Highlight the code in your post and press the # button in the toolbar.
    2. Show appreciation to those who have helped you by clicking below their posts.
    3. If you are happy with a solution to your problem, mark the thread as [SOLVED] using the tools at the top.

    "Slow is smooth, smooth is fast."

  3. #3
    Registered User
    Join Date
    08-08-2013
    Location
    Texas
    MS-Off Ver
    Excel 2010
    Posts
    46

    Re: Concatenate with several criteria and looping

    Correct B through G and I want the output to go to column A

    I am trying to concatenate names. There is a column for husband and wife prefix, first name, and last name each.
    Depending on what is present I need to concatenate those columns in a specific order as shown in my example.

    I hope that clarifies
    Thanks


    Quote Originally Posted by XeRo Solus View Post
    Columns B & G or B thru G? What are you trying concatenate? and which cell is it supposed to go into?
    Last edited by martinez77; 08-13-2013 at 11:42 PM.

  4. #4
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,960

    Re: Concatenate with several criteria and looping

    To attach a Workbook
    (please do not post pictures of worksheets)
    • Click Advanced (next to quick post),
    • Scroll down until you see "Manage Attachments",
    • Click that then select "add files" (top right corner).
    • Click "Select Files" find your file, click "open" click "upload"
    • Once the upload is completed the file name will appear below the input boxes in this window.
    • Click "Done" at bottom right to close the Attachment Manager.
    • Click "Submit Reply"
    Ben Van Johnson

  5. #5
    Forum Expert Solus Rankin's Avatar
    Join Date
    05-24-2013
    Location
    Hollywood, CA
    MS-Off Ver
    Win7 Office 2010 VS Express 2012
    Posts
    2,655

    Re: Concatenate with several criteria and looping

    Dim l As Long
    Dim lRow As Long
    Dim cel As Range
    Dim x As Integer
    
    lRow = Range("B" & Rows.Count).End(xlUp).Row
    
    For l = 1 To lRow
        x = 0
        For Each cel In Range("B" & l & ":G" & l)
            If cel.Value <> "" Then
                x = x + 1
            End If
        Next cel
        If x = 6 Then
            Range("A" & l).Value = Range("B" & l).Value & " " & Range("E" & l).Value & " " & Range("G" & l).Value
        Else:
            Range("A" & l).Value = Range("B" & l).Value & " " & Range("C" & l).Value & " " & Range("D" & l).Value
        End If
    Next l
        
    
    End Sub

  6. #6
    Registered User
    Join Date
    08-08-2013
    Location
    Texas
    MS-Off Ver
    Excel 2010
    Posts
    46

    Re: Concatenate with several criteria and looping

    OK so that code does exactly what I described which is stellar. I am having trouble following the logic.
    If I wanted to add statements how would I go about it. My goal is to understand the syntax also.

    if x = 6 then statement confuses me. Could you explain?

    I will need for example to add a statement that states

    if columns bcd are empty, but columns efg are not then concatenate efg in column a.

    what would that statement look like?

    Thanks again

    I need to add a few more statements

  7. #7
    Forum Expert Solus Rankin's Avatar
    Join Date
    05-24-2013
    Location
    Hollywood, CA
    MS-Off Ver
    Win7 Office 2010 VS Express 2012
    Posts
    2,655

    Re: Concatenate with several criteria and looping

    lRow = Range("B" & Rows.Count).End(xlUp).Row
    
    For l = 1 To lRow
    Begins to loop through all rows
        For Each cel In Range("B" & l & ":G" & l)
    loops through B:G in a row
    If cel.Value <> "" Then
                x = x + 1
            End If
    Counts each cell in B:G that is not empty
    If x = 6 Then
            Range("A" & l).Value = Range("B" & l).Value & " " & Range("E" & l).Value & " " & Range("G" & l).Value
    If none are empty in concatenates these values.
     Else:
            Range("A" & l).Value = Range("B" & l).Value & " " & Range("C" & l).Value & " " & Range("D" & l).Value
    Else if some are empty it concatenates these cells.

  8. #8
    Forum Expert Solus Rankin's Avatar
    Join Date
    05-24-2013
    Location
    Hollywood, CA
    MS-Off Ver
    Win7 Office 2010 VS Express 2012
    Posts
    2,655

    Re: Concatenate with several criteria and looping

    However, if you're going to add criteria you should probably use Select Case

+ 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. Looping using left function resulting in Concatenate
    By jaimie1664 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 07-12-2010, 08:12 AM
  2. looping through excel cells based on criteria using VBA
    By born2achieve in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-07-2008, 01:24 AM
  3. Looping two criteria - Beginner help
    By chris100 in forum Excel Programming / VBA / Macros
    Replies: 16
    Last Post: 10-30-2006, 02:29 PM
  4. Looping on Criteria
    By VBA Noob in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 08-14-2006, 04:23 PM
  5. Looping filter criteria
    By VBA Noob in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 08-13-2006, 01:00 PM

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.6.0 RC 1