this is my first time posting! So I created a database on ms access that helps in invoicing and helps to create and analyse data. I wanted to simplify it and add code to it. One of the modules that I created is the following one, it takes four numbers as input, three of them have a default value of 0, if they are anything other than zero then it is supposed to concatenate them. I believe( atleast hope) the module is correct, but when I add it to the form, it displays an error "?Name".
Thanks in advance! :-)
This is the code:
Function MultiApart(ApOne As Single, ApTwo As Single, ApThree As Single, ApFour As Single) As String
Dim a As String
Dim b As String
Dim c As String
Dim d As String
a = CStr(ApOne)
b = CStr(ApTwo)
c = CStr(ApThree)
d = CStr(ApFour)
MultiApart = a
If (ApTwo > 0#) Then MultiApart = MultiApart & ", " & b
If (ApThree > 0#) Then MultiApart = MultiApart & ", " & c
If (ApFour > 0#) Then MultiApart = MultiApart & ", " & d
End Function
Related
I'm trying to control a Pivot table's filter by using VBA and I'm struggling to correctly using the "VisibleItemsList" with a variable that contains the filters settings, I'll post the code for a better understandig:
Public Sub updatePivotTable(param2Set() As Variant)
'Array("[Budget].[AM].&[Aeffe USA]","[Budget].[AM].&[Alberganti]","[Budget].[AM].&[Blokh]","[Budget].[AM].&[Bondi]")
Dim y As Integer
Dim parametersCount As Integer
parametersCount = UBound(param2Set)
Dim filters As String
'filters = "Array("
For y = 0 To parametersCount - 1
filters = filters + """[Budget].[AM].&[" & param2Set(y) & "]"", "
Next y
filters = filters + """[Budget].[AM].&[" & param2Set(y) & "]"""
With Sheets("Export BUDG")
.PivotTables("pvtToExport").PivotFields("[Budget].[AM].[AM]").VisibleItemsList = Array(filters)
End With
End Sub
The error I get is:
Query (1, 28) Sintax ',' incorrect. ("[Budget].[AM].&[Caprioli]",
"[Budget].[AM].&[Bondi]")."
Can anyone point me in the right direction? Thanks.
Public Function SameStuff(s1 As String, s2 As String) As Boolean
Dim bad As Boolean
SameStuff = False
ary1 = Split(Replace(s1, " ", ""), ",")
ary2 = Split(Replace(s2, " ", ""), ",")
Length1 = UBound (ary1)
Length2 = UBound(ary2)
k=1
If Length1<= Length2 and Length1<>0 then
for i=0 to Length1-1
If ary1(i) = ary2(i) then
ary3(k,i) = ary1(i)
End If
Next i
k=k+1
else
Exit function
End If
End Function
Here I take value from Range("A1") - (has 3 words) and value of Range("A2") - (has 4 words). Split them both by finding space between words and store them in arrays. If length of one array is 3 and other is 4, 3 words from both the arrays will be compared. If 3 words are found to be same then Range("B1") and Range("B2") must both have the 3 word name i.e Range("A1").Value. I think this logic will work fine to find similar names like "ABC DEF HIJ " in A1 and "ABC DEF HIJ Limited" in A2.
I am not able to put it in code.
Word length will not remain the same i.e 3,4 .
Use a dictionary would be an easy alternative, you can use the .exists method to do this for you, you have to transfer the array (result of split() ) to a dictionary tho, but that's a loop, not too tricky. Or, you could leave one of the inputas as a string and split only 1, and use if strStringLeftAlone like "* " & strSection(x) & " *" or use instr, with the same idea as the search " " & strSection(x) & " " or find
This should work regardless how long the arrays are, i.e. no matter how many words (and spaces) there are in each of the strings to be compared. Notice I removed the k variable as it didn't seem to serve any purpose in the code. This solution does presuppose, however, that ONLY the LAST word in the two strings is different.
Public Function SameStuff(s1 As String, s2 As String) As Boolean
Dim sameBool As Boolean
Dim i As Long, Length1 As Long, Length2 As Long
Dim tempArr1 as String, tempArr2 as String
Dim ary1 as Variant, ary2 as Variant
ary1 = Split(Replace(s1, " ", ""), ",")
ary2 = Split(Replace(s2, " ", ""), ",")
Length1 = UBound (ary1)
Length2 = UBound(ary2)
If Length1 <= Length2 and Length1 > 0 then
For i=0 to Length1-1
tempArr1 = tempArr1 & ary1(i)
tempArr2 = tempArr2 & ary2(i)
Next i
If tempArr1 = tempArr2 then sameBool = True
End If
SameStuff = sameBool
End Function
Edit
Added some variable declarations to the code that I had forgotten, otherwise the code would not work with Option Explicit at the top of the module.
Dim ssi(11) As String
For i = 0 To 10
If ssi(i) = "" Then ssi(i) = "0"
For j = 0 To Val(ssi(11)) + i
ssi(i) = xuh(Val(ssi(i)))
Next
Next
If ssi(11) = "2" Then
L_zz.Caption = Val(Left(ssi(0) & ssi(1) & ssi(2) & ssi(3) & ssi(4) & ssi(5) & ssi(6) & ssi(7), ssi(10)))
ElseIf ssi(11) = "3" Then
L_zz.Caption = Val(Left(ssi(0) & ssi(1) & ssi(2) & ssi(3) & ssi(4) & ssi(5) & ssi(6) & ssi(7), ssi(10))) * (-1)
End If
I am new here and new to VB as well.
I am trying to understand this double loop in vb code.
ssi(i) is defined as a String variable. and each element is assigned to a specific number in a String. Hope I told it clearly.
My problem with this loop is below.
Since i ranges from 0 to 10, what does this j mean? Does j mean the new ssi(1-10) or another whatever number?
I think the best way to answer your question about understanding a double loop is to try looking at something simpler.
The first program I always write in each new version of BASIC that comes along is a 12 times table.
I've modified it a bit below to be a 12 x 10 table for the purpose of illustrating for you how a double loop works ... hope it helps:
For x As Integer = 1 To 12
For y As Integer = 1 To 10
Console.Write(x * y)
Console.Write(vbTab)
Next
Console.WriteLine()
Next
I have come across a very strange scenario. In a function I will receive a string of condition to be evaluated.
E.g.
(a>b and (b=2 or c!=3))
Where a,b and c are my variable names.
I tried and searched a lot but didn't get anything meaningful.
So my question is: Is it possible to evaluate a string like this? If yes, please provide me some hint on this.
Alternative way, add a reference to Microsoft Script Control
Dim vx As MSScriptControl.ScriptControl
Set vx = New MSScriptControl.ScriptControl
a = 100
b = 200
c = 300
Cond = "(a>b and (b=2 or c<>3))"
With vx
.Language = "VBScript"
.AddCode "function stub(a,b,c): stub=" & Cond & ": end function"
result = .Run("stub", a, b, c)
End With
MsgBox result
Note you will need to replace != with <> as the former is not valid in VB* (and and/or is not valid in jScript)
Here's a proper answer to your question, rather than just a comment.
You need to:
Set a reference to Microsoft Visual Basic for Applications Extensibility x.x (Tools/References) in the VBIDE.
Trust access to the VBA project object model (use Google to find out how to do this for your version of Excel).
Run initValues() then call getConstantValue("(a>b and (b=2 or c<>3))")
Code:
Option Explicit
Dim a As Long
Dim b As Long
Dim c As Long
Sub initValues()
a = 3
b = 2
c = 4
End Sub
Function getConstantValue(constStr As String) As Variant
Dim oMod As VBIDE.CodeModule
Dim i As Long, _
num As Long
Set oMod = ThisWorkbook.VBProject.VBComponents("Module1").CodeModule
For i = 1 To oMod.CountOfLines
If oMod.Lines(i, 1) = "Function tempGetConstValue() As Variant" Then
num = i + 1
Exit For
End If
Next i
oMod.InsertLines num, "tempGetConstValue = " & constStr
getConstantValue = Application.Run("tempGetConstValue")
oMod.DeleteLines num
End Function
Function tempGetConstValue() As Variant
End Function
I'm trying to make a calendar in vb.net and I have come across this problem. I want to append some text into an existing datatable row. When I watch my debugger it says:"In order to evaluate an indexed property, the property must be qualified and the arguments must be explicitly supplied by the user.".
Dim aantalRijen As Integer = 1
For x = 0 To 6
Dim dttopdrachten As New DataTable
dttopdrachten = opdrachtendao.getOpdrachtenByDate(Today.AddDays(x))
If dttopdrachten.Rows.Count > aantalRijen Then
aantalRijen = dttopdrachten.Rows.Count
End If
Next
For z = 0 To aantalRijen - 1
Dim r As DataRow
r = dttAgenda.NewRow()
dttAgenda.Rows.InsertAt(r, z)
Next
For i = 0 To 6
Dim aantalItems As Integer = 0
Dim dttopdrachten As New DataTable
dttopdrachten = opdrachtendao.getOpdrachtenByDate(Today.AddDays(i))
aantalItems = dttopdrachten.Rows.Count
For j = 0 To aantalItems - 1
Dim info As String = dttopdrachten.Rows(j).Item(0).ToString & vbCrLf & dttopdrachten.Rows(j).Item(2).ToString & vbCrLf & dttopdrachten.Rows(j).Item(3).ToString & vbCrLf & dttopdrachten.Rows(j).Item(4).ToString & vbCrLf & dttopdrachten.Rows(j).Item(5).ToString & vbCrLf & dttopdrachten.Rows(j).Item(6).ToString
dttAgenda.Rows(j).Item(i) = info
Next
Next
dgvAgenda.DataSource = dttAgenda
In the code above, I first count how many rows I have to make. Afterwards I add the amount of rows to the datatable (columns are added before). Until here it works, but then when I keep debugging I get the error. I tried googling but nothing could help me so far.
Seem problem has been solved without changing anything. So if someone want to make a calendar. Here's the solution ;)