Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Visual Basic
  4. Split cells with comma and space

Split cells with comma and space

Scheduled Pinned Locked Moved Visual Basic
collaborationquestion
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    manju 3
    wrote on last edited by
    #1

    Hi Team, I am trying to split the cells if comma and space . I tried below code.Its spliting for space now. How can i change it so that it will check for multiple(both space and comma)

    Dim splitvals As Variant
    Dim totalVals As Long

    Set sh1 = ThisWorkbook.Sheets(1)
    Set sh2 = ThisWorkbook.Sheets(2)

    sh2.Cells.Clear
    lrow1 = sh1.Range("A65356").End(xlUp).Row

    For j = 2 To lrow1
    splitvals = Split(sh1.Cells(j, 2), " ")
    For i = LBound(splitvals) To UBound(splitvals)
    lrow2 = sh2.Range("B65356").End(xlUp).Row
    lrow3 = sh2.Range("A65356").End(xlUp).Row
    sh2.Cells(lrow3 + 1, 1) = sh1.Cells(j, 1)
    sh2.Cells(lrow3 + 1, 2) = splitvals(i)
    Next i

    Next j

    Even i tried:

    splitvals = Split(sh1.Cells(j, 2), ", " " ")

    Please suggest me . Thanks Manju

    L M 2 Replies Last reply
    0
    • M manju 3

      Hi Team, I am trying to split the cells if comma and space . I tried below code.Its spliting for space now. How can i change it so that it will check for multiple(both space and comma)

      Dim splitvals As Variant
      Dim totalVals As Long

      Set sh1 = ThisWorkbook.Sheets(1)
      Set sh2 = ThisWorkbook.Sheets(2)

      sh2.Cells.Clear
      lrow1 = sh1.Range("A65356").End(xlUp).Row

      For j = 2 To lrow1
      splitvals = Split(sh1.Cells(j, 2), " ")
      For i = LBound(splitvals) To UBound(splitvals)
      lrow2 = sh2.Range("B65356").End(xlUp).Row
      lrow3 = sh2.Range("A65356").End(xlUp).Row
      sh2.Cells(lrow3 + 1, 1) = sh1.Cells(j, 1)
      sh2.Cells(lrow3 + 1, 2) = splitvals(i)
      Next i

      Next j

      Even i tried:

      splitvals = Split(sh1.Cells(j, 2), ", " " ")

      Please suggest me . Thanks Manju

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Most method calls operate on objects, but your Split call does not appear to be connected to any object. Where is the documentation for that method?

      M 1 Reply Last reply
      0
      • L Lost User

        Most method calls operate on objects, but your Split call does not appear to be connected to any object. Where is the documentation for that method?

        M Offline
        M Offline
        manju 3
        wrote on last edited by
        #3

        Dim splitvals As Variant
        Dim totalVals As Long
        Dim myData As Workbook
        Set myData = Workbooks.Open("D:\Test.xlsx")

        Set sh1 = myData.Sheets(1)
        Set sh2 = myData.Sheets(2)
        sh2.Cells.Clear
        lrow1 = sh1.Range("A65356").End(xlUp).Row

        For j = 2 To lrow1
        splitvals = Split(sh1.Cells(j, 2), ",")

        For i = LBound(splitvals) To UBound(splitvals)
            lrow2 = sh2.Range("B65356").End(xlUp).Row
            lrow3 = sh2.Range("A65356").End(xlUp).Row
            sh2.Cells(lrow3 + 1, 1) = sh1.Cells(j, 1)
            sh2.Cells(lrow3 + 1, 2) = splitvals(i)
        Next i   
        

        Next j

        Thanks Sharan

        L 1 Reply Last reply
        0
        • M manju 3

          Dim splitvals As Variant
          Dim totalVals As Long
          Dim myData As Workbook
          Set myData = Workbooks.Open("D:\Test.xlsx")

          Set sh1 = myData.Sheets(1)
          Set sh2 = myData.Sheets(2)
          sh2.Cells.Clear
          lrow1 = sh1.Range("A65356").End(xlUp).Row

          For j = 2 To lrow1
          splitvals = Split(sh1.Cells(j, 2), ",")

          For i = LBound(splitvals) To UBound(splitvals)
              lrow2 = sh2.Range("B65356").End(xlUp).Row
              lrow3 = sh2.Range("A65356").End(xlUp).Row
              sh2.Cells(lrow3 + 1, 1) = sh1.Cells(j, 1)
              sh2.Cells(lrow3 + 1, 2) = splitvals(i)
          Next i   
          

          Next j

          Thanks Sharan

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          That is just a repeat of your question. I asked you where the Split method is defined.

          Richard DeemingR 1 Reply Last reply
          0
          • L Lost User

            That is just a repeat of your question. I asked you where the Split method is defined.

            Richard DeemingR Offline
            Richard DeemingR Offline
            Richard Deeming
            wrote on last edited by
            #5

            It's a built-in VBA method: Split function (Visual Basic for Applications) | Microsoft Docs[^]


            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

            "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

            1 Reply Last reply
            0
            • M manju 3

              Hi Team, I am trying to split the cells if comma and space . I tried below code.Its spliting for space now. How can i change it so that it will check for multiple(both space and comma)

              Dim splitvals As Variant
              Dim totalVals As Long

              Set sh1 = ThisWorkbook.Sheets(1)
              Set sh2 = ThisWorkbook.Sheets(2)

              sh2.Cells.Clear
              lrow1 = sh1.Range("A65356").End(xlUp).Row

              For j = 2 To lrow1
              splitvals = Split(sh1.Cells(j, 2), " ")
              For i = LBound(splitvals) To UBound(splitvals)
              lrow2 = sh2.Range("B65356").End(xlUp).Row
              lrow3 = sh2.Range("A65356").End(xlUp).Row
              sh2.Cells(lrow3 + 1, 1) = sh1.Cells(j, 1)
              sh2.Cells(lrow3 + 1, 2) = splitvals(i)
              Next i

              Next j

              Even i tried:

              splitvals = Split(sh1.Cells(j, 2), ", " " ")

              Please suggest me . Thanks Manju

              M Offline
              M Offline
              Maciej Los
              wrote on last edited by
              #6

              Quote:

              How can i change it so that it will check for multiple(both space and comma)

              You can't pass multiple delimiters into VBA Split function. You have to call Split function again:

              For j = 2 To lrow1
              splitvals = Split(sh1.Cells(j, 2), " ")
              For i = LBound(splitvals) To UBound(splitvals)
              lrow2 = sh2.Range("B65356").End(xlUp).Row
              lrow3 = sh2.Range("A65356").End(xlUp).Row
              sh2.Cells(lrow3 + 1, 1) = sh1.Cells(j, 1)
              sh2.Cells(lrow3 + 1, 2) = splitvals(i)
              Next i

              splitvals = Split(sh1.Cells(j, 2), ",")    
              For i = LBound(splitvals) To UBound(splitvals)
                  lrow2 = sh2.Range("B65356").End(xlUp).Row
                  lrow3 = sh2.Range("A65356").End(xlUp).Row
                  sh2.Cells(lrow3 + 1, 1) = sh1.Cells(j, 1)
                  sh2.Cells(lrow3 + 1, 2) = splitvals(i)
              Next i   
              

              Next j

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups