Is there a way pass variable in if condition in Vba - vba7

If (Formclub) Then do stuff
Formclub = ActiveSheet.cells(1,119).value or ActiveSheet.cells(1,120).value or ActiveSheet.cells(1,119).value
I am fetching this data from excel file and checking if this condition is true

Related

Can Evaluate be used to set the value of a global variable without calling a macro or function?

My requirement is to use .Evaluate() from VB.net to set a variable in an Excel VBA add-in. I have access to the sheet in VB.Net. Therefore I can easily execute iSheet.Evaluate("MyMacro(12)"). The add-in's MyMacro() is certainly capable of setting the value of a global variable. However, we are looking for a way to avoid having to modify the add-in to get the global variable set.
I was hoping that I could call (from VB.net) something like iSheet.Evaluate("myVar = 12"). Is there a way to use an existing VBA function or Evaluate syntax to set a variable to a value given the name and value in a string?

Make Text Box visible with condition

I am new at VBA and I am trying some code and I could not find a solution or where I am doing wrong.
What I want: The combination of two different text boxes make another text box visible(so it starts in useform not visible). With one condition I could do it using select case(LLL) but when I try it with another variant it does not work(XXX). There is no error message, the code runs but does not show the text box.
Sub Visible()
If userform.TextBox5.Value = "XXX" And userform.TextBox10.Value = "245" Then
userform.TextBox1.Visible = True
userform.T_1.Visible = True
Else
GoTo LLL
End If
LLL:
Select Case True
Case userform.TextBox5.Value = "LLL", userform.TextBox10.Value >= "145"
If userform.Option000.Value = True Then
userform.TextBox1.Visible = True
userform.T_1.Visible = True
Else
userform.TextBox1.Visible = False
userform.T_1.Visible = False
End If
End Select
I understand some of your questions I must say that my code is quite big one cause it is linked to SAP in order to get some values from there and due to its size the complete code is splitted in modules and I am only sharing the module where I am facing problems.
There is one case for select case statement cause it was the only way that it worked close for what I need. I have a lot of variables for the fields TextBox5 and TextBox10 the values of these textboxes come from SAP and when I combine them, other Text Boxes shall be visible depending on the variables given. The problem is that for just one combination (this one is the one that I applied the select case statement) I need another variable (option000) so that TextBox1 and T_1 become visible. When I tried to do it only with if statements it did not worked.

Prevent Excel from making links to external macro functions in formulas absolutely

There are two workbooks: The view foo.xlsm and the control bar.xlsm which gets loaded from the foo.xlsm with the Workbooks.Open()-Method and includes the VBA-Code necessary for the view. It does so because there are multiple possible paths to the bar.xlsm which have to be checked prior.
Set wC = Workbooks.Open("path\bar.xlsm", ReadOnly:=True, Editable:=False, AddToMru:=False)
In foo.xlsm there is a formula referencing to a macro function from bar.xlsm:
=bar.xlsm!functionName(parameter)
The problem is that on every startup excel modifies that link to the last path from which the bar.xlsm got loaded successfully, e.x.:
='C:/Users/X/Desktop/bar.xlsm'!functionName(parameter)
This behaviour is unwanted because the path of bar.xlsm can and will change. How to prevent excel from making this link absolutely?
In the meantime I have found an uncomplicated workaround. I call the desired method using a wrapper function in the view workbook:
Public Function functionWrapper() As Integer
functionWrapper = Application.Run("bar.xlsm!functionName(parameter)")
End Function
The wrapper function is called inside the desired cell formula as follows:
=functionWrapper(parameter)

Formulas to be triggered when Excel is opened or saved

I have several custom-made (VBA) formulas in my Excel. Examples are
= isfileExists(c3)
= isReadOnly(c3)
There are multiple calls to these functions (200/column)
These are slowing down the file as they involve heavy VBA. I want the above formulas to be run only when user opens the file or saves the file only!
How can I go on to do this without touching 'enabling/disabling automatic calculations'? (Please note I do know how to write the event functions but I am looking for an idea to put inside them/anywhere)
Sample idea I have is, adding a ' infront the formulas and whenever user opens/saves, that ' (apostrophe) would be removed using macro; hence the formulas would calculate. Any other easier suggestions?
Thanks much!
You can cache the formula results in a Global (or Static inside the function) dictionary object: once you've checked a file once you can use the cached result instead of repeating the file check)
Public Function ExpensiveCheck(fpath As String) As Boolean
Static dict As Object, ans As Boolean
'create the dictionary if not already created
If dict Is Nothing Then
Set dict = CreateObject("scripting.dictionary")
End If
If Not dict.exists(fpath) Then
ans = (Dir(fpath, vbNormal) <> "") 'do your checking here
dict.Add fpath, ans
End If
ExpensiveCheck = dict(fpath)
End Function
The dict will be lost when the session ends, so the formula should always refresh the cache when you open the file.
Downside is you may need some mechanism to clear/refresh the cache if you want to update the output after some change in the files being checked.
If you don't want your UDFs to calculate along with the rest of the worksheet cells, then you don't want UDFs; make them Private, or stick Option Private Module at the top of the standard module where they're declared, to make them unavailable to use as custom worksheet functions.
Then handle Workbook.Open and Workbook.BeforeSave events, and have these two handlers call some Private Sub that's responsible for writing to the cells you currently have these UDFs in.
That way the values will only be computed when the workbook opens and before it's saved.
UDFs that incur disk I/O aren't ideal: keep that processing for macros.

RecordSet and Function from vb6 to vb.net

I was wonder how to convert this line of code to vb.net. I changed the dataset to recordset and it is indeed a function. When i convert It gave me this error. The dataset is filled with values from Database Its just that this "!" is the main error can anyone tell me what that means in vb6? The error that came out was"Dataset Cannot Be index because there is no default Properties"
'getting values from database procedure
If priRecordSet!CodeStatus Then
IfCodeStatus = True
Else
IFCodeStatus = False
End If
Return IfCodeStatus
To access a particular column value of a particular row, use
myTableData.Rows(rowNumber)(columnNumber)
or
myTableData.Rows(rowNumber).Item(columnNumber)