pushing delete vs. backspace

moserz

New Member
Joined
Jul 12, 2006
Messages
20
I have written some code in excel for a worksheet where students enter their answers and then the cell next to it tells them if the answer is correct, incorrect, or if it will be graded later (meaning there's not really a right or wrong answer). Now in 7 of the answer cells, I have the "will be graded later" answer. However, when I try to erase the contents of the cell, I run into a problem. On 4 of them, it works just fine whether I push delete of backspace. But on the other 3 (cells D17, D22, and D25, when I push delete, I get an error (Method 'Range' of object '_Worksheet' failed). If I use backspace, it works just fine. Any ideas why?

Code:
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
  Dim VRange As Range
  Set VRange = Range("InputRange")
  If Intersect(Target, VRange) Is Nothing Then Exit Sub
      Select Case Target.Address
        Case "$D$5"
          answerCell = "E5"
          valuesAreEqual = CompareValues(Target.Value, "electron gun", False)
        Case "$D$7"
          answerCell = "E7"
          valuesAreEqual = CompareValues(Target.Value, "negative", False)
        Case "$D$9"
          answerCell = "E9"
          valuesAreEqual = CompareValues(Target.Value, "phosphor", False)
        Case "$D$11"
          answerCell = "E11"
          valuesAreEqual = CompareValues(Target.Value, "", True)
        Case "$D$17"
          answerCell = "E17"
          valuesAreEqual = CompareValues(Target.Value, "", True)
        Case "$D$22"
          answerCell = "E22"
          valuesAreEqual = CompareValues(Target.Value, "", True)
        Case "$D$25"
          answerCell = "E25"
          valuesAreEqual = CompareValues(Target.Value, "", True)
        Case "$D$30"
          answerCell = "E30"
          valuesAreEqual = CompareValues(Target.Value, 13, False)
        Case "$D$34"
          answerCell = "E34"
          valuesAreEqual = CompareValues(Target.Value, 44, False)
        Case "$B$38"
          answerCell = "B39"
          valuesAreEqual = CompareValues(Target.Value, "", True)
        Case "$C$38"
          answerCell = "C39"
          valuesAreEqual = CompareValues(Target.Value, "", True)
        Case "$D$38"
          answerCell = "D39"
          valuesAreEqual = CompareValues(Target.Value, "", True)
        Case "$D$45"
          answerCell = "E45"
          If Not IsNumeric(Evaluate("=" & Target)) Or Target = "" Then
            valuesAreEqual = -1
          Else
            Target.Value = Evaluate(Range("D45").Value)
            valuesAreEqual = CompareValues(Target.Value, 1.706 * 10 ^ 11, False)
          End If
        Case "$D$52"
          answerCell = "E52"
          valuesAreEqual = CompareValues(Target.Value, 3.07, False)
      End Select

    If (valuesAreEqual = 0) Then
      Range(answerCell).Value = "Will be graded later."
      Target.Interior.ColorIndex = xlNone
    ElseIf (valuesAreEqual = 1) Then
      Range(answerCell).Value = "Correct!"
      Target.Interior.ColorIndex = xlNone
    Else
      Range(answerCell).Value = "Try Again."
      Target.Interior.ColorIndex = 36
      Target.Activate
    End If
End Sub

Function CompareValues(UserValue, CorrectValue, WillBeGradedLater)
  UserValue = LCase(UserValue)
  If WillBeGradedLater Then
    CompareValues = 0
  ElseIf (InStr(UserValue, CorrectValue) <> 0) Then
    CompareValues = 1
  Else
    CompareValues = -1
  End If
End Function
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.

Damon Ostrander

MrExcel MVP
Joined
Feb 17, 2002
Messages
4,239
Hi moserz,

I can't duplicate the problem. It seems to work fine regardless of whether I use delete or backspace.

I suggest you let us know which line of code it is failing on, and what version of Excel you are using.

Damon
 

moserz

New Member
Joined
Jul 12, 2006
Messages
20
I'm using Excel 2003 and I am having the issue on cells D17, D22, and D25. The code for those cells is in lines 18-26 of my code.

It looks like on the ones i have problems with, the cell D17 is actually a merged cell of D17 and D18. Same with D22 and D25. But the ones that the delete does work for, they aren't merged cells. Could this be the reason?
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
59,079
Office Version
  1. 365
Platform
  1. Windows
It looks like on the ones i have problems with, the cell D17 is actually a merged cell of D17 and D18. Same with D22 and D25. But the ones that the delete does work for, they aren't merged cells. Could this be the reason?
Quite possibly. Merged cells are known to create problems with things like VBA, and many programmers (like myself) avoid them at all costs because of this.

I would recommend removing the merged cells and see if that fixes your problem. You can get the same visual effect as Merged Cells by using the Center Across Selection format option (and that doesn't give VBA problems).
 

Forum statistics

Threads
1,141,665
Messages
5,707,696
Members
421,524
Latest member
Bharath99

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
Top