Mismatch error in VBA

willow1985

Well-known Member
Joined
Jul 24, 2019
Messages
929
Office Version
  1. 365
Platform
  1. Windows
Hello,

I have the below code and I am still very new to vba, but I cannot see where I went wrong...I get a mismatch error on the first line. What I am trying to write is if there are any blank cells between A2 and J2 that it will prompt the msg box and end the macro and not proceed to updating the Batch Log spreadsheet. I hope someone can shed some light on the error.

Thank you

Code:

If ThisWorkbook.Sheets("New BL Data").Range("A2:J2").Value = "" Then
MsgBox "Please Complete all Fields"
Exit Sub
End If
Range("A2:I2").Select
Selection.Copy
Sheets("Batch Log").Select
Range("BLTable").Cells(1, 1).End(xlDown).Offset(1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("New BL Data").Select
Range("A1").Select


'
End Sub
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
You cannot check multiple cells for a value like.
Are the blank cells genuinely blank/empty or do they contain a formula that returns ""
 
Upvote 0
2 Cells did contain a formula (A2 and J2), so I changed it to apply to B2 to I2 (non formula cells). I am testing it and filled out some cells and left other blank in that range. I want it to not allow the macro to run if any cells between B2 and I2 are blank.
I am also having trouble with the macro unprotecting Sheet: Batch Log and protecting it again after the update. See new code:

If ThisWorkbook.Sheets("New BL Data").Range("B2:I2").Value = "" Then
MsgBox "Please Complete all Fields"
Exit Sub
End If
Range("A2:I2").Select
Selection.Copy
Sheets("Batch Log").Select
Columns("A:I").Select
Range("BLTable[[#Headers],[TYPE]]").Activate
Selection.Locked = False
Selection.FormulaHidden = False
Range("BLTable").Cells(1, 1).End(xlDown).Offset(1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Columns("A:I").Select
Range("BLTable[[#Headers],[TYPE]]").Activate
Selection.Locked = True
Selection.FormulaHidden = False
Range("BLTable").Cells(1, 1).End(xlDown).Offset(1).Select
Sheets("New BL Data").Select
Range("A1").Select


'
End Sub
 
Last edited:
Upvote 0
Try
Code:
With ThisWorkbook.Sheets("New BL Data")
   If Application.CountIf(.Range("B2:I2"), "") > 0 Then
      MsgBox "Please Complete all Fields"
      Exit Sub
   End If
End With
 
Upvote 0
That worked. Now my only problem is the error I get at the Selection.Locked = False portion of the code.
Error: 1004, unable to set the locked property of the range class

PS the locked sheet is not password protected.
 
Upvote 0
Try unprotecting the sheet first.
If that doesn't work, you will need to start a new thread.
 
Upvote 0
Isnt that what the below is supposed to do, unprotect the sheet?

Columns("A:I").Select
Range("BLTable[[#Headers],[TYPE]]").Activate
Selection.Locked = False

Selection.FormulaHidden = False
 
Upvote 0
Nope, there is nothing in there to unprotect the sheet.
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,161
Members
453,021
Latest member
Justyna P

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