Run-time error: Select method of Range class failed - excel

I am currently in Excel 2010 and created this spreadsheet and code in this version. My colleagues (using the spreadsheet) are using Excel 2007 (haven't been upgraded yet). They are getting the error message but I am not.
I had the following code set up so that based upon the selection in the ComboBox, the specified cells would populate with "N/A" or remain/become blank.
Private Sub ComboBoxSite6_Change()
If ComboBoxSite6.Value = "N/A" Then
Range("Site6Cells").Select
Selection = "N/A"
Else
If ComboBoxSite6.Value <> "N/A" Then
Range("Site6Cells").Select **This line highlights with the error when debugged
Selection = ""
End If
End If
End Sub
The change of the combobox and the population (or not) of "N/A" is all happening with the same sheet of the workbook so that shouldn't be a problem. But even so, I tried adding the following before each of the "Range..." lines: Sheets("Site Prep"). This did not work either.
Additionally, I have 5 other columns and comboboxes doing the same action, coded in the same way prior to this piece of code and there are no problems indicated.
Can anyone tell me what the problem might be? Is this a backwards compatability issue?

It looks as if the Range called "Site6Cells" doesn not exist in the right way.
Named ranges can be either global (= member of the whole workbook) or local (= member of the worksheet). If the Named range you created is a local one than you cannot call it as if it were a global one.
Check this via the ribbon for Formulas > Name Manager > Property called Scope
Compare this with that for your collegues and see if this is different.
To change it you will have to recreate it (delete the old ane, make a new one), or use a 3rd party Name manager, or change the scope via VBA.

Related

How to dynamically populate MSForms combobox on worksheet with VBA?

I have an Excel sheet for data input by a third party. To restrict erroneous data I want to restrict data to a certain list of items.
As the list is rather long, I want to use a combobox, so that when they start typing the correct names show up.
This worked in testing, but as soon as I shipped the Excel file the ActiveX Combobox started acting out. This appears to be known behaviour, without a fix besides not using the ActiveX version of the combobox.
I'm switching to make use of the Forms version of the combobox.
Besides the combobox there is a simple cell with data validation with two options: "Professioneel" and "Particulier". I use an onchange event to capture if the selection changes, and then I populate my combobox with the corresponding list.
This worked with the ActiveX version, but nor the forms version.
The code below is my latest try at populating the forms combobox.
The Else clause of the If statement still shows the code that used to work for the ActiveX version of the combobox.
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Range("C7")) Is Nothing Or Target.Cells.Count > 1 Then Exit Sub
If Range("C7").Value = "Particulier" Then
ThisWorkbook.Worksheets(4).Shapes("ComboBox1").List = ThisWorkbook.Worksheets(8).Range("B2:B58").Value 'this does not work
Else
ComboBox1.List = ThisWorkbook.Worksheets(7).Range("B2:B434").Value 'this used to work for a combobox
End If
End Sub
I get error 438.
How do I get this to work with a Forms version of the combobox, or is this behaviour not supported in the forms version?
Let's say that your drop down is named "Drop Down 1", try the following . . .
ThisWorkbook.Worksheets(4).Shapes("Drop Down 1").ControlFormat.List = ThisWorkbook.Worksheets(8).Range("B2:B58").Value
Alternatively, you can refer to your drop down using the DropDowns collection . . .
ThisWorkbook.Worksheets(4).DropDowns("Drop Down 1").List = ThisWorkbook.Worksheets(8).Range("B2:B58").Value

Textbox "Method or data member not found"

I am writing an aircraft system emulation using Excel (2013) VBA, Windows 10. I have macros which will allow students to operate the system and display it on the screen. Before the xmas break i was doing some development work and it was working fine. However I have come back to it today I am getting a "Method or data member not found" for some of the textboxes, which are still showing the correct names. I would say that in design mode I can't access the textbox properties.
This is the code that was working:
Sub Batt_MasterOFFExample()
' Indications
Sheet2.Ess1Volt.Text = "---V"
With Sheet2.Ess1Volt
.ForeColor = &HFFFFFF
End With
End Sub
Now when I run it it highlights Sheet2.Ess1Volt.Text = "---V" and gives me the error.
use option explicit to avoid such mistakes. ;-)
Sheet2 isn't declared as a worksheet (dim Sheet2 as worksheet // set sheet2= whatever ) , so I assume you meant Worksheets("Sheet2")
The same thing happened to me. After deleting the textbox and recreating it, everything worked just fine.

Cell connected by controlsource keeps losing its formula

There are two cells in my spreadsheet, one contains input value only (let's say cell_1) and another one (cell_2) has formula referencing to the former one (=cell_1). For convenience, I also create a Userform with a Textbox whose controlsource property is linked to cell_2.
If there is no UserForm, everything works just fine. But with UserForm (shows with vbModeless) and the value in cell_1 changes (no matter it is changed via manual or another VBA subs), the formula in cell_2 will be overwritten by the value of cell_1 and the value in TextBox stops updating.
Has anyone encountered this strange thing?
ps: I've created a simple example, you can download it here
http://wikisend.com/download/192680/Control_Source_Bug_Test.xlsm
[update] 2014/11/29
Although the root cause is still unknown, I found that rowsource property of ListBox works perfectly; the formula of the cell connected by rowsource won't be affected. With proper setting it will look just like TextBox and one can use it as a replacement!
i think, you should have added your reset formula almpst everywhere.
Range("C7").Formula = "=C4"
added to the spinbuttons and also into userform textboxes.
here is same example sheet which I corrected my way and I think it works. Test and tell me if I am right.

#NAME? error in Excel for VBA Function

I am making my first VBA program and trying to run the following function. The function checks a specific named range for the first row which does not have a value greater than it's leading value, but less than 1.
Public Function findPurchase()
Dim CRT As Range
Set CRT = Range("CostRateTable")
Dim existsBetter As Boolean
existsBetter = True
Dim r As Integer
r = 2
Dim c As Integer
c = 4
While existsBetter
Dim Found As Boolean
FoundBetter = False
While Not FoundBetter And c <= CRT.Columns.Count
If CRT(r, c) > CRT(r, 2) And CRT(r, c) < 1 Then
FoundBetter = True
Else
c = c + 1
End If
Wend
existsBetter = FoundBetter
If existsBetter Then
r = r + 1
End If
Wend
findPurchase = CRT(r, 3)
'MsgBox(findPurchase)
End Function
I know the function does what it is supposed to because I have both manually checked the table of values, removed the comment ' from the MsgBox, and used the debug tools to step in and out of each of the functions steps as it went through the table. However, when I reference the function in Excel with =findPurchase() I'm given a #NAME? error. The function even shows up in the function auto-complete box when I begin to type its name. When I write other functions, both with and without parameters, I can reference them just fine, for example:
Function addtwo()
addtwo = 1 + 2
End Function
What am I doing wrong with my function which causes it not to work?
You are getting that error because you have a module with the same name as the function.
Change that name to say find_Purchase and everything will be fine :) See the image below...
I had the same issue myself. It turned out that I "Saved As..." another file and macros were not enabled for that file. No banner on the top appeared, but a #NAME? error was generated.
I reopened the file, enabled macros, and the problem was resolved.
Make sure you have placed the function in a Standard Module. The error message means Excel can't find the function.
When Excel opens an unkown workbook containing VBA-Code, it usually asks for macros to be enabled by the user (depending on the application settings).
If the user then enables the macros, all event-driven procedures will be started, such as auto_open or others.
Custom VBA Functions however require for a full recalculation of the workbook. Otherwise the functions return-value still is #NAME, as the calculation is only done directly after opening the workbook.
In order to work directly at the first time opening, one has to add the following line to the workbook_open event
'
' Workbook open event
Private Sub Workbook_Open()
Application.CalculateFullRebuild
End Sub
Check "Trust access to the VBA project object model" in Macro settings from Macros security
One reason for this problem is security restrictions.. I had this problem and I activate "Enable all macros" from security center, and the problem solved
I had a similar persistent problem with one of my functions when everything else seemed fine.
Open the worksheet & go to the Developer Tab. Open VBA, and back on the Developer ribbon select "View Code". See if it opens any similar Code (apart from your Module) specific to that worksheet (eg. Sheet2 (Code). I found that I had duplicated the code on the worksheet in addition to the Module. Delete the "worksheet" code. (You may need to save the workbook & re-open at this stage). When I deleted the worksheet code, the module function then worked.
In addition to checking some of the above mentioned items, you might need to specify the filename where the custom function is actually defined, e.g. cell content
=XLstart.xlsm!myCustomFunc(Arg1,Arg2)
where myCustomFunc is defined in the startup file XLstart.xlsm.
Following the Excel help for "Correct a #NAME? error":
In the formula bar, select the [suspect] function name.
In the Name Box (to the left of the formula bar), click the arrow and then select a [user-defined] function from the list that Excel suggests.
This will add the filename per the above format.
MS 2010, Windows 10.
Here's why I got that error. This answer is not provided so far.
If you have two or more workbooks (spreadsheets) open, then you may have your module under the other workbook - not the only you want to do the calculation on. This may seem impossible but ... as soon as you open the Developer/VBA code editor Excel wants to show you the structure (objects, modules, etc) of every open workbook. It's not what I expect as a developer, but there it is. So like me, you may have pressed 'Add module' and dropped the code in another workbook and worksheet.
If this is your issue, nothing mention above will work. Move your VBA module and code to the correct spreadsheet visible through this VBA code editor.
True,
I had the same (in Excel 2010) and when I migrated to Excel 2016 , the function prototype was shown, but when I completed the function, the #NAME error was shown with a pop-up... so the code was never triggered.
It turned out I had a Macro of the same name as a Sub or UDF function !
I renamed the Macro, and then it worked
Cheers
Another cause I found for the #NAME? error is that the macro workbook with the custom function has a range name the same as the function name. I changed the function name and solved the problem.
This solution applies to users with an Excel installed in another language than "United States English":
I had a similar problem when making a copy of the active workbook to duplicate it and immediately opened the copy afterwards:
Non-working code:
ThisWorkbook.SaveCopyAs NewFileName
Set wb = Workbooks.Open(FileName:=NewFileName)
This always showed me several cells with Error 2029 / "#NAME?". If I opened the Workbook "the official way" via the File-Menu it worked as expected.
I solved the issue by adding the parameter "local:=true" to the open statement:
Working code:
ThisWorkbook.SaveCopyAs NewFileName
Set wb = Workbooks.Open(FileName:=NewFileName, Local:=True)
as VBA expected english function names in my German workbook. With this parameter VBA is told directly to use the local names.
I hope that helps someone not to loose several hours, as I did...
Short answer - if the function was working before, RESTART YOUR COMPUTER.
Long answer - I had this same thing happen to me. The problem is that the function I had created had been working for months. Then one day it just started showing a #NAME error instead of working like it was before. I had tried closing all other excel workbooks and even closing excel all-together and re-opening the sheet. Nothing seemed to work. Then for kicks, I edited the code to where I knew VBA would complain that there is a compile error. Surprisingly, it didn't complain. OK... I saved and closed excel anyways and then restarted my computer.
Once rebooted, I re-opened the excel workbook. Then VBA finally gave me a compile error. So I changed my function back to the original code I had before and now the sheet is running the function like it is supposed to. No more #NAME error.
Not sure all of those steps are necessary, but simply restarting the computer seems to have fixed my issue.

Excel VBA Custom Function giving "Reference Not Valid" Error

I've attempted to track down and solve this error for hours now and I just can't figure it out. Here's the setup.
I have one excel workbook which has two sheets in it: "Input" and "Calculations". I've written in a few custom functions to calculate certain things. These functions are used in the "Calculations" sheet, but reference cells in the "Input" sheet. Now if I'm just using the sheet itself everything works perfectly fine and the functions work.
However, I have a second excel workbook which interacts with the first. I have a macro in the second workbook which attempts to define values in the "Input" sheet of the first workbook. However, when it does this, suddenly the functions don't work. When I attempt to trace the error of the cell in the "Calculations" sheet, and attempt to go to the cell in "Input" sheet, it claims the reference is invalid.
I have no idea what the problem is. At first I thought it maybe had something to do with the name of the first workbook (which was "Log K Calculator 7.0.0.xlsm") but I've tried changing that and I get the same problem. Here is the macro in the second sheet which attempts to change the values in the first:
Sub macro()
Dim logK As String
Dim this As String
logK = "Log K Calculator 7.0.0.xlsm"
this = ThisWorkbook.Name
Workbooks(logK).Activate
Workbooks(logK).Sheets("Input").Cells(11, 4).Value = Workbooks(this).Sheets(1).Cells(1, "B").Value
Workbooks(logK).Sheets("Input").Cells(12, 4).Value = Workbooks(this).Sheets(1).Cells(2, "B").Value
Workbooks(logK).Sheets("Input").Cells(14, 4).Value = Workbooks(this).Sheets(1).Cells(3, "B").Value
End Sub
I know this thread is 4 years old but, just in case anybody is still searching for the answer like I was, for me this error was caused not by my code but by the fact that I was running the procedure by clicking on a textbox which I had copied from another workbook. I had attached the procedure/macro to the textbox but had forgotten that there was already a hyperlink assigned to the same textbox. Removing the hyperlink fixed the issue.
You might want to check how Excel is treating whatever value is sitting in your external workbook. E.g. is it treating the numbers as text? You could check this by entering, say '10000, '300 and '600 manually in the input sheet (forcing it to treat them as text but display numbers), and see if you get the same ref error.
If you do, then casting the value into a typed variable (Long, Decimal, you name it) would probably fix the issue. Or at least make VBA choke on the incorrectly formatted ones (e.g. some have spaces in them, which might explain the apparent inconsistency)

Resources