The code below is intended to check if the cells in a given range are empty. If they are not, the user is asked if they want to save the contents of the range. If they select "No" the contents of the range is cleared.
Dim answer As Integer
If Not WorksheetFunction.CountA(Worksheets("Temp").Range("A1:D101")) = 0 Then
answer = MsgBox("Save changes for this account?", vbYesNo)
If answer = vbYes Then
'do nothing
Else
Worksheets("Temp").Range("A1:D101").ClearContents
End If
End If
For some reason the clear method just won't clear anything. I have tried first activating the sheet. First selecting the range. First selecting the sheet then selecting the range. Using ".Clear" instead of ".ClearContents". Adding an Else statement to the outer If statement. I've tried selecting a range of only filled cells. No matter what I've done, the contents of the range will stay there until I delete it manually. Everything else seems to work fine. I cannot for the life of me figure out what is missing from my code.
Any suggestions?
Edit: Here is the code for the entire function:
Private Sub NameBox_Change()
Dim answer As Integer
If Not WorksheetFunction.CountA(Worksheets("Temp").Range("A1:D101")) = 0 Then
answer = MsgBox("Save changes for this account?", vbYesNo)
If answer = vbYes Then
'do nothing
Else
Worksheets("Temp").Range("A1:D101").ClearContents
End If
End If
With Me
Worksheets("Temp").Cells(1, 1).Value = Format(Date, "mm-dd-yyyy")
Worksheets("Temp").Cells(1, 2).Value = Worksheets("Users").Cells(.NameBox.ListIndex + 1, 1).Value
Worksheets("Temp").Cells(1, 3).Value = Worksheets("Users").Cells(.NameBox.ListIndex + 1, 6).Value
Worksheets("Temp").Cells(1, 4).Value = Worksheets("Users").Cells(.NameBox.ListIndex + 1, 7).Value
ReadLabel.Caption = Worksheets("Temp").Cells(102, 3).Value
SpentLabel.Caption = Worksheets("Temp").Cells(102, 4).Value
ToSpendLabel.Caption = Worksheets("Temp").Cells(102, 3).Value - Worksheets("Temp").Cells(102, 4).Value
End With
End Sub
I am an idiot. This function is triggered when a the user selects a new name from a dropdown list. The original name was being cleared and then replaced by the new name. I just hadn't realized that the names had changed. Thanks for helping my look at this with fresh eyes. I will leave the question up in case someone finds my code useful.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.