Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Split text in cells with VBA
#1
Example:

Sub SplitTextInCells()
    Dim sourceRange As Range
    Dim cell As Range
    Dim parts As Variant

    'Set the range where the text is located (assuming data starts from A1)
    Set sourceRange = Range("A1:A5")

    'Loop through each cell in the range
    For Each cell In sourceRange
        'Split the text in the cell based on the delimiter
        parts = Split(cell.Value, ",")

        'Place the split parts in adjacent columns
        cell.Offset(0, 1).Resize(1, UBound(parts) + 1).Value = parts
    Next cell
End Sub

Explanation:

The code defines a subroutine named SplitTextInCells.
It declares variables for the source range (sourceRange), a loop variable (cell), and an array variable to hold the split parts (parts).
Set sourceRange = Range("A1:A5") specifies the range A1:A5 where the text is located.
The For Each loop is used to loop through each cell in the sourceRange.
The Split function is used to split the text in the cell based on the delimiter ("," in this example). 
The result is stored in the array variable parts.
The split parts are then placed in adjacent columns to the right of the original cell. 
cell.Offset(0, 1) refers to the cell next to the current cell, and Resize(1, UBound(parts) + 1) adjusts the size of the destination range to fit the number of split parts. 
The split parts are written to the destination range using .Value = parts.
Also follow me on Youtube for videos about video games:
https://www.youtube.com/channel/UCxfkGVU...2mQ/videos
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Format cells in a range with bold font and yellow background color Qomplainerz 0 333 07-27-2023, 11:26 AM
Last Post: Qomplainerz
  Count colored cells with VBA Qomplainerz 0 223 07-27-2023, 11:07 AM
Last Post: Qomplainerz
  Calculate the avg and color the cells when their values are above or below the avg Qomplainerz 0 266 07-27-2023, 10:41 AM
Last Post: Qomplainerz
  Protect specific cells with VBA Qomplainerz 0 252 07-27-2023, 10:39 AM
Last Post: Qomplainerz
  Format cells with conditional formatting in VBA Qomplainerz 0 239 07-27-2023, 08:16 AM
Last Post: Qomplainerz
  Loop to fill numbers in cells with VBA Qomplainerz 0 228 07-27-2023, 08:16 AM
Last Post: Qomplainerz
  Formatting cells with VBA Qomplainerz 0 213 07-27-2023, 06:31 AM
Last Post: Qomplainerz

Forum Jump:


Users browsing this thread: 1 Guest(s)