Merging specific cells(dynamic) and Suppress Excel's msgbox

sebas1102

New Member
Joined
May 14, 2006
Messages
26
Hi all,
I have an excel list that pulls out data from a master list.
from column A onwards, it pulls out the item's name("A"), Date of Production("B") and total quantity("C").
For an item with different production dates, the total quantity are the same. So, I first sort out items according to their names, and merge the total quantities together to 1 cell. ( for example"A1" to "A5" are of the same item apple, and "A6" to "A11" are all bananas) So I need to merge the cells of total quantities from C1 to C5 and B6 to B11 respectively.

2 problems cropped out: 1) I used record macro, but it cannot dynamically merge the cells if the number of items change.
[vba]
Range("C1:C5").Select
Selection.Merge
Range("C6:C11").Select
Selection.Merge
[/vba]

2) When I run the macro, a msgbox keeps popping out:
"selection contains multiple data values. merging cells will keep the left uppermost data only."

so i tried using [vba]on error resume next[/vba] , but it still does not suppress the popup.

Please help!
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
That's because it's not an error :)

What you want is to set DisplayAlerts to False to stop the prompt from appearing.

Code:
Application.DisplayAlerts = False

Range("C1:C5").Merge
Range("C6:C11").Merge

Application.DisplayAlerts = True
 
Upvote 0
sebas1102

Why do you want to merge in the first place?

All that will do is lose data.
 
Upvote 0
sebas1102

Why do you want to merge in the first place?

All that will do is lose data.

I need to merge those cells because the data is the same and I do not want to confuse the user. Is it possible?
 
Upvote 0
That's because it's not an error :)

What you want is to set DisplayAlerts to False to stop the prompt from appearing.

Code:
Application.DisplayAlerts = False

Range("C1:C5").Merge
Range("C6:C11").Merge

Application.DisplayAlerts = True

oh ok! haha thanks alot!
 
Upvote 0
Hi, Cindy Ellis,

try this
Code:
Option Explicit
Option Base 1

Sub merge_same_data()
'Erik Van Geit
'050802 0337
'merge cells with same contents in single column
'skip "singles" to make code faster

Dim rng As Range
Dim LR As Long
Dim i As Long
Dim j As Long
Dim k As Long
Dim Arr As Variant
Dim ArrItem As String
Dim ArrRowNumbers() As Variant
Dim HL As Integer 'highlight

Const col = 1
Const FR = 2

If Cells(Rows.Count, col) <> "" Then LR = Rows.Count Else LR = Cells(Rows.Count, col).End(xlUp).Row
Set rng = Range(Cells(FR, col), Cells(LR, col))

Arr = rng.Value
i = 1
j = 1
    Do
    ArrItem = Arr(i, 1)
    k = i
        On Error Resume Next 'avoids bug at the end of the loop "Arr(i, 1)" when i > UBound(arr)
        Do
        i = i + 1
        Loop While ArrItem = Arr(i, 1)
        On Error GoTo 0
            'If k <> i - 1 Then     'enable these line to skip singles
            ReDim Preserve ArrRowNumbers(j + 1)
            ArrRowNumbers(j) = k + FR - 1
            ArrRowNumbers(j + 1) = i - 1 + FR - 1
            j = j + 2
            'End If                 'enable these line to skip singles
    Loop While i < LR

  
With Application
.ScreenUpdating = False
.DisplayAlerts = False
End With

For i = 1 To j - 1 Step 2
Range(Cells(ArrRowNumbers(i), col), Cells(ArrRowNumbers(i + 1), col)).Merge
Next i
'For i = 1 To j - 1 Step 2
'HL = IIf(HL = 35, 36, 35)
'Range(Cells(ArrRowNumbers(i), col), Cells(ArrRowNumbers(i + 1), col)).Interior.ColorIndex = HL
'Next i

With Application
.ScreenUpdating = True
.DisplayAlerts = True
End With

End Sub
kind regards,
Erik
 
Upvote 0
another thought on this
using conditional format, you could "hide" the duplicates

something like
select A2:A65536
menu format / conditional format
optio "formula"
=A2=A1
fontcolor = white


merging cells has major disadvantages: a lot of experts hate them :-)
 
Upvote 0

Forum statistics

Threads
1,226,508
Messages
6,191,444
Members
453,658
Latest member
healmo

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