Formatting an Excel dataBase header using vba - vba

I have an excel table and I want to extract a portion of the columns names: for example, the column name is "Y2014", I should get the String (or integer) "2014" only
here is my code
Function findValue(year As String)
Dim formattedYear As String
formattedYear = Microsoft.VisualBasic.Right(year, 1)
findValue = formattedYear
End Function
I get an error of value either if I try with a simple value doing findValue("Y2014") of if I try with the cell value findValue(Q1) (where A1 content is equivalent to "Tableau1[[#En-tĂȘtes];[Y2014]])":
can someone help? thanks.

First impression looking at your code is that functions always return a value so you have to specify what type the function returns like
Function findValue(year As String) as string
What sort of error are you getting?

I just had to get rid of the Microsoft.VisualBasic. mention

Related

Returning a column number from a table using the =COLUMN function

I have the following function in Excel that returns the column number of a table based on the column header.
The table is called Config and the table column header is value. The below will return to me the column number in excel.
=COLUMN(Config[Value])-COLUMN(Config)+1
Could anybody let me know how this can be used in VBA?
I was hoping i could use something like ...
Application.WorksheetFunction.Column
but it seems Column is not available for me to use.
Would anyone have any ideas on this?
Any help would be greatly appreciated.
Since you're using a table (ListObject), you can refer to a ListColumn by its header. If you need the column number, you can use ListColumn.Range.Column - something like this.
Option Explicit
Sub Test()
Dim lc As ListColumn
Dim col As Long
Set lc = Sheets("Sheet1").ListObjects("Table1").ListColumns("Data1")
col = lc.Range.Column
Debug.Print col
End Sub
The syntax for getting the column would be something like
Worksheets("Sheet1").Range("B1").Column
to get the column number of cell B1.
If you use named ranges this will work too:
Worksheets("Sheet1").Range("NameOfRange").Column
I used a much simpler method:
=COLUMNS(X[[#Headers],[A]:[B]])
Where:
X is the Table name
A is the name of the 1st column
B is the name of the column you need.
This formula is dynamic

Convert string a double in a word macro

I am trying to create a macro for Word 2013 that does the following: the macro should capture the value of a cell of a word table and then add another value and paste the result in another cell of the same table.
My code so far is:
Sub prueba()
Dim a As String, b As String, c As String
Dim entero1 As Double, entero2 As Double
Dim resultado As Double
Dim tabla1 As Table
Set tabla1 = ActiveDocument.Tables(1)
a = tabla1.Cell(Row:=1, Column:=3).Range
entero1 = CDbl(a)
End Sub
But when I run it I get an error 13
To evaluate the error add the following two lines to validate if the data type obtained in "a" was a string
MsgBox (TypeName(a))
MsgBox (a)
And I got the following
I believe that the CDbl function does not finish converting the string to double because as they see the chain has a small square, what is not like to erase it so that the conversion is achieved.
Thank you very much for your help.
One way of extracting just the numeric portion of the Range would be to use the Val function, e.g.
entero1 = Val(a)
If the string a contained, for instance, 123.23XYZ4567 then Val(a) would return the number 123.23.
That should ensure that the non-numeric character that you are getting at the end of your Range is removed.
The answer provided by YowE3K is elegant and has my vote. For further information:
That 'small square' is the end of cell marker which is part of Cell.Range.Text (.Text is the default property returned when returning a range object is inappropriate).
To actually remove the end of cell marker (Chr(13) & Chr(7)) you can use something like this:
?CDbl(Replace$(Selection.Range.Cells(1).Range.Text, Chr(13) & Chr(7), vbNullString))
A possible advantage of this approach is that it may provide better opportunity to trap errors if you are only expecting numeric characters.

Use of Index funtion in VBA returning run-time error message

I can't figure out why my cog a run-time is returning a run-time error message
Unable to get index property for worksheet function.
Below is my code,
Function Distance() As Integer
Distance = Application.WorksheetFunction.Index(Workbooks("Practice 1.xlsm").Worksheets("Sheet2").Range("I11:P18"), Range("K12"))
End Function
The index function needs range, row and column - thus three parameters. In your case you only give two. If you start a new empty application, and you write
?Application.WorksheetFunction.Index(Range("A1:C10"), Range("D1"),Range("D2")).address
in the immediate window, you would get $A$2:$C$2, in case that you write 2 in D2 and 1 in E1.
Or with your code something like this:
Function Distance() As Integer
Distance = Application.WorksheetFunction.Index(Workbooks("Practice 1.xlsm").Worksheets("Sheet2").Range("I11:P18"), Range("K12"),1)
End Function

Get Named Range From Row Number

My worksheet contains rows that have been assigned named ranges. For example, row 10 of sheet(1) has been named "Kumquats". I want to write a function that returns the name of the named range when the row # is passed to it. So the function call would be:
getRowNamedRange(rowNum as integer) as string
and the function would return the string "Kumquats" if passed the number 10.
Any ideas?
So according to the reference supplied by Scott something like the following might do:
Public Function getRowNamedRange(rowNum as integer) as string
getRowNamedRange = Activesheet.rows(rowNum).Name.Name
End Function

Problems with VLOOKUP in VBA

All,
I'm trying to use vlookup in a simple VBA function, but it is continually returning #VALUE!
Here is the code:
Public Function getAreaName(UBR As Integer) As String
Dim result As String
Dim sheet As Worksheet
Set sheet = ActiveWorkbook.Sheets("UBR Report")
' check level 3 then 2 then 4 then 5
result = Application.WorksheetFunction.VLookup(UBR, sheet.Range("UBRLookup"), Application.WorksheetFunction.Column(sheet.Range("UBRLookup[Level 3]")), False)
getAreaName = result
End Function
Any thoughts?
I'm not quite sure what you're trying to do with the "UBRLookup[Level 3]" reference, but as Joseph has pointed out, that's the bit that you're doing wrong.
[ is not a valid character for a named range in Excel.
The column that you're referencing needs to be a numeric value, the offset from the start of the table-array you've defined as your named range.
The below should work, provided the column you want to pull out is the second column in your named range (e.g. what you're referring to as [level 3] is in the second column).
Public Function getAreaName(UBR As Integer) As String
Dim result As String
Dim sheet As Worksheet
Set sheet = ActiveWorkbook.Sheets("UBR Report")
result = Application.WorksheetFunction.VLookup(UBR, sheet.Range("UBRLookup"), 2, False)
getAreaName = result
End Function
Update:
I've had a look at Excel 2007 and from what I can see the column function isn't exposed as an Application.WorksheetFunction.
You can use it on the sheet with =Column(D4), but when trying to autocomplete within the vba editor, the function isn't there. This may be due to a difference in versions, so I'll ignore that for now.
It still definitely seems like you're mis-using the third argument. If you really don't want to use the number reference we need to find out where the function is going wrong.
A few tests along the lines of
Debug.Print Application.WorksheetFunction.Column(D4)
Debug.Print sheet.Range("UBRLookup[Level 3]")
should hopefully help to show you exactly where it's going wrong - I believe that it will object to both of the above, but if it returns some useful information then we may be a step closer to your solution.
Break you function up into more pieces. Then debug it and make sure every piece is set up the way you expect.
Example:
Public Function getAreaName(UBR As Integer) As String
Dim result As String
Dim sheet As Worksheet
Set sheet = ActiveWorkbook.Sheets("UBR Report")
Dim range as Range = sheet.Range("UBRLookup")
Dim column as Column = Application.WorksheetFunction
.Column(sheet.Range("UBRLookup[Level 3]"))
result = Application.WorksheetFunction.VLookup(UBR, range, column, False)
getAreaName = result
End Function
In fact, just by doing that I noticed something weird. You use a range in two different places, but in one place you're looking for UBRLookup, and in another you're looking for UBRLookup[Level 3], is that correct?
I am disturbed by
Dim column as Column =
Application.WorksheetFunction.Column(sheet.Range("UBRLookup[Level 3]"))
You should Dim column as long, I think, and maybe use a variable name that's not to be confused with a property, like lngCol.
This part: sheet.Range("UBRLookup[Level 3]") is suspect as "UBRLookup[Level 3]" is not a valid range name.