Unable to execute kill function VBA - Run-time error '13' - excel

I'm unable to perform the Kill function using Visual Basic in Excel. When I run the code to Kill, I get
"Run-time error '13' Type mismatch'
Does anybody have any ideas? I've tried
Kill("pathname")
and
Kill "pathname"
I've tried different filetypes in various locations and always end up getting the same error.
Edit- I've now reverted to attempting the simplest of Macros and I still get the error instantly:
Sub KillFile()
Dim filetokill As String
filetokill = "C:\Users\thomas.bennett\Desktop\test.txt"
VBA.Kill filetokill
End Sub

Both of these should work, to delete a file from for instance, your desktop.
Sub killfile1()
Dim filetokill As String
filetokill = "C:\Users\yourusername\Desktop\test.txt"
Kill filetokill
End Sub
Sub killfile2()
Kill "C:\Users\yourusername\Desktop\test.txt"
End Sub
I've had an idea though.. you haven't created a FUNCTION or SUB called Kill have you? I just created one and it didn't disallow it:
Function kill(filename As Integer)
End Function
If this is present, I get the exact same error (13) as you when running either of my example macros. Check your code for other uses of Kill.

Related

Automatically run VBA macro without a message box

I have an Excel macro that I would like to run automatically when the file is opened. The only way I have gotten this to work is by adding a msgbox before calling to my subroutines. However, this requires me to click OK or close the box for the macros to run. I have tried using a timed msgbox sub, but this also does not work.
For some reason, the msgbox pops up before Excel is fully opened, at which point the macro gets stuck here (code for this is below). From here, I tried waiting for the file itself to be opened until it is in write-mode (Workbook.ReadOnly = false). This also did not work.
Public Sub msgBoxTimer()
Const timeout = 2
Dim objShell As Object
Set objShell = CreateObject("WScript.Shell")
objShell.Popup "Measurement will begin shortly", timeout
End Sub
Private Sub Workbook_Open()
Call msgBoxTimer
Call init ' initiate device
Call updateIndex ' collect & record measurements
End Sub
I get from your comment that you are probably running other shell commands in init and updateIndex.
What needs to be clear is that when you execute a Shell command via a Shell object in VBA, the default behavior is not to wait for the shell command to complete before running the next line of codes.
If you want Excel to wait for the Shell command to be completed before continuing, you can have a look at the answers to this question.
That being say, if you want Excel to be fully open before running any shell commands, you can use a MsgBox like you originally intended, but it has to be VBA's MsgBox that you would simply call like this:
MsgBox "Measurement will begin shortly"
The VBA thread will wait for the "OK" button to be pressed before continuing the execution.

Excel Macro Expected Function or variable

I am following this script
https://www.extendoffice.com/documents/excel/3921-excel-save-and-close-workbook-after-inactivity.html
it looks simple and straight forward.
but when I open the excel file I get these errors
---------------------------
Microsoft Visual Basic for Applications
---------------------------
Compile error:
Expected Function or variable
---------------------------
OK Help
---------------------------
I am not sure if I am doing anything wrong
but everything looks alright to me
any idea how to fix that?
Thanks K Daves
Do you know why I get error on End Sub and On Error Resume Next?
There was an HTML coding error on their website.
In the sub TimeSetting(), it should look like this:
Sub TimeSetting()
CloseTime = Now + TimeValue("00:00:15")
On Error Resume Next
Application.OnTime EarliestTime:=CloseTime, _
Procedure:="SavedAndClose", Schedule:=True
End Sub
The <span style="background-color:#ff0"> portion that was in your code was actually meant to style the the Sub on their website - not meant to be part of the code.

Different computers handling "On Error Resume Next" differently?

I have the following code:
Sub test()
On Error GoTo Label
Debug.Print 1 / 0
Exit Sub
Label:
MsgBox "Infinity!"
End Sub
If I run this code on my laptop, I get "Infinity", as I expected. However... When I run it on my desktop, excel throws an error message Runtime error 11 ... Division by zero
Has anyone ever experienced this before? It's almost like my Excel desktop client has just 'forgotton' how to handle errors...
Most likely this is due to different settings in your VB editor under
Tools>>Options>>General>>Error Trapping
If set to "Break on all errors" then that's what it does...

Excel Timer Function Throwing "Expected Function or variable" Error

I have an Excel macro I've called for years I need a pause in running code. Here's the macro:
Sub timeFrame(PauseTime As Double)
'
Dim Start As Double
Start = timer
Do
DoEvents
Loop Until (timer - Start) >= PauseTime
End Sub
I just ran a macro that calls the one above and got an "Expected Function or variable" compile error with the Start = time line highlighted. In troubleshooting, I established that the error occurs with any macro that contains the same line. I even went to MSDN and copied their sample code, and got the same error when I tried to run it.
I thought I might have a missing library reference but nothing showed up there. After an hour or so of Googling I'm at a complete loss for a reason for the error, and would greatly appreciate some fresh insight.
This can happen if you have a Function or Sub that shares the same name as the Module. Every name must be unique. Even the Modules.

excel vba error handling

I'm having trouble with an error handling in VBA excel.
Basicaly, I have a situation where I'm dealing with multiple errors within same error handling block.
To make things really simple let's just say:
Sub some_function ()
On Error go to step1
step2:
some code which triggers an error
Exit Sub
step1:
Okay, so far so good.
Problem is, that in this block of code can also occur an error
with the same Err.Number but I have to deal with him on other way
which is not specified in this block of code.
go to step 2
End sub
I'm using SAP session to connect to SAPgui and I cannot predict which error will occur. If I can catch an error within error handling block of code I can solve this situation.
So basicaly I'm blind foilded. If an error occurs I try to do some other things and if it works OK, but if an error occurs second time (within error handling block) it will throw me an error.
Is there any walkaround?
UPDATE:
just thinking out loud.
If I use On Error Resume next statement and do as following:
On Error Resume next
some code,
line of code that could trigger an error
if Err.Number <> 0 Then
try to handle an error
Err.Clear
End if
line of code that could also trigger an error
If Err.Number <> 0 Then
Try to handle an error
Err.Clear
End If
Would that be OK? or is there any better solution?
Is it possible to use Resume next statement within the procedure in only a certain block of code? Let's just say we have a procedure with 20 lines, and I would like to use Resume Next statement between 10th and 15th line.. Is it possible to enable and disable Resume next statement, or on Error line?
How about putting the code in step 2 into a separate procedure? There, you can do separate error handling with a new on error goto statement.

Resources