Friday, September 28, 2012

Get Folder Path From User Input


This is some very useful code from Stack Overflow which allows the user to select a folder in VBA and reads the folder path into a variable. I'm currently making a macro which opens several XML files in a folder, and this will help users to input the folder where those XML files are located:

Sub SelectFolder()
    Dim diaFolder As FileDialog

    ' Open the file dialog
    Set diaFolder = Application.FileDialog(msoFileDialogFolderPicker)
    diaFolder.AllowMultiSelect = False
    diaFolder.Show

    MsgBox diaFolder.SelectedItems(1)

    Set diaFolder = Nothing
End Sub

Monday, September 17, 2012

VBA - Delete Worksheet With A Specific Name If It Exists

Application.DisplayAlerts = False 
On Error Resume Next 
ThisWorkbook.Sheets("Sheet1").Delete 
On Error Goto 0 
Application.DisplayAlerts = True 

  • The code between "On Error Resume Next" and "On Error Goto 0" will be skipped if an error is generated. 
  • It also won't display the error to the user because of the code on line 1 (Application.DisplayAlerts = False).
  • "ThisWorkbook" will need to be declared.


Friday, September 14, 2012

Error occurred during initialization of VM java/lang/NoClassDefFoundError: java/lang/Object


If you get this error when you just type "java" into the command prompt, then it can be solved by typing "PATH=C:\Program Files\Java\jdk1.7.0_06\bin" (the folder of your java.exe).