VBA - Is there a way to get a Constant value from a variable String? - vba

I have created few Constants (VBA identifiers declared with the key word Const) that look like Rg_Euro, Rg_Usd, Rg_Cad, ... to define specific regions into my workbook.
As I have these "extensions" (Euro, Usd, Cad, ...) in the DataBase that I'm working with, I tried to get the values of my constant by creating a string like this : Str = "Rg_" & extension(i)
But I can't seem to find a workaround to call the Constant and get its value from it... I'm googled it but didn't found what I was looking for and I'm starting to think that it might not be possible directly...
I thought of a User Defined Function with a Select Case on the String to return the right value, but it is just going to add another function, so I'm looking for a more direct solution if there is one!

I'm not a pro and my answer it's only what I've done to solve a similar problem, anyway, I hope it helps:
You can add controls (for example Textbox) named as your constants and set the value you need, then you'll be able to catch any value with this:
Me.Controls("RG_" & extension(i)).text

Related

VBA DIM a string of TEXT to use as part of a value

I am trying to calculate an abbreviation based on a combobox value to use it as part as a value in another module and for some reason the value doesn't transfer to another module at all.
I have it declared as
Public MachineLetter As String
Above the module in which an if function finds what the actual string should say.
But when I try to reference this MachineLetter in another sub, it doesn't show up. I did try to do the dim and if function inside the actual module in which I currently need it and it did work. So I wonder what's the problem?
Could someone help me finding a solution to this problem?
Check where you've declared MachineLetter, it should be declared in a standard module.
Also, make sure you you haven't accidentally declared it more than once, for example in a function/sub. If you do that then it's value will be 'hidden'.
Finally, make sure you are actually setting it's value.
I tested and no problem. When something fells strange try to put a breakpoint to see better the problem.

VBA object properties appear in lower case

Sometimes when developing you find that typical property names show up in lower case.
TempVars.Item("abc")
Might appear like this.
TempVars.item("abc")
Or
tbxMyTextbox.Value
Shows up as
tbxMyTextbox.value
The question is why does this happen and how do you fix it?
I've asked myself this question several times and seen others ask it in SO and elsewhere. In most cases the question comes up when searching for answers on other coding errors. Sometimes the developer (me too) wonders if there's something wrong that they're missing that causes this lower case issue.
A while back I ran across an answer (I'll add the original reference when I run across it again) that made the most sense and actually allowed me to correct this nagging issue.
Apparently it happens when you use a variable in lower case that has a name that's the same as a property.
So
Dim value as string
will result in
myObject.Value
appearing as
myObject.value
Solution?
Because VBE apparently considers this variable across the entire IDE -- apparently without regard to scope; the way to revert to the proper case is to temporarily create the variable in upper case and avoid naming variables with the same name as existing properties in your application.
So the solution for the .value / .Value issue above would be to temporarily include the line
Dim Value as string
in a module within your app. Run the code that includes this line.
Afterwards, remove this temporary line of code.
Then as hoped, .value properties will again appear as .Value and not .value.
Of course, we should avoid those reserved words in the first place, but if at some point that wasn't done the above should fix the issue.
Here are some reserved word links:
Microsoft - Access 2002 & later
Allen Browne's bad word list
Reserved Word search tool (various languages)
Thanks to #ScottCraner for the nudge about avoiding reserved words.
Try something like this:
CallByName(myObject, "value", VbGet)

Check if string follows format

Basically I have created an acronym finding macro and it works well except it includes all of our reference numbers. Now unfortunately changing the search parameters won't work as many acronyms include both letters and numbers.
My idea was to compare the string, once found, and if it is in the reference number format e.g.
LetterNumberNumberLetterLetterNumberNumberNumberNumber
I will simply not include it.
I'm certain there must be a simple way of doing this and me not being able to locate it is a case of not knowing what to search for but anyway thank you in advance for the help.
Use LIKE:
'//LetterNumberNumberLetterLetterNumberNumberNumberNumber
if ucase$("A12BC3456") like "[A-Z][0-9][0-9][A-Z][A-Z][0-9][0-9][0-9][0-9]" then
msgbox "is ref no."

Beginner with OO CLI to generate PDFs and Uno.Any issue in VB.Net

I'm a beginner in writing code that allows us (my company) to generate reports using the OpenOffice DLL allowing the generation of PDFs. I'm using ASP.NET, writing in VB. I need to be able to modify the cell of a table using the xPropertySet member/functions. So I have a variable named Property which is of the the xPropertySet and references the cell that I want. I'm assuming that I need to use the setPropertyValue function but I'm not entirely sure what arguments I should give it. The end result I want is the following:
Description: ________________
I need to figure out how to make the cell that I have above with the underscores just a border on the bottom. Because I'll have some fields that will be pre-populated. As a test I tried doing objCell.Property.setPropertyValue("CharColor", 255), but I get an error saying that Integer cannot be casted to an uno.Any type. Which, unfortunately I know nothing about. Any help would be greatly appreciated. Thank you.
Alright, sorry all I figured it out finally. All I did is created a border variable of type BorderLine like follows
Dim border As New unoidl.com.sun.star.table.BorderLine
Then I set the outerWidth like so:
border.OuterLineWidth = 1
Then I use the setPropertyValue method for the specific cell that I need So something like this:
objCell.Properties.setPropertyValue("BottomBorder", New uno.Any(border.GetType, border))
Of course Properties between ObjCell and Properties is the xPropertySet of the cell. Anyway, hope this helps someone else.

Calculate a percentile using EPPlus

I'm trying to use either PERCENTILE.EXC, PERCENTILE.INC or PERCENTILE.
Looking at FormulaParserManager.GetImplementedFunctionNames() these are not implemented functions.
I wondered if I could set the formula and leave it to Excel to calculate. So far I've not got this to work and I get a #NAME? and "The formula contains unrecognized text". Merely clicking in the formula bar causes the formula to be calculated correctly.
Inspecting the internals of the Excel file I am creating (via EPPlus):
_xludf.PERCENTILE.EXC(B14:B113,0.95)
whereas in Excel I get:
_xlfn.PERCENTILE.EXC(A14:A113,0.95)
I think this is user defined function vs function. I've tried prefixing "_xlfn." to my formula string.
This is as far as I've got I think I either need to roll my own percentile calculation in code or manipulate the xml in the Excel file maybe.
Any help appreciated.
Doh! I spend all afternoon stuck, post a question here and then immediately suss the answer...
Anyway in case anyone is interested:
var row95 = percentile95RowLookup[profileKey];
var percentileRange = sheet.Cells[row, i, row+ profileCollection.Profiles.Count-1, i];
sheet.Cells[row95, i].Formula = $"_xlfn.PERCENTILE.INC({percentileRange.Address},0.95)";
With the important proviso that workbook.CalcMode is not Manual.