getting data out of merged cells

vbnewbiebastard

New Member
Joined
Mar 20, 2002
Messages
1
hey... I'm making a vb script that beeps and gives a msgbox if a user types in more than 25 characters in a string. The following code works when I try it with 1 cell, but I get a data mismatch error when I try to check the contents of a merged cell. Is there something besides excel.range.value I should be using?

Thanks in advance,

-ROb

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
Dim ThisCell As String
If Target.Column = 2 And Target.Row = 15 Then
ThisCell = Mid(Target.Value, 26, 1)
If ThisCell <> "" Then
Beep
foo = MsgBox("Please enter no more than 26 characters", vbOKOnly)
Target.Value = "enter text here"
End If
End If
End Sub
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Hi vbnewbie,

The problem is that a merged cell is actually a range containing more than one cell, and you can't access the Value property of a range containing more than one cell. I believe in your case the problem will go away if you change all your references to Target.Value to Target.Cells(1).Value, which just yields the first cell in the range (which is where the displayed cell contents reside).
 
Upvote 0

Forum statistics

Threads
1,214,583
Messages
6,120,383
Members
448,955
Latest member
BatCoder

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