+ Reply to Thread
Results 1 to 8 of 8

Merging two cells and separating data with symbol

Hybrid View

  1. #1
    Registered User
    Join Date
    10-08-2022
    Location
    Italy
    MS-Off Ver
    2019
    Posts
    14

    Merging two cells and separating data with symbol

    Hi all!

    I've a file (structure is always the same) with data that I need to merge and separate with "+".
    More precisely, in "Tutor" sheet you find columns with "Tutor 1", "Tutor 2", etc.
    For each tutor, for each day, there are two cells (both with/without text or only one of them).
    I need that always (also if they're empty) the 2 cells (for each day) are merged and, if both cells contain text, data must be separated by "+").

    In "Desidered" sheet you can find an example of what I ask.

    Please, can someone help me?

    Note 1: "Tutor 1, 2, etc" will be replaced by real name.
    Note 2: AC and AD columns can be deleted.
    Attached Files Attached Files
    Last edited by Natale; 06-22-2023 at 12:57 PM.

  2. #2
    Forum Expert
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    O365 32bit (Windows)
    Posts
    2,068

    Re: Merging two cells and separating data with symbol

    Something like this in the Worksheet code

    Sub test()
    Application.DisplayAlerts = False
    Application.ScreenUpdating = False
    For c = 3 To 55 Step 2
        If Cells(4, c) Like "Tutor*" Then
            For r = 5 To 35
                v2 = Trim(Cells(r, c + 1).Value)
                v1 = Trim(Cells(r, c).Value)
                v1 = v1 & IIf(Len(v2) And Len(v1), "+", "")
                
                With Range(Cells(r, c).Address & ":" & Cells(r, c + 1).Address)
                    .Merge
                    .HorizontalAlignment = xlCenter
                    .VerticalAlignment = xlCenter
                End With
                
                If Len(v1) Or Len(v2) Then
                    Cells(r, c) = v1 & v2
                End If
                DoEvents
            Next r
        End If
    Next c
    Range("AC:AD").EntireColumn.Delete
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
    MsgBox "Finished"

  3. #3
    Registered User
    Join Date
    10-08-2022
    Location
    Italy
    MS-Off Ver
    2019
    Posts
    14

    Re: Merging two cells and separating data with symbol

    Quote Originally Posted by ByteMarks View Post
    Something like this in the Worksheet code

    Sub test()
    Application.DisplayAlerts = False
    Application.ScreenUpdating = False
    For c = 3 To 55 Step 2
        If Cells(4, c) Like "Tutor*" Then
            For r = 5 To 35
                v2 = Trim(Cells(r, c + 1).Value)
                v1 = Trim(Cells(r, c).Value)
                v1 = v1 & IIf(Len(v2) And Len(v1), "+", "")
                
                With Range(Cells(r, c).Address & ":" & Cells(r, c + 1).Address)
                    .Merge
                    .HorizontalAlignment = xlCenter
                    .VerticalAlignment = xlCenter
                End With
                
                If Len(v1) Or Len(v2) Then
                    Cells(r, c) = v1 & v2
                End If
                DoEvents
            Next r
        End If
    Next c
    Range("AC:AD").EntireColumn.Delete
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
    MsgBox "Finished"
    Thank you very much, ByteMarks!
    It works perfectly! :D :D

  4. #4
    Registered User
    Join Date
    10-08-2022
    Location
    Italy
    MS-Off Ver
    2019
    Posts
    14

    Re: Merging two cells and separating data with symbol

    Ho, ByteMarks!

    Today I've tried your code in real file. It doesn't work because cells in row 4 contain real name, not "Tutor 1, 2, etc" (ad I wrote in first message).
    I'm trying to modify it but I can't.
    Suggestion, please?

  5. #5
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2406 Win 11 Home 64 Bit
    Posts
    23,983

    Re: Merging two cells and separating data with symbol

    Please don't quote whole posts -- it's just clutter. If you are responding to a post out of sequence, limit quoted content to a few relevant lines that makes clear to whom and what you are responding


    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED.

    Also, you may not be aware that you can thank those who have helped you by clicking the small star icon located in the lower left corner of the post in which the help was given. By doing so you can add to the reputation(s) of those who helped.
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  6. #6
    Forum Expert
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    O365 32bit (Windows)
    Posts
    2,068

    Re: Merging two cells and separating data with symbol

    Sorry.I missed that.
    Try deleting the columns first and then just checking for something in row 4

    Sub test()
    Application.DisplayAlerts = False
    Application.ScreenUpdating = False
    Range("AC:AD").EntireColumn.Delete
    For c = 3 To 53 Step 2
        If Len(Cells(4, c).Value) Then
            For r = 5 To 35
                v2 = Trim(Cells(r, c + 1).Value)
                v1 = Trim(Cells(r, c).Value)
                v1 = v1 & IIf(Len(v2) And Len(v1), "+", "")
                
                With Range(Cells(r, c).Address & ":" & Cells(r, c + 1).Address)
                    .Merge
                    .HorizontalAlignment = xlCenter
                    .VerticalAlignment = xlCenter
                End With
                
                If Len(v1) Or Len(v2) Then
                    Cells(r, c) = v1 & v2
                End If
                DoEvents
            Next r
        End If
    Next c
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
    MsgBox "Finished"

  7. #7
    Registered User
    Join Date
    10-08-2022
    Location
    Italy
    MS-Off Ver
    2019
    Posts
    14

    Re: Merging two cells and separating data with symbol

    Hi, ByteMarks!
    Don't worry, you've been precious again!
    It works but I find another strange problem: if I copy the result of macro in a new file, cells are copied 2 by 2, not like a unique cell (I don't know if i'm clear).

  8. #8
    Forum Expert
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    O365 32bit (Windows)
    Posts
    2,068

    Re: Merging two cells and separating data with symbol

    I'm not noticing anything unusual if I copy the results to a new file.

+ 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. Replies: 5
    Last Post: 11-08-2022, 03:46 PM
  2. Separating data into separate cells
    By lauracvp in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 03-16-2016, 12:39 PM
  3. Separating Line of Data in different Cells
    By area51plustax in forum Excel General
    Replies: 5
    Last Post: 08-21-2015, 12:14 AM
  4. [SOLVED] Separating multiple data (w delimiter) from one cell to many cells
    By rocketeer425 in forum Excel General
    Replies: 6
    Last Post: 02-14-2014, 03:56 AM
  5. Separating data with spaces and putting in two different cells
    By multimediocrity in forum Excel General
    Replies: 7
    Last Post: 02-19-2013, 01:52 AM
  6. Replies: 2
    Last Post: 08-14-2012, 01:19 PM
  7. Separating cell data into 2 new cells.
    By Joe@WSC in forum Excel - New Users/Basics
    Replies: 0
    Last Post: 07-07-2009, 10:10 AM

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