How to split text and merge in new row?

genetist

Board Regular
Joined
Mar 29, 2013
Messages
75
Office Version
  1. 2016
Platform
  1. Windows
Hi to all
I have data like this

SNP1SNP2SNP3SNP4SNP5SNP6
A/AT/TG/GC/CA/TG/C
G/GG/CG/GC/CT/TMISSING
A/GNAG/GC/CNANA

i have data in 2nd and 3rd rows of above table, i would like to have results like in 4th row. basically (in below table just example of 3 columns (SNP) i would like to split my data separated by / any cell not containing same letters like A/A, T/T, G/G, C/C i would like to give NA to those cells and then no work to do but i want them in final table. my final would be merge of split values like 4th row above table or 6th row of below table

SNP1SNP2SNP3
ATC
ATC
GNAC
GNAC
A/GNAC/C

I tried using concatenate formula =CONCATENATE(LEFT(B2,1),"/",LEFT(B3,1)) but i need to repeat this for right side text too, is there formula to split right and left and concatenate at the same time? any help in this regard is highly appreciated.
Thanks in advance
Regards
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Hi,

assuming your data is in rows 2 & 3 (starting column A), and is 6 columns across - the following VBA code might help get what you want. Output will start in row 4.
apologies I am not sure how to do in formulas ..

VBA Code:
Sub split_rows()

Dim row_num, Col_num As Long
Dim outpuit_val As String

For row_num = 2 To 3
    For Col_num = 1 To 6
        Mytext = ActiveSheet.Range(Cells(row_num, Col_num), Cells(row_num, Col_num)).Value
       
        If Left(Mytext, 1) = Right(Mytext, 1) Then
       
            output_val = Left(Mytext, 1)
        Else
            If UCase(Mytext) = "MISSING" Then
                output_val = "MISSING"
            Else
                output_val = "NA"
            End If
        End If
       
        If row_num = 2 Then
                Range(Cells(row_num + 2, Col_num), Cells(row_num + 2, Col_num)) = output_val
                Range(Cells(row_num + 3, Col_num), Cells(row_num + 3, Col_num)) = output_val
          
        Else 'row_num = 3
       
                Range(Cells(row_num + 3, Col_num), Cells(row_num + 3, Col_num)) = output_val
                Range(Cells(row_num + 4, Col_num), Cells(row_num + 4, Col_num)) = output_val
        End If
             
    Next Col_num
Next row_num

For Col_num = 1 To 6

    digit1 = Range(Cells(5, Col_num), Cells(5, Col_num))
    digit2 = Range(Cells(6, Col_num), Cells(6, Col_num))
   
    If digit1 <> "NA" And digit2 <> "NA" And digit1 <> "MISSING" And digit2 <> "MISSING" Then
   
        output_val = digit1 & "/" & digit2
    Else
        output_val = "NA"
   
    End If

    Range(Cells(8, Col_num), Cells(8, Col_num)) = output_val

Next Col_num

End Sub
 
Upvote 0
Solution
Dear
Hi,

assuming your data is in rows 2 & 3 (starting column A), and is 6 columns across - the following VBA code might help get what you want. Output will start in row 4.
apologies I am not sure how to do in formulas ..

VBA Code:
Sub split_rows()

Dim row_num, Col_num As Long
Dim outpuit_val As String

For row_num = 2 To 3
    For Col_num = 1 To 6
        Mytext = ActiveSheet.Range(Cells(row_num, Col_num), Cells(row_num, Col_num)).Value
      
        If Left(Mytext, 1) = Right(Mytext, 1) Then
      
            output_val = Left(Mytext, 1)
        Else
            If UCase(Mytext) = "MISSING" Then
                output_val = "MISSING"
            Else
                output_val = "NA"
            End If
        End If
      
        If row_num = 2 Then
                Range(Cells(row_num + 2, Col_num), Cells(row_num + 2, Col_num)) = output_val
                Range(Cells(row_num + 3, Col_num), Cells(row_num + 3, Col_num)) = output_val
         
        Else 'row_num = 3
      
                Range(Cells(row_num + 3, Col_num), Cells(row_num + 3, Col_num)) = output_val
                Range(Cells(row_num + 4, Col_num), Cells(row_num + 4, Col_num)) = output_val
        End If
            
    Next Col_num
Next row_num

For Col_num = 1 To 6

    digit1 = Range(Cells(5, Col_num), Cells(5, Col_num))
    digit2 = Range(Cells(6, Col_num), Cells(6, Col_num))
  
    If digit1 <> "NA" And digit2 <> "NA" And digit1 <> "MISSING" And digit2 <> "MISSING" Then
  
        output_val = digit1 & "/" & digit2
    Else
        output_val = "NA"
  
    End If

    Range(Cells(8, Col_num), Cells(8, Col_num)) = output_val

Next Col_num

End Sub
Dear RobP
Good Afternoon

Thanks for your valuable time and efforts to help to solve my problem, will this script will work to any number of columns lets my data is there up to 20 columns (rows always 2&3 only) after changing column number to 20.
Thanking you very much :)
 
Upvote 0
Hi,

yes, as long as you change both lines of code that refer to the col_num (1 to 6 should be 1 - 20 or 1 to whatever you want ).

Glad it worked for you.

cheers
Rob
 
Upvote 0
Dear RobP
Good Afternoon
Excellent its working fine after changing column numbers in both the places
Thanks a lot :)
 
Upvote 0

Forum statistics

Threads
1,214,591
Messages
6,120,429
Members
448,961
Latest member
nzskater

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top