Split a number in an Excel table two cells by condition

Source: Internet
Author: User

There's a friend. The A1 cells and B1 cells in the Excel worksheet have two digits, and the two numbers are the same, now you want to find the same number and write to cell C1, find the numbers in the A1 that are not in the B1 and write to cell D1. Find the numbers in the B1 that are not in the A1 and write to cell E1.

As in the following worksheet picture:

  

I don't know whether the numbers given are the same rule, that is, the number of the first raw data is the same as the previous number of the second original data. If this is the case, then the following code can be given to this specific example:


Sub separatenumber ()
     Dim Strfirst as String
     Dim strresult as String
     Dim startnum as Integer
      Dim endnum as   String
     Dim i as Integer , J as Integer
     Strfirst = Left (range ("B1″), 1"
    startnum = InStr (1, Range ("A1″), Strfirst)
     j = 1
     for i = Startnum to Len (Range ("A1″)"
         endnum = Mid (Range ("A1″), I, 1"
          If endnum = Left (Range (B1″), j) Then
            j = j + 1
          End If
     Next I
     If J > 1 Then
         strresult = Mid (Range (A1″), Startnum, i-1)
     end If Data
    range ("C1″)" in the
     cell C1. Value = Strresult data
    range ("D1″") in the
     cell D1. Value = Left (Range (A1″), StartNum-1)
     cell E1 data
     range ("E1″"). Value = Right (range (B1″), Len (range (B1″)-j)
end Sub
code is simple, but uses several VBA functions.
discussion:

    • In fact, the code can be further simplified because VBA also has an array function (the Split function).
    • If you want to make it generic, you can convert the above code into a custom function and replace the hard-coded code with a relative amount.
    • A number in two cells if you don't follow the rules mentioned above, you might have the same number in the middle of two cells, and the rest of the numbers in one cell, or the middle part of the number in the other cell, and so on.

Interested friends can continue to study.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.