VB code to add a sheet to a workbook in Excel - vb.net

I use oleautomation in dBase to add a sheet to a workbook in Excel. The MS docs show the following syntax for VB:
expression.Add (Before, After, Count, Type)
taken from: https://learn.microsoft.com/en-us/office/vba/api/excel.sheets.add
I want to add the sheet after the last sheet, which means that I am not submitting the first parameter. Typically, I then substitute the "missing" parameter with a null. This does however generate an error:
error: Ole Dispatch Exception: Unable to get the Add property of the Sheets class
Could someone post the VB syntax to add a sheet after "Sheet1"?
Thanks,
Gaetano.

Related

Reference external file with ^ in name

I am trying to pull data from external workbooks into a main workbook. I have done this before several times and it always works with the below formula.
='FolderPath[FileName]WorksheetName'!CellReference
The above formula still works for me now on many workbooks. However I have thousands of workbooks with a ^ in the name. Example Workbook1^01012017.
When I get to these types of workbooks I get the below error:
"The syntax of this name isn't correct." and the text in the cell gets highlighted until the first ^.
If I type in = in my original workbook
Open the workbook I want to reference
Select the cell I need
Hit Enter
The link works. If I copy that text exactly and change the reference from cell A1 to A2 in another cell, I get the same error. The problem seems to be that it's reading the ^ as a symbol instead of a text. Is there any other way to get this to work? I have tried using a indirect function, but those don't work when the external workbook is closed.
Any suggestions?

Error 1004 when using =AND(ISERROR) formula in macro

I'm recording a macro to automate some Excel reports and have encountered the following bug whenever I try and run an iserror(search) formula:
Run-time error '1004': Application-defined or object-defined error
I have two lists. The formula iterates through the first list and compares the values with those of the second list, hiding any matching values.
The formula in Excel is like this only with a wider criteria range:
=AND(ISERROR(SEARCH($B$3212,B2)),ISERROR(SEARCH($B$3213,B2)))
It works perfectly when I insert the formula directly into the spreadsheet cell however I get an error when I record and later run the macro using the same formula.
EDIT 2
I got the formula insertion to work through the macro but now I cannot filter the data as before, even when I do it manually without the macro.
Below is a link to a picture giving an example of the type of lookup I'm trying to achieve, previously it worked perfectly and removed all the rows which contained a string from the 'to remove list' now I cannot get it to filter at all. I've tried removing the macro after saving in notepad in case the file had become corrupted but it still does not filter as before. What could be causing this?
This is how the lookup works
Cell [A13] would contain the aforementioned ISERROR formula in this example.
This formula doesn't translate well to VBA in its current form. You should use the VBA Instr function instead of the worksheet function Search.
Function FindSubstring() As Boolean
Dim rngFindText As Range
Dim rngWithinText As Range
Set rngFindText = Sheet1.Range("B3212")
Set rngWithinText = Sheet1.Range("B2")
FindSubstring = InStr(rngWithinText, rngFindText)
End Function
Sub foobar()
Debug.Print FindSubstring
End Sub
You are asking Excel a question to tell you to find the contents of $B$3212 in B2 and to find if again.
Usually the SEARCH is used to find the contents of one thing in another, by using it again the AND statement you are asking it again ... and for what?
Hence the question does not make sense.
What I think you might be asking if just once and if there is an error meaning it did not find it there in this instance for it to return 0.
=IF(ISERROR(SEARCH($B$3212,B2)),0,SEARCH($B$3212,B2))
I figured this one out, the original 1004 error was caused by vba only partially recording the formula, the solution involved simply going into the debugger to find which line hadn't been translated correctly and editing that line. I then had to edit the formula so as to be able to filter out values acording to my criteria and ended up with a formula closer to this:
=AND(ISERROR(SEARCH("Value1",B2)), ISERROR(SEARCH("Value2",B2)))

Excel VBA - Pull information into user form to update

I am trying to create a userform that allows the users to update issues stored in a specific sheet (called Issues List). I have built a dropdown list using data validation that allows the user to select the unique issue name from a list. I have created a button next to that dropdown which opens up the userform and correctly imports the issue name identified from the dropdown.
What I need to figure out is, when the user form is initiated how do I have it search column B in my Issues List sheet and identify which row contains the issue selected by the user, and populate the fields of the user form with the information found in rows C-X of the Issues List sheet.
What I have been trying to use is an index match function, but have been unsuccessful in getting the code to work. An example of what I have been using is:
Resolved.Value = Application.WorksheetFunction.index
('Issue List'!$X$2:$X$1000,Application.WorksheetFunction.match
('Priority Table'!I35,'Issue List'!$B$2:$B$1000,0))
Any help would be greatly appreciated.
Thanks in advance!
When you use Worksheet Functions in VBA, you still have to pass in the ranges using VBA language:
So instead of:
'Issue List'!$X$2:$X$1000
you would use:
Worksheets("Issue List").Range("X2:X1000")
And instead of:
'Priority Table'!I35
Just use:
Worksheets("Priority Table").Range("I35")
Note that you can also refer to ranges by names, which can make coding easier and also far safer. When you insert rows in spreadsheets, Excel doesn't automatically update ranges in any VBA code. A reference to I35 will always to be I35.
Instead, define a name for cell I35 in Excel as normal, then refer to it in the code.
For example, if you name I35 as "Issue"
You can refer to the cell by:
Range("Issue")
(If it is a global variable, which it is be default as long as it's a unique name in the workbook, you don't need to use the Sheets("Priority Table") qualifier.
Refer to this documentation for more info on how to refer to ranges in Excel from VBA:
https://msdn.microsoft.com/en-us/library/office/gg192736(v=office.14).aspx

Run-time error '1004' ShowDataForm method of Worksheet class failed

ActiveSheet.ShowDataForm
Returns Error 1004
I tried:
Range("B4:D4").Select
ActiveSheet.ShowDataForm
It didn't help.
When you use the ShowDataForm method, Microsoft Excel looks for the data list in two places:
The defined name "Database." If a range has been defined as the database, Microsoft Excel will display the data form, and the data in that range will be accessible.
Cells A1:B2 on the specified worksheet. Microsoft Excel will attempt to find a list whose upper-left corner lies in the range A1:B2. If a list is found, Microsoft Excel will display the data form and the data in the list will be accessible.
WORKAROUND
Either place the Table in A1:B2 Range.
Or Name the Range Database, Ex:
Range("B5").CurrentRegion.Name = "database"
ActiveSheet.ShowDataForm
worked for me
Source 1
Source 2
If you are struggeling with this solution...
It seems the "database" Text is language based. Once i changed it to "datenbank" (german Excel) it worked. Dont know if this is the case for all localisations though.
Thanks for the solution!

MS-Excel 2010... Cannot get VBA code in immediate window to work?

Basically I am debugging my code and I created a break point. At this break point I want to check the address of a name range "Sales". I have looked up ways to do this and I have tried the following lines of code each and I always get a "run-time error 1004 Application defined or object defined error". Here are the lines of code i have tried:
? Range(“Sales”).address
Debug.Print Range(“Sales”).address
the following website claims these lines of code should work what am I doing wrong?http://excelexperts.com/Quick-Immediate-Window-Commands-useful-for-debugging
can not comment yet. so:
Tried two lines of code in immediate window and got no error.
so, result is: named range "Sales" is missing in your workbook.
To find the named range enter this in immediate window:
Application.Goto Reference:="Sales"
if this "Sales" exist then range would be selected in your workbook.
Else Error '1004' will come: "the text you entered is not a valid reference or defined name."
OR,
You can find your named ranges in these areas:
Formulas tab --> Defined Names --> Name Manager
or
Home tab --> Editing --> Find & Select --> Go To