I have a simple Function to get and save values in array:
Function GetDataOwner()
Dim DataOwner(2) As String
DataOwner(0) = Sheets(1).Range("H21").Value
DataOwner(1) = Sheets(1).Range("I21").Value
DataOwner(2) = Sheets(1).Range("J21").Value
End Function
In other function, I would like read value:
Sub GenerateDB()
Dim DataOwner() As String
DataOwner = GetDataOwner()
MsgBox DataOwner(1)
End Sub
But Excel say me error 13 type mismatch. I am novice with VBA function
Soluce:
Function GetDataOwner()
Dim DataOwner(2) As String
DataOwner(0) = Sheets(1).Range("H21").Value
DataOwner(1) = Sheets(1).Range("I21").Value
DataOwner(2) = Sheets(1).Range("J21").Value
GetDataOwner = DataOwner
End Function
Sub GenerateDB()
Dim DataOwner() As String
DataOwner = GetDataOwner()
MsgBox DataOwner(1)
End Sub
Thanks to #Comintern
Related
I want to replace several characters in a string with several other characters with the following function:
Public Function CharacterChange(S As String) As String
Dim A As String * 1
Dim B As String * 1
Dim i As Integer
Const AccChars = "°é*±äöüßaaŐŰú#~Mµ"
Const RegChars = "DEX+aousaaouudcMU"
For i = 1 To Len(AccChars)
A = Mid(AccChars, i, 1)
B = Mid(RegChars, i, 1)
S = Replace(S, A, B)
Next
CharacterChange = S
Exit Function
End Function
Then I call the function here:
Private Sub ButtonClick()
...
CharacterChange(MyText)
...
End Sub
But it seems I don't get back the result of the function, after calling it I still see my original text with the special characters. What is wrong, please advise.
I have a simple function to get data:
Function GetAppro(Current_Sheet As String)
Dim myArray As Variant
myArray = Worksheets(Current_Sheet).Range("A3:C6")
GetAppro = myArray
End Function
And other funtion to get other data:
Function GetTabs()
Dim Get_Tabs_generated(2) As String
Get_Tabs_generated(0) = "AA"
Get_Tabs_generated(1) = "BB"
Get_Tabs_generated(2) = "CC"
GetTabs = Get_Tabs_generated
End Function
In my final procedure i do:
Sub GenerateDB()
Dim Appro() As String
Dim Tabs() As String
'Init
Tabs = GetTabs()
For Each Tabs_item In Tabs
Appro = GetAppro(Tabs_item.Value)
MsgBox Appro(0, 0)
Next Tabs_item
End Sub
Excel say me compile error Object required (Error 424). I am novice with functions
Solution:
Do not forget CStr()
Sub GenerateDB()
Dim Appro() As String
Dim Tabs() As String
'Init
Tabs = GetTabs()
For Each Tabs_item In Tabs
Appro = GetAppro(CStr(Tabs_item))
MsgBox Appro(0, 0)
Next Tabs_item
End Sub
Special thanks to #ScottCraner
Is it possible to make the following function returns either multiple values or single value according to how the user enters formula?
Public Function Test(ByVal flNumber As Double) As Variant
Dim flResult1 As Double
Dim sResult2 As String
Dim bArrayFormula As Boolean
flResult1 = Round(flNumber / 10 ^ 6, 1)
sResult2 = "million"
' How to know that the formula is entered as an array formula?
If bArrayFormula Then
Test = Array(flResult1, sResult2)
Else
Test = flResult1 & " " & sResult2
End If
End Function
Just examine Application.Caller
Public Function SmartFunction() As String
addy = Application.Caller.Address
If Range(addy).HasArray Then
SmartFunction = "Array Formula"
Else
SmartFunction = "Normal Formula"
End If
End Function
I'm still new to VBA Excel coding. Do tell me if there's anything that needs improvement.
In the example below I'm trying to get the list of even values from the generate class and insert into the excel vba sheet. But how do I count the number of list returned?
Private Function Generate()
Dim red(1 To 20) As String
For i = 1 To 20
red(i) = i * 2
Next i
Generate = red()
End Function
Sub Format()
Dim str() As String
str() = Generate
Range("A1").Select
With Selection
For i = 1 To str().Count 'what do I do with this? Obviously str().Count is not working.
.Offset(1, i).Value = str(i)
Next
End With
End Sub
Thank you.
Managed to solve on my own and here is the answer:
Private Function Generate()
Dim red(1 To 20) As String
For i = 1 To 20
red(i) = i * 2
Next i
Generate = red()
End Function
Sub Format()
Dim str() As String
str() = Generate
Range("A1").Select
With Selection
For i = LBound(str) To UBound(str)
.Offset(i - 1, 0).Value = str(i)
Next
End With
End Sub
I am attempting to do a VLOOKUP on a different worksheet based on given parameters in the function. I've played around with it for several hours and can not figure out why it is not working. I cut down the code as much as I could to test, but am unable to effectively find a solution. I think it might be an issue of how I am calling the range from the other worksheet for the VLOOKUP. Code is below. Please advice. If I'm unclear about what I'm asking just ask and I will provide feedback. Thank you
Function GraphDataA(cR As String, time As String, aClient As String, tps As String, dat As String)
Dim client As Boolean
Dim day As Boolean
Dim tot As Boolean
Dim dayTotData As Range
Dim dayTotDatas As Worksheet
Set dayTotDatas = ActiveWorkbook.Sheets("DayTot")
Set dayTotData = dayTotDatas.Range("A3:AI168")
client = False
day = False
tot = False
If date = "" Then
GraphDataA = ""
End If
If aClient = "" Then
GraphDataA = ""
End If
If cR = "Client" Then
client = True
End If
If time = "Day" Then
day = True
End If
If tps = "Total" Then
tot = True
End If
If client = True Then
If day = True Then
If tot = True Then
GraphDataA = WorksheetFunction.VLookup(aClient, dayTotData, WorksheetFunction.Match(dat, dayDate, 0) + 8, _
False)
End If
End If
End If
End Function
VLOOKUP() will throw an error if nothing matches. So you need to add error catching code to your function.
You need to modify the function as
Function MyFunction() as Something
On Error Goto ErrorHandler
' Your existing code goes here
Exit Function
ErrorHandler:
MyFunction = -1 ' Or something which indicates that the value isn't found
End Function
You don't appear to be returning any value from your function. Try adding As Variant to the end of the first line like so:
Function GraphDataA(cR As String, time As String, aClient As String, tps As String, dat As String) As Variant