Trying to merge cells across with * at the start of the text

Darren Smith

Well-known Member
Joined
Nov 23, 2020
Messages
631
Office Version
  1. 2019
Platform
  1. Windows
Find the * in a text string then merge the text across the columns.
At the moment it seems to just merge a load of random cells

VBA Code:
Sub MergeCells()
    Dim myFirstRow As Long
    Dim myLastRow As Long
    Dim myCriteriaColumn As Long
    Dim myFirstColumn As Long
    Dim myLastColumn As Long
    Dim ws As Worksheet
    Dim myCriteria As String
    Dim iCounter As Long
 
    myFirstRow = 13
    myCriteriaColumn = 5
    myFirstColumn = 5
    myLastColumn = 14
    myCriteria = "Merge cells"
 
    Set ws = ThisWorkbook.Worksheets("Job Card with Time Analysis")
 
    With ws
 
        myLastRow = .Cells.Find(What:="*", LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
 
        For iCounter = myLastRow To myFirstRow Step -1
            If .Cells(iCounter, myCriteriaColumn).Value = myCriteria Then .Range(.Cells(iCounter, myFirstColumn), .Cells(iCounter, myLastColumn)).Merge
        Next iCounter
 
    End With
 

End Sub
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
There is nothing in your code that will cause it to fail as long as it is being used correctly.

Are you trying to merge cells that start with "*" or those that contain "Merge cells"? Those are 2 different scenarios, a partial match will require the use of InStr while an exact match of entire contents will not.

Also, If in vba is case sensitive, so "Merge cells" and "merge cells" are not the same.
 
Upvote 0
There is nothing in your code that will cause it to fail as long as it is being used correctly.

Are you trying to merge cells that start with "*" or those that contain "Merge cells"? Those are 2 different scenarios, a partial match will require the use of InStr while an exact match of entire contents will not.

Also, If in vba is case sensitive, so "Merge cells" and "merge cells" are not the same.
Are you trying to merge cells that start with "*" Yes
 
Upvote 0
In that case the variable, myCriteria should be "*", not "Merge cells". Also, as mentioned above, you need to use InStr for a partial match.

Note that I've used InStr(...) =1 in order to check that the * is the first character, without =1 it would return true for a * anywhere in the cell.
VBA Code:
Sub MergeCells()
    Dim myFirstRow As Long
    Dim myLastRow As Long
    Dim myCriteriaColumn As Long
    Dim myFirstColumn As Long
    Dim myLastColumn As Long
    Dim ws As Worksheet
    Dim myCriteria As String
    Dim iCounter As Long
 
    myFirstRow = 13
    myCriteriaColumn = 5
    myFirstColumn = 5
    myLastColumn = 14
    myCriteria = "*"
 
    Set ws = ThisWorkbook.Worksheets("Job Card with Time Analysis")
 
    With ws
 
        myLastRow = .Cells.Find(What:="*", LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
 
        For iCounter = myLastRow To myFirstRow Step -1
            If InStr(.Cells(iCounter, myCriteriaColumn).Value, myCriteria) = 1 Then .Range(.Cells(iCounter, myFirstColumn), .Cells(iCounter, myLastColumn)).Merge
        Next iCounter
 
    End With
 

End Sub
 
Upvote 0
It might be possible but it is not something that I would attempt to do personally. Merged cells are a bad idea at the best of times, trying to do something like that is going to be messy and unnecessarily complicated.
 
Upvote 0
What about making the text visible without merging the cells is that possible?
 
Upvote 0
As long as the cells to the right are empty the text (not numeric data) should be visible anyway, by default it will spill across empty cells.
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,244
Members
452,622
Latest member
Laura_PinksBTHFT

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