[VBA] Merge Cells

ExcelNooberino

New Member
Joined
Jan 2, 2019
Messages
43
Office Version
  1. 2016
Platform
  1. Windows
Hi there everyone!

I was wondering if it is possible to merge cells through VBA code, cuz I've been trying to pull it off and it seems I'm getting nowhere, I probably need some extra help :laugh: So the thing is, I add two new rows at the same time with some data by clicking this button but I need to merge some of the cells in that specific row. Something like this:

Public Sub SumREGR_MAT()

Application.ScreenUpdating = False


Dim y2 As Integer
Dim Row2 As Integer


y2 = MAT_02.Application.WorksheetFunction.Max(Range("B:B"))
Row2 = MAT_02.Cells(Rows.Count, 3).End(xlUp).Offset(1, 0).Row


MAT_02.Cells(Row2, 2).Value = y2 + 1
MAT_02.Cells(Row2 + 1, 2).Value = y2 + 1
MAT_02.Cells(Row2, 3).Value = Now
MAT_02.Cells(Row2 + 1, 3).Value = Now
MAT_02.Cells(Row2, 4).Value = "=SUM(MAT_02_EXP!K:K)"
MAT_02.Cells(Row2 + 1, 4).Value = "=SUM(MAT_02_EXP!K:K)"
MAT_02.Cells(Row1, 5).Value = "ME5009AAAA0"
MAT_02.Cells(Row1 + 1, 5).Value = "ME5008AAAA2"
MAT_02.Cells(Row2, 6).Value = 0.5
MAT_02.Cells(Row2 + 1, 6).Value = 0.5

Application.ScreenUpdating = True

End Sub

What I need is to merge MAT_02.Cells(Row2, 3) and MAT_02.Cells(Row2 + 1, 3) for example...
 

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.
Try this

Code:
Public Sub SumREGR_MAT()


    Application.ScreenUpdating = False
    Dim y2 As Integer
    Dim Row2 As Integer
    
    y2 = MAT_02.Application.WorksheetFunction.Max(Range("B:B"))
    Row2 = MAT_02.Cells(Rows.Count, 3).End(xlUp).Offset(1, 0).Row
    
    MAT_02.Cells(Row2, 2).Value = y2 + 1
    MAT_02.Cells(Row2 + 1, 2).Value = y2 + 1
    'MAT_02.Cells(Row2, 3).Value = Now
    'MAT_02.Cells(Row2 + 1, 3).Value = Now
    
[COLOR=#0000ff]    MAT_02.Cells(Row2, 3).Resize(2).Merge[/COLOR]
[COLOR=#0000ff]    MAT_02.Cells(Row2, 3).Value = Now[/COLOR]
    
    MAT_02.Cells(Row2, 4).Value = "=SUM(MAT_02_EXP!K:K)"
    MAT_02.Cells(Row2 + 1, 4).Value = "=SUM(MAT_02_EXP!K:K)"
    MAT_02.Cells(Row1, 5).Value = "ME5009AAAA0"
    MAT_02.Cells(Row1 + 1, 5).Value = "ME5008AAAA2"
    MAT_02.Cells(Row2, 6).Value = 0.5
    MAT_02.Cells(Row2 + 1, 6).Value = 0.5
    
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,760
Messages
6,180,816
Members
452,996
Latest member
nelsonsix66

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