+ Reply to Thread
Results 1 to 22 of 22

VBA Mid function: One sources to multiple cells

  1. #1
    Forum Contributor
    Join Date
    09-22-2012
    Location
    Malaysia
    MS-Off Ver
    Excel 2010
    Posts
    184

    VBA Mid function: One sources to multiple cells

    Hi all,

    I would like to know how to create a VBA that can do the above.

    Here is an example:

    The data before extract is "a=1,995.000 b=2,001.000 c=1,994.000 d=1,996.000 e=1,281".

    Need to extract number only to each cell, 1995 to column 1, 2001 to column 2 and so on.

    Please check the attachment out.midmultiplecell.xlsx

    Thank you.

  2. #2
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,811

    Re: VBA Mid function: One sources to multiple cells

    In VBA, look at the Split() function http://msdn.microsoft.com/en-us/libr...=vs.90%29.aspx . If you use "=" as the delimiter, you will split the string into several "x,xxx.xxx b". Then cycle through the resulting array, using the left() function on the appropriate elements to remove the trailing spaces and letters.

    If you split on spaces, you will get a series of a=xxxx.xxx and then you can split these again on the equals sign to get the number.
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  3. #3
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: VBA Mid function: One sources to multiple cells

    Try the attached.
    Attached Files Attached Files

  4. #4
    Forum Contributor
    Join Date
    09-22-2012
    Location
    Malaysia
    MS-Off Ver
    Excel 2010
    Posts
    184

    Re: VBA Mid function: One sources to multiple cells

    Mr.Shorty

    Thank you for your advice. I thought mid is the way but I was wrong.

  5. #5
    Forum Contributor
    Join Date
    09-22-2012
    Location
    Malaysia
    MS-Off Ver
    Excel 2010
    Posts
    184

    Re: VBA Mid function: One sources to multiple cells

    Quote Originally Posted by AB33 View Post
    Try the attached.
    Tested it. It working. Can you please explain so I can amend it myself? Instead take it raw I could learn something here. Here is the follow up question.

    1) Another problem I am having is some "=" come with "f". So the result is that particular number is skipped.

    For example:

    Data is "a=1,995.000 b=2,001.000 c=1,994.000 df1,996.000 e=1,281".
    After split; cell 1 = 1995, cell 2 = 2001, cell 3 = 1994, cell 4 = 1281 and cell 5 = "".

    2) Instead of having data in column H, what need to be change if I want to move data to column K.
    Last edited by DavidRoger; 08-26-2014 at 09:16 PM.

  6. #6
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: VBA Mid function: One sources to multiple cells

    You need to change H to K. Please see attached comment.
    Attached Files Attached Files

  7. #7
    Forum Contributor
    Join Date
    09-22-2012
    Location
    Malaysia
    MS-Off Ver
    Excel 2010
    Posts
    184

    Re: VBA Mid function: One sources to multiple cells

    Quote Originally Posted by AB33 View Post
    You need to change H to K. Please see attached comment.
    Have changed H to K but error.

    I have notice that there are some changes to the code. If the code is mean for when = is f, it is not working. The number with "f" still skipped.

    Can you explain the procedure of your code?

  8. #8
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: VBA Mid function: One sources to multiple cells

    I have added a line to exclude a null string value. Could you please attach a sample which showing you the wrong result? I need to see what is going on. The code worked on the sample.

  9. #9
    Forum Contributor
    Join Date
    09-22-2012
    Location
    Malaysia
    MS-Off Ver
    Excel 2010
    Posts
    184

    Re: VBA Mid function: One sources to multiple cells

    I have added macro splitmeupK.

    Please check the attachment if it is right.

    midmultiplecell2.xlsm

  10. #10
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: VBA Mid function: One sources to multiple cells

    David,
    I have got the same out put as per attached, so I do not see what was wrong with the first code.
    Am I missing something?
    Attached Files Attached Files

  11. #11
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: VBA Mid function: One sources to multiple cells

    Okay,
    I have now added another (New) code which is probably what you wanted. Try it
    Attached Files Attached Files

  12. #12
    Forum Expert
    Join Date
    07-20-2011
    Location
    Mysore, India.
    MS-Off Ver
    Excel 2019
    Posts
    8,577

    Re: VBA Mid function: One sources to multiple cells

    Pl see attached file with MID function.
    Attached Files Attached Files

  13. #13
    Forum Contributor
    Join Date
    09-22-2012
    Location
    Malaysia
    MS-Off Ver
    Excel 2010
    Posts
    184

    Re: VBA Mid function: One sources to multiple cells

    Tried your new code. It work and I have amended to my need.

    However, I notice one thing. The code run from the first row of the data to the last row.

    Is there a way to code just for one row at a times?

  14. #14
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: VBA Mid function: One sources to multiple cells

    Well, the code runs for rows which data in column H. You have data in 3 rows, the code will exit once it has reached row 3.
    It loops from row 1 to row 3.
    If you want to fix the loop

    Please Login or Register  to view this content.
    runs from J2 to J3.

  15. #15
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: VBA Mid function: One sources to multiple cells

    hi DavidRoger, option, please check attachment, press Run button or run code "test"(ALT+F8, select "test", press Run)
    Attached Files Attached Files

  16. #16
    Forum Contributor
    Join Date
    09-22-2012
    Location
    Malaysia
    MS-Off Ver
    Excel 2010
    Posts
    184

    Re: VBA Mid function: One sources to multiple cells

    Pls refer to #17.
    Attached Files Attached Files
    Last edited by DavidRoger; 08-27-2014 at 11:14 AM. Reason: double post

  17. #17
    Forum Contributor
    Join Date
    09-22-2012
    Location
    Malaysia
    MS-Off Ver
    Excel 2010
    Posts
    184

    Re: VBA Mid function: One sources to multiple cells

    Quote Originally Posted by AB33 View Post
    Please Login or Register  to view this content.
    runs from J2 to J3.
    Tried (K4:K10) and tested but doesn't work. It does not stop at K10. Attached is the amended copy. Could be I miss something.

    For my case, there is no need for loop. Just need to split at one row when I paste data in the column K.
    Attached Files Attached Files

  18. #18
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: VBA Mid function: One sources to multiple cells

    Should be

    Please Login or Register  to view this content.
    Which means from K4 to K10

  19. #19
    Forum Contributor
    Join Date
    09-22-2012
    Location
    Malaysia
    MS-Off Ver
    Excel 2010
    Posts
    184

    Re: VBA Mid function: One sources to multiple cells

    Below is referring to the attachment

    Referring to your code,
    Please Login or Register  to view this content.
    .

    I keep the front part
    Please Login or Register  to view this content.
    and deleted the back part
    Please Login or Register  to view this content.
    . But the code has to change every time new data is insert, K4 to K5 and so on.

    Need something like
    Please Login or Register  to view this content.
    which doesn't work.

    Please Login or Register  to view this content.
    refers to cell K3 in the Excel. Cell K3 is formulated so the code work on the last data I key in.

    For the attachment, I have change K4 to K10 for testing.
    Attached Files Attached Files

  20. #20
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: VBA Mid function: One sources to multiple cells

    David,
    I have spent over 3 days on this thread.
    As I said, you have 2 options with range. You can have fixed range
    For Each r In Range("k4:k10")
    which means the loop is fixed . It goes from row 4 to row 10.
    The second option is variable range
    For Each r In Range("K4", Range("K" & Rows.Count).End(xlUp))
    it goes down from row 4 to the last row in column K. The last row in column K could be in row 10, 20 , 40 or 1m.
    If you are struggling with basic loop, I do not know how are you going to adjust the rest of the code.

  21. #21
    Forum Contributor
    Join Date
    09-22-2012
    Location
    Malaysia
    MS-Off Ver
    Excel 2010
    Posts
    184

    Re: VBA Mid function: One sources to multiple cells

    Hi AB33,

    I have to thank you for your code and your every patient. I know I am asking the impossible. But I just have to know.

    After a day of studying your new code, I have learn new thing. I have concluded my finding
    Please Login or Register  to view this content.
    . I attached here for your finding.

    I thought I can conclude my work here but at the last moment I found out there is one more problem to solve. If I have not prepared for other to read the attachment, I won't know.

    It doesn't split if there is no "," or ".0" in the data. ie, e=100, f=999.

    It's ok if you don't want to answer this.

    Again thank you.
    Attached Files Attached Files

  22. #22
    Forum Contributor
    Join Date
    09-22-2012
    Location
    Malaysia
    MS-Off Ver
    Excel 2010
    Posts
    184

    Re: VBA Mid function: One sources to multiple cells


+ 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. [SOLVED] Function to Gather Data from Multiple Sources
    By Office_Worker83 in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 10-25-2012, 01:58 AM
  2. Find Multiple Occurences from Multiple Sources
    By erome in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 10-24-2012, 01:16 PM
  3. VBA Pivot Table - Multiple Tables - Multiple Sources of Data
    By ryanmorris in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-08-2012, 03:13 PM
  4. Replies: 0
    Last Post: 12-12-2011, 07:27 PM
  5. Lookup with multiple sources
    By saved1ne in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 01-21-2011, 01:32 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