Run-Time Error 13 VBA

Yeoman.jeremy

Board Regular
Joined
Apr 4, 2011
Messages
90
Hi there.

Not too sure what i can do to fix this coding, basically when the checkbox is checked, it adds one to a particular cell on another workbook.
Code:
Sub CheckBox1_Click()
strFirstFile = "C:\Users\owner\Desktop\Test Over WBK"
strSecondFile = "C:\Users\owner\Desktop\Statistics Runpage"
If ThisWorkbook.Sheets("Sheet1").CheckBox1.Value = True Then

Set wbk = Workbooks.Open(strFirstFile)
Set wbk = ThisWorkbook
With wbk.Sheets("Sheet1")
[E4] = [E4] + 1
[F4] = [F4] - 1
End With
End If

Set wbk = strSecondFile
With wbk.Sheets("sheet1")
If ThisWorkbook.Sheets("Sheet1").CheckBox1.Value = False Then
Set wbk = strFirstFile
With wbk.Sheets("Sheet1")
[F4] = [F4] + 1
[E4] = [E4] - 1

End With
End If
End With
End Sub

When i run this it says Run-Time Error 13: Type Mismatch
 
Just Wrote in your Codeing now , feels a lot tidier :P
Only, came across this problem
"Run-time Error '91':
Object Variable or With Block variable not set"

Here's the code, and the spot i found the problem in

Code:
Sub CheckBox1_Click()
Dim wbStats As Workbook
Dim wbTest As Workbook
Dim strTestFileName As Workbook
Dim srtPath As String
 
   Set wbStats = ThisWorkbook ' create reference to the workbook the code is in
 
   strPath = "C:\Users\owner\Desktop\Test Over WBK"
   
   [COLOR="Red"]strTestFileName = "Test Over WBk.xls"[/COLOR]
 
   ' open and create a reference to the 'Test Over WBk' workbook
   Set wbTest = Workbooks.Open(strPath & strTestFileName)
 
   ' code to do stuff
   If wbStats.Sheets("Sheet1").Checkbox1.Value = True Then
   
With wbTest.Sheets("Sheet1")
.Range("E4").Value = Range("E4").Value + 1
.Range("F4").Value = Range("F4").Value - 1
End With
End If

If wbStats.Sheets("Sheet1").Checkbox1.Value = False Then
With wbTest.Sheets("Sheet1")
.Range("E4").Value = Range("E4") - 1
.Range("F4").Value = Range("F4") + 1
End With
End If
 
Upvote 0

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
That's a typo - it should be declared as a string.

That's why it's got str at the beginning and I should have remebered.

The only other problem with the code that I can see is that you appear to have mislaid a few dots again.:)
 
Upvote 0
blimmin Dots eh :biggrin:

So now it's fining a problem with my IF
Code:
[COLOR="Red"]If wbStats.Sheets("Sheet1").Checkbox1.Value = True Then[/COLOR]
   
With wbTest.Sheets("Sheet1")
.Range("E4").Value = Range("E4").Value + 1
.Range("F4").Value = Range("F4").Value - 1
End With
End If
 
Upvote 0
aw hang on, that was what came up when i was stepping through it.
I just ran the macro, and it said it couldn't find the file "C:\Users\owner\Desktop\Test Over WBKTest Over WBK.xls"
My guess is because the coding says
Code:
Dim strTestFileName As String
Dim srtPath As String
'
'
'
Set wbTest = Workbooks.Open(strPath & strTestFileName)
 
Upvote 0
Well, and this is a wild guess - call me crazy, that's probably because you've included what appears to be the file name in the path.:)
Rich (BB code):
strPath = "C:\Users\owner\Desktop\Test Over WBK"
 
Upvote 0
Hang on, i figured it out.
The problem was with the
Code:
 strPath = "C:\Users\owner\Desktop\Test Over WBK"
   
   strTestFileName = "Test Over WBk.xls"

It was placing them two together, so it was looking for
"C:\Users\owner\Desktop\Test Over WBKTest Over WBK.xls"

and also, for some reason it doesn't like it when i put .xls on the end.

So now this is all running smoothly, except for one problem which i've encountered D:

When i run it, instead of taking the values on the new page and increasing them by one etc, it takes the values from the first page, and increases THOSE by one on the new workbook
 
Upvote 0
That sounds like you still don't have the referencing right.

Might even have mislaid a dot or two.:)
 
Upvote 0
I found the Dots! and it works! YAY!

heh thanks an awful lot!

now to apply it to the real deal and hope i don't mess it all up horribly :biggrin:
 
Upvote 0

Forum statistics

Threads
1,224,507
Messages
6,179,176
Members
452,893
Latest member
denay

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