Check if cells are empty before pasting values

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
5,832
Office Version
  1. 2007
Platform
  1. Windows
Morning,
Im having an issue with the code below & now confused myself.

Previously i had just copied / pasted the values.
I have now see nothing is in place should i paste over value already in the cells as shown.
I have added the If IsEmpty code to check if cells have values & if Yes then just show msgbox but if empty then allow the paste of values to contine.
This is where ive now confused myself.

Rich (BB code):
Private Sub MayButton_Click()
If IsEmpty(Range"(E4:E13").Value)= True Then
MsgBox "CELLS HAVE VALUES OVERWRITE ?", VbYesNo"
If answer = vbYes Then
  MsgBox "Yes"
Else
  MsgBox "Calls Have Values"
Exit Sub
End If

Range("I9:I18").Copy Destination:=Range("E4:E13")
MsgBox "ALL FIGURES HAVE BEEN TRANSFERED", vbInformation, "MONTHS FIGUES MESSAGE"
Unload SUMMARYSHEETYEAR
Range("I9:I18").ClearContents
Range("I9").Select
End Sub
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Hi there

Do get an error with the code above?
 
Upvote 0
Ok... Not at PC at the moment but try the below...

VBA Code:
Private Sub MayButton_Click()
    If IsEmpty(Range"(E4:E13").Value = TRUE Then
        MsgBox "CELLS HAVE VALUES OVERWRITE ?", VbYesNo
        If answer = vbYes Then
            MsgBox "Yes"
        Else
            MsgBox "Calls Have Values"
            Exit Sub
        End If
        Range("I9:I18").Copy Destination:=Range("E4:E13")
        MsgBox "ALL FIGURES HAVE BEEN TRANSFERED", vbInformation, "MONTHS FIGUES MESSAGE"
        Unload SUMMARYSHEETYEAR
        Range("I9:I18").ClearContents
        Range("I9").Select
    End Sub

The only possible problem I could see is the extra ")" at...

VBA Code:
.Value)= True Then

and extra " at

VBA Code:
VbYesNo"

" in wrong place

VBA Code:
If IsEmpty(Range"(E4:E13")

However still untested.
 
Upvote 0
I see all sorts of issues and typos with this section here:
VBA Code:
If IsEmpty(Range"(E4:E13").Value)= True Then
MsgBox "CELLS HAVE VALUES OVERWRITE ?", VbYesNo"
If answer = vbYes Then
  MsgBox "Yes"
Else
  MsgBox "Calls Have Values"
Exit Sub
End If
Try replacing that block with this:
VBA Code:
If WorksheetFunction.CountA(Range("E4:E13")) > 0 Then
    answer = MsgBox("CELLS HAVE VALUES OVERWRITE?", vbYesNo)
    If answer = vbYes Then
        MsgBox "Yes"
    Else
        MsgBox "Cells Have Values"
        Exit Sub
    End If
End If
 
Upvote 0
I think this is now what i need but syntax error on line shown in Red

Rich (BB code):
Private Sub MayButton_Click()
If WorksheetFunction.CountA(Range("E4:E13")) > 0 Then
    answer = MsgBox("CELLS HAVE VALUES OVERWRITE THEM ?, vbYesNo)vbCritical"
    
    If answer = vbNo Then
    Exit Sub
    Else
    
    Range("I9:I18").Copy Destination:=Range("E4:E13")
    MsgBox "ALL FIGURES HAVE BEEN TRANSFERED", vbInformation, "MONTHS FIGUES MESSAGE"
    Unload SUMMARYSHEETYEAR
    Range("I9:I18").ClearContents
    Range("I9").Select
    End If
End If

End Sub
 
Upvote 0
You have your double-quote in the wrong place (should be after the question mark), and you cannot put "vbCritical" after the parentheses.
Did you try my solution? I already corrected all the issues your code had to do what you want?
 
Upvote 0
I did & it sorted it out.
What i had supplied in my post was to get me going.
This is what i would like to do but still syntax error in same line.

Run the code.
Check if values are in the range shown.
If yes show msgbox with yes / no
Selecting No exits sub so i can then look at cells in question.
Selecting Yes overwrites the values.
Msgbox shown to confirm All figures transfered>

I think once syntax is done all will be ok

Rich (BB code):
Private Sub MayButton_Click()
If WorksheetFunction.CountA(Range("E4:E13")) > 0 Then
    answer = MsgBox("CELLS HAVE VALUES OVERWRITE THEM ?", vbCritical + vbYesNo"
    If answer = vbNo Then
    Exit Sub
    Else
    
    Range("I9:I18").Copy Destination:=Range("E4:E13")
    MsgBox "ALL FIGURES HAVE BEEN TRANSFERED", vbInformation, "MONTHS FIGUES MESSAGE"
    Unload SUMMARYSHEETYEAR
    Range("I9:I18").ClearContents
    Range("I9").Select
    End If
End If

End Sub
 
Upvote 0
Think ive sorted it

Rich (BB code):
Private Sub MayButton_Click()
If WorksheetFunction.CountA(Range("E4:E13")) > 0 Then
    answer = MsgBox("CELLS CONTAIN VALUES, DO WE OVERWRITE THEM ?", vbCritical + vbYesNo)
    If answer = vbNo Then
    Exit Sub
    Else
    Range("I9:I18").Copy Destination:=Range("E4:E13")
    MsgBox "ALL FIGURES HAVE BEEN TRANSFERED", vbInformation, "MONTHS FIGUES MESSAGE"
    Unload SUMMARYSHEETYEAR
    Range("I9:I18").ClearContents
    Range("I9").Select
    End If
End If

End Sub
 
Upvote 0
The code below is what i have at present.
It checks if values are in the cell range and does what should be done when selecting the Yes / No on the Msgbox.

What ive overlooked now is what if the cell range is empty.
Currently with the cell range is empty so i dont get to see the Msgbox & obviously no values are pasted.
I need to be able to paste values if cell range is empty THEN if cells have values use code supplied below


Rich (BB code):
Private Sub MayButton_Click()
If WorksheetFunction.CountA(Range("E4:E13")) > 0 Then
    answer = MsgBox("CELLS CONTAIN VALUES ALREADY, OVERWRITE THEM ?", vbCritical + vbYesNo)
    If answer = vbNo Then
    Exit Sub
    Else
    
    Range("I9:I18").Copy Destination:=Range("E4:E13")
    MsgBox "ALL FIGURES HAVE BEEN TRANSFERED", vbInformation, "MONTHS FIGUES MESSAGE"
    Unload SUMMARYSHEETYEAR
    Range("I9:I18").ClearContents
    Range("I9").Select
    End If
End If

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,185
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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