Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-24-2014, 07:54 PM
BobBridges's Avatar
BobBridges BobBridges is offline Workbook data Names in VBA are string only? Windows 7 64bit Workbook data Names in VBA are string only? Office 2010 32bit
Expert
Workbook data Names in VBA are string only?
 
Join Date: May 2013
Location: USA
Posts: 700
BobBridges has a spectacular aura aboutBobBridges has a spectacular aura about
Default Workbook data Names in VBA are string only?


I'm not sure how to entitle this thread, nor how to google for the answer. I don't use names much in Excel, but I have a situation where it seems useful and I want my program to use it too.

Now, a name can refer either to a range (eg Market!C8) or to a value (2100). I see in VBA that I can get my program to use the range just fine, for example
Code:
Set ro = ThisWorkbook.Names("cFuel").Range
Great, I can use that in a program. But when I want to use one that's just a scalar value, something curious happens. Maybe you already know what I'm going to say.

In my worksheet, for example, one formula refers to "RC5*RpPK". It works fine, multiplying the value in col 5 by 0.45 and displaying the numeric I want.

But in my VBA program, when I refer to Names("RpPK").Value, I get back a string "=.045". When I try adding 1 to it, I don't get 1.45, I get an error. I've tried various combinations, but all I get back is the string, not the value.

Now, this doesn't have to be a deal-breaker; I can always strip off the leading "=" and use a conversion function. But it seems silly; surely Excel has some method or property that just issues the numeric value rather than a string value. The worksheet has access to the numeric value; why wouldn't VBA?

Anyone know how to do this?
Reply With Quote
  #2  
Old 01-27-2014, 08:27 PM
excelledsoftware excelledsoftware is offline Workbook data Names in VBA are string only? Windows 7 64bit Workbook data Names in VBA are string only? Office 2003
IT Specialist
 
Join Date: Jan 2012
Location: Utah
Posts: 455
excelledsoftware will become famous soon enough
Default

Bob,
You have been an amazing help to everyone on this forum. I would be honored to try and help since I have a little bit of an idea of what it might be. could you post a sample worksheet so we can see the name values and hopefully be able to get you an answer.
Reply With Quote
  #3  
Old 01-27-2014, 09:59 PM
BobBridges's Avatar
BobBridges BobBridges is offline Workbook data Names in VBA are string only? Windows 7 64bit Workbook data Names in VBA are string only? Office 2010 32bit
Expert
Workbook data Names in VBA are string only?
 
Join Date: May 2013
Location: USA
Posts: 700
BobBridges has a spectacular aura aboutBobBridges has a spectacular aura about
Default

Wow, excelled, what a kind thing to say! Ok, here's the workbook.

I've just started playing a Facebook game called "Airline Manager". Some of the calculations were a little complex, and I decided that rather than looking up some of the more stable values in a separate worksheet I'd try using the Name manager this time. Here's a map to one example of the problem:

1) In the Market worksheet I collect raw data on each type of aircraft. Out at the right, in columns 11 through 16, are the number of workers I'd have to hire to serve that airplane. The Airbus A300-600, for example (on line 2 of the Market worksheet) is a big passenger plane that requires 2 pilots, 9 on-board attendants, 5 mechanics, 5 clean-up crew, 5 more doing check-in, and 2 supervisors.

2) In the name manager are some values showing what I pay each position. SalChk, for example, shows that I pay check-in crew members $400/day. SalPilot is currently $525/day, and so on.

3) The Market worksheet, as I said, is where I collected the raw data on each airplane. But I decide which ones I want to buy for my hypothetical airline ("TeacherSwift Air") using the analyses on the Aircraft tab. Take a look at column 15, headed "Staff"; each cell in that column passes the model name from col 1 to a worksheet function named "Staff", which calculates the total salary paid daily to maintain one aircraft of that model. The Aerospatiale ATR 42-500, for example, on line 3 of the Aircraft worksheet, costs $3500/day in crew salaries.

4) The worksheet functions are in a code module named Functions. You'll find the Staff function there.

5) The Staff function starts by getting the argument, the aircraft model, and finding what row it's on in the Market worksheet.
Code:
rm = WFMatch(Mdl.Value, so.Columns(1))
(You can find WFMatch in the mCommon code module, if you want to be sure it works right. But it does.)

6) Then the Staff function calculates the total salary by multiplying {the count for each type of crew member} by {the salary for that crew type}:
Code:
  Staff = so.Cells(rm, cnP).Value * NN("SalPilot") _
        + so.Cells(rm, cnCr).Value * NN("SalCrew") _
        + so.Cells(rm, cnM).Value * NN("SalMech") _
        + so.Cells(rm, cnCl).Value * NN("SalCln") _
        + so.Cells(rm, cnCh).Value * NN("SalChk") _
        + so.Cells(rm, cnS).Value * NN("SalSpv")
7) NN is a function that gets the value from the name manager for the given label. You'll find it in the mCommon code module, and now we've come to the problem. What I expected to be able to do for each Name is something like this:
Code:
value = ThisWorkbook.Names("SalPilot").Value
But what I always get when I do that is not the numeric value 525, but the string value "=525".

So if you look at NN, what it actually does is get that string value, then do a Mid(string, 2) on it to cut out the '=', then add 0 to it to convert it to a numeric value, and finally return it to the calling routine.

I gotta believe that isn't necessary; there must be a way to get the numeric value directly. But if so, I haven't figured it out yet.
Attached Files
File Type: xlsm Airline Manager.xlsm (201.7 KB, 10 views)
Reply With Quote
  #4  
Old 01-27-2014, 11:34 PM
excelledsoftware excelledsoftware is offline Workbook data Names in VBA are string only? Windows 7 64bit Workbook data Names in VBA are string only? Office 2003
IT Specialist
 
Join Date: Jan 2012
Location: Utah
Posts: 455
excelledsoftware will become famous soon enough
Default

Wow this was certainly informational. I am sure you thought of this solution but I will post what I would do so others reading this forum can see.

I don't think there is a way to get just the integer value from a name since I believe a name is always a string. This can be verified by trying a Shape Name. I tried entering a shape with the name 6 but it still goes to "6". I think that names have to be strings so they can be referred to in any MS program. Similar to if you use a mid formula on group of numbers it now becomes a string and you have to add 0 to it to bring it back to a number. Good ol' data types.

Here is the solution I would suggest that you could put at the beginning of your Sub and then you can use the same variable name throughout your code.

Code:
SalChk = Mid(ThisWorkbook.Names("SalChk"), 2) + 0
SalCln = Mid(ThisWorkbook.Names("SalCln"), 2) + 0
SalCrew = Mid(ThisWorkbook.Names("SalCrew"), 2) + 0
SalMech = Mid(ThisWorkbook.Names("SalMech"), 2) + 0
SalPilot = Mid(ThisWorkbook.Names("SalPilot"), 2) + 0
SalSpv = Mid(ThisWorkbook.Names("SalSpv"), 2) + 0
By the way this same thing can be achieved using mid and Cstr, or replace and + 0 it looks like there are lots of ways but it does not look like a name can have just a number value since it will always add on that equals sign to make it a string.

If anybody else finds out different please let us all know.

Thanks
Reply With Quote
  #5  
Old 01-28-2014, 07:06 AM
Catalin.B Catalin.B is offline Workbook data Names in VBA are string only? Windows Vista Workbook data Names in VBA are string only? Office 2010 32bit
Expert
 
Join Date: May 2011
Location: Iaşi, România
Posts: 386
Catalin.B is on a distinguished road
Default

Hi Bob,
Have you tried this:
Code:
Application.Evaluate(ThisWorkbook.Names("SalPilot").RefersTo)
Or:
Code:
Application.Evaluate(ThisWorkbook.Names("SalPilot").Value)
Reply With Quote
  #6  
Old 01-28-2014, 11:01 PM
BobBridges's Avatar
BobBridges BobBridges is offline Workbook data Names in VBA are string only? Windows 7 64bit Workbook data Names in VBA are string only? Office 2010 32bit
Expert
Workbook data Names in VBA are string only?
 
Join Date: May 2013
Location: USA
Posts: 700
BobBridges has a spectacular aura aboutBobBridges has a spectacular aura about
Default

Quote:
Originally Posted by execelledsoftware
Wow this was certainly informational.
LOL. I remember a comic translation of womamspeak into manspeak and vice versa. One of the entries was a woman saying "You certainly are...manly." The translation is "you're sweating and you smell a lot". I get the feeling that "informational" may mean "boy, you sure churned out a lot of words, there".

Yeah, of course the way you suggested is the obvious way to do it. But I have a sort of mania about putting frequently-used routines inside smaller routines, to make them easier to use; and I not only call NN five times in this function, but I also call in from the function immediately above. As soon as I saw I needed to do it more than once, that was it; I assumed I'd want it again in yet other routines, sooner or later, so I put it in a separate function.

Quote:
Originally Posted by Catalin.B
Have you tried [Application.Evaluate]?
I'd never even heard of it. And now that I look it up, it seems to me I've been ignoring the methods of the Application object far too long. I think I have some reading and experimenting to do. Thanks, Catalin; I don't know that I'll end up using it—what I have now seems to work—but even if I don't, I'll have learned about Evaluate and those other useful-looking methods and will probably use some of them on another occasion.
Reply With Quote
  #7  
Old 01-29-2014, 05:36 AM
excelledsoftware excelledsoftware is offline Workbook data Names in VBA are string only? Windows 7 64bit Workbook data Names in VBA are string only? Office 2003
IT Specialist
 
Join Date: Jan 2012
Location: Utah
Posts: 455
excelledsoftware will become famous soon enough
Default

Hey Bob,

I didn't intend for "informational" to sound the way it came it out. I was just surprised how the naming conventions worked and how they are rendered in VBA so I thought it was "informational".

@ Catilin the evaluate function looks like it can do quite a bit. Thank you for bringing it up.
Reply With Quote
  #8  
Old 01-29-2014, 09:02 AM
BobBridges's Avatar
BobBridges BobBridges is offline Workbook data Names in VBA are string only? Windows 7 64bit Workbook data Names in VBA are string only? Office 2010 32bit
Expert
Workbook data Names in VBA are string only?
 
Join Date: May 2013
Location: USA
Posts: 700
BobBridges has a spectacular aura aboutBobBridges has a spectacular aura about
Default

Excelled, don't take me too seriously; I didn't assume that's the way you meant it, I just thought it sounded funny. It was self-mockery more than anything else.
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Workbook data Names in VBA are string only? Way to search for a string in text file, pull out everything until another string? omahadivision Excel Programming 12 11-23-2013 12:10 PM
Replace contents form 1 workbook with another based on simular data shabbaranks Excel 1 12-05-2012 11:11 PM
Workbook data Names in VBA are string only? macro to transfer data from one workbook to another workbook virsojour Excel Programming 5 02-01-2011 08:58 PM
inserting a string of data into an MS Word table??? matto Word VBA 0 07-16-2010 09:35 AM
Workbook data Names in VBA are string only? How do I merge data from one sheet in a workbook out into multiple sheets nolesca Excel 4 06-07-2010 08:13 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 01:17 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft