vlookup on excel vba throws error 400 - vba

I am trying to retrieve "name" in the sheet "Properties" but I keep getting the error 400 whenever I try to run it.
I am able to retrieve each individual content by a MsgBox, but I just don't know why this Vlookup statement throws me an error 400.
temp = Application.WorksheetFunction.VLookup(name,
currentWb.Worksheets("Properties").Range("B2:C11"), 2, False)
This is how I set name:
name = Wb.Worksheets("Sample").Cells(rrow, 9).Value
where "rrow" is my for loop.
And this is how i declare my temp:Dim temp As String
Anybody has any idea why ? Is it something to do with the syntax ?

Related

Is there a way to convert my old data macros from table to vba after a database split?

I recently split my ms access database into a front, and back end. I didn't know that the data macros that were on the table wouldn't be able to work anymore. I was wondering if there is any way to convert these to vba.
if IsNull([prmQuoteID])
RaiseError
Error Number 10
Error Description Please specify quote details.
end if
RunDataMacro
Macro Name Quotes.GetNextQuoteNumber
SetLocalVar
Name varNextQuoteNumber
Expression = [ReturnVars]![retNextQuoteNumber]
RunDataMacro
Macro Name Quotes.UpdateQuoteTotal
parameters
prmQuoteId = [prmQuoteId]
if [varNextQuoteNumber]=0
RaiseError
Error Number 20
Error Description Unable to retrieve next quote number!
stopmacro
end if
Look Up A Record In SELECT Quotes.ID, Quotes.StatusID, Quotes.QuoteNumber, Quotes.Submitted FROM Quotes;
Where Condition = [ID]=[prmQuoteID]
if [StatusID]>=10
RaiseError
Error 30
Error Description Quote has already been marked submitted.
End If
Edit Record
SetField
Name StatusId
Value = 10
SetField
Name QuoteNumber
Value = [varNextQuoteNumber]
SetField
Name Submitted
Value = Date()
Example of Data Macro
Data Macro Photo of above code
It connects to an form on click embedded macro in the front end database
On click embedded macro
OnError
go to Macro Name
ErrorHandler
SetTempVar
Name YesNoMessage
Expression = "Quote cannot be changed once it is submitted."
SetTempVar
Name YesNoMessage2
Expression = ""Do you want to continue?""
OpenForm
Form Name YesNoAlert
View Form
Window Mode Dialog
If [TempVars]![YesNoResponse]=False
stopmacro
End If
RunMenuCommand
command saveRecord
RunDataMacro
Macro Name Quotes.Submit
Parameters
prmQuoteId = [txtID]
RefreshRecord
RunMacro
Macro Name SetFromState
SetTempVar
Name AlertMessage
Expression "Quote #" & [txtQuoteNumber] & " is now submitted for approval."
OpenForm
Form Name Okalert
View Form
Window Mode Dialog
Submacro: ErrorHandler
RunMacro
Macro Name Utilities.ErrorAlert
End Submacro
I was wondering what the best way to convert these would be and still have them work for someone with limited VBA knowledge.

Excel 2013 VBA Runtime Error 5 with listobjects.add

I have a main sub that I am using to set up my worksheets and data for analysis in other subroutines. As part of this I am turning some imported data into a table(data was imported from a CSV file) and placed into "IncidentsData" worksheet. The code for the entire module compiles. However, when I step through the module I get "Run-Time Error '5': Invalid procedure call or argument" on the line where I format the imported data into a table at:
Worksheets("IncidentsData").ListObjects.Add(xlSrcRange, ISDRange, , xlYes).Name = "IncidentsDataTable"
Here is the overall subroutine:
Public Sub Categorise_Incidents()
Dim incidentsData1 As ListObject
Set Commodity = Worksheets("CommoditySheet").ListObjects("Table1")
Set QEListByGenus = Worksheets("QEListByGenus")
Set Results = Worksheets("Results")
Set IncidentsDataSheet = Worksheets("IncidentsData")
ISDRange = IncidentsDataSheet.UsedRange
Worksheets("IncidentsData").ListObjects.Add(xlSrcRange, ISDRange, , xlYes).Name = "IncidentsDataTable"
Set incidentsData1 = IncidentsDataSheet.ListObjects("IncidentsDataTable")
initialiseDictionaries
MainSearchLoop QEListByGenus, incidentsData1, dictQENSGenus
End Sub
I have gone to a number of sites to work out how to do this action including:
1. the stack overflow pages on runtime error 5 in excel vba - most of these seem to be specific to pivot tables and didn't tell me what I was doing wrong
2. https://msdn.microsoft.com/en-us/vba/excel-vba/articles/listobjects-object-excel
3. stack overflow questions on creating a table in VBA How to create a table using vba?
4. analysistabs on creating a table in excel vba
https://analysistabs.com/excel-vba/tables-examples/
Plus a bunch of others. Unfortunately, they haven't helped me work out what I am doing wrong. Any help would be greatly appreciated.
Cheers, S26
Edited: to include .Name as that also does not work and throws a runtime error and to fix typos/errors.
First, change xlSrcERange and lYes to xlSrcRange and xlYes.
You're missing the .Name at the end of Worksheets("IncidentsData").ListObjects.Add(xlSrcRange, ISDRange, , xlYes). You haven't specified which property of the newly created table you want to be "IncidentsDataTable".
Alternately, you can do this:
Set incidentsData1 = Worksheets("IncidentsData").ListObjects.Add(xlSrcRange, ISDRange, , xlYes)
incidentsData1.Name = "IncidentsDataTable"
If you take that approach, it looks like you don't need to change the Name at all.

Deleting and creating an userform with the same name dynamically

Hi I'm actually coding to delete all the userforms of a project and recreate them by using a XML file.
The problem is that when I delete a userform and I want to recreate it with the same name, I get the following error "Run-time error '75' - Path/File error".
I use this to delete userforms :
Application.VBE.VBProjects(iMyName).VBComponents.Remove VBComponent:=.Item(x)
Where iMyName is the project name and x the variable in my loop.
And just after I use:
Set ihm_f = .Add(vbext_ct_MSForm)
ihm_f.Properties("Name") = "CA"
Where ihm_f is my new userform and CA the name of the previous Userform I deleted for example.
Do someone know how to do ?
Try Saving the Workbook between Removing and re-Creating.
Works for me, but I would be interested to knows a better way.

Access VBA Return form control value from variable

I am trying to return the value of a form control using the below coding, unfortunately i get the error "Runtime error 2465, Cant find the field "Forms!Frm1!Control1 referred to in your expression".
vartest = "Forms!" & Screen.ActiveForm.Name & "!Control1"
MsgBox Controls(vartest).Value
Please can someone help so I can get the value to be returned?
Thanks.

Method 'range' of object _global failed when doing nothing

I have a big macro which basically processes some columns and spits out results based on some cross-checking with an access 2003 database. It works absolutely fine - no hitches at all.
However, I recently had to make a modification to it. It was literally changing an '8' to a '9' in one line of the code. But next time I ran it, it threw the 1004: Method 'Range' of object '_Global' failed error. Excel 2003 is a funny thing - I once scratched my head for hours over this, trying to find offending lines of code that could be causing the error, but alas to no avail. I did something that I didn't expect to trigger anything:
Starting with the original macro (100% confirmed working), if I just open the code up, and then save it so the 'last updated' metadata will update to reflect the save, though absolutely nothing has changed, it will throw that error again on opening.
It's as if it's so fragile that saving the macro as is will break it. Any ideas?
Update: here's what I changed that initially brought about the issue
iOutputCols = 9 'this was changed to 9 from 8
ReDim Preserve sOutputCols(iOutputCols)
sOutputCols(0) = "Policy No"
sOutputCols(1) = "Client"
sOutputCols(2) = "Trans"
sOutputCols(3) = "Effective Date"
sOutputCols(4) = "ClosingRef"
sOutputCols(5) = "Gross"
sOutputCols(6) = "Comm"
sOutputCols(7) = "Net Due"
sOutputCols(8) = "Risk" 'this line was added
Making the change here, while originally causing the error, doesn't seem special - I did small changes like the above elsewhere in the code and in other modules, one time I even did something as testval = "test" and even that redundant line will produce the error. The most minimalistic way to cause it? Simply open it up, save it without changing anything, and on next use the error occurs.
The error occurs at this line, in a completely different code section which is part of a form:
If strErr <> "" Then MsgBox strErr, vbInformation + vbOKOnly, "Action Error"
Application.ScreenUpdating = True 'error occurs here, message box which shows me the error right above
End Sub
Update 2
Removing the error handling throws the error on this line
Case "> Send Next Period Reminder" 'error on line below
Call ReplaceText(wordApp, "[office_address]", Range("Address_" & Worksheets("UserParms").Range("F6").Value).Value) 'error this line
Call ReplaceText(wordApp, "[office_address2]", Range("Address2_" & Worksheets("UserParms").Range("F6").Value).Value)
'more of the same replacetexts below
For context, this is when an option is selected for "Send Next Period Reminder", which pulls a word .dot template from a static folder and populates it based on the data selected within the sheet (Hence the replace texts). This is in a different module and has never been touched before.
Try properly qualifying your Range method calls. You have lines like this:
Call ReplaceText(wordApp, "[office_address]", Range("Address_" & Worksheets("UserParms").Range("F6").Value).Value) 'error this line
Call ReplaceText(wordApp, "[office_address2]", Range("Address2_" & Worksheets("UserParms").Range("F6").Value).Value)
While it may not be obvious, there are cases, both environmental and code-based, where these unqualified uses of Range could fail. Change the references like Range("Address... to something like yourTargetWS.Range("Address...