VBA Merging Cells Help Needed

Tuffman

New Member
Joined
May 31, 2012
Messages
49
Office Version
  1. 2016
Platform
  1. Windows
I am trying to merge two cells that will be next to each other using VBA. I need to keep the row constant but the columns will change. I have tried using the two lines of code, but neither of them work. I would appreciate any insight into getting these to work. Thank you.

Worksheets("GT Quick Reference View").Range(Cells(1, j), Cells(1, j + 1)).Merge

Worksheets("GT Quick Reference View").Range("A" & j:"A" & j + 1).Merge
 
No worries Jeff. Happens to the best of us as you showed!!!!!
 
Upvote 0

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
You need to qualify properly.
Code:
Worksheets("GT Quick Reference View").Range(Worksheets("GT Quick Reference View").Cells(1, ColumnNum), Worksheets("GT Quick Reference View").Cells(1, ColumnNum + 1)).Merge

Or
at the top declare
Code:
Dim ws2 As Worksheet
Set ws2 = Worksheets("GT Quick Reference View")

more code here

ws2.Range(ws2.Cells(1, ColumnNum), ws2.Cells(1, ColumnNum + 1)).Merge

I did not go through all the code but the above stood out for me.
Try the changes and let us know either one way or the other.
I got your first recommendation to jolivanes. Thank you very much.

And yes the first time pasting is in C1.

Thanks again, and thank you Jeff as well.
 
Upvote 0
xlCellTypeLastCell includes cells that appear empty but have formatting without a value.
Use either one of these instead.
Code:
lr = Cells.Find("*", ,xlValues , , xlByRows, xlPrevious).Row = Values only
lr = Cells.Find("*", ,xlFormulas , , xlByRows, xlPrevious).Row = Includes Formulas


You have to change the references to your way of doing it. I just typed it as short as I could!!!
Try this on a copy of your original.
Not tested. A guess of what you need.
Code:
Sub Can_You_Try_This()
Dim cN As Long, rN As Long, maxrN As Long, sRng As Long, sStr As String
Dim ws1 As Worksheet, ws2 As Worksheet
Set ws1 = Worksheets("Test Design")
Set ws2 = Worksheets("GT Quick Reference View")
sStr = "GT"
maxrN = ws1.Cells.SpecialCells(xlCellTypeLastCell).Row
sRng = maxrN - 3818    '<---- why this? There should be a more robust way.
cN = 1
    For rN = 3 To sRng
        If ws1.Cells(rN, 2).Value = sStr Then
            ws1.Cells(rN, 1).Copy ws2.Cells(1, cN)
            ws2.Cells(1, cN).Resize(, 2).Merge
        End If
    cN = cN + 2
    Next rN
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,206
Members
452,618
Latest member
Tam84

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