Write the date in BDS function automatically - excel

I'm working with data from Bloomberg. My code is :
Sub Code_Fin()
Sheets("SPX").Cells(2, 3).Formula = "=BDS(""SPX Index"", ""INDX_MEMBERS"",
""END_DATE_OVERRIDE=20210810"",""cols=1;rows=1000"")"
End Sub
Do you know please, how replace the ""END_DATE_OVERRIDE=20210810"" by something like ""END_DATE_OVERRIDE=today()"" ? I would like to change the date automatically.
Thank you !

Related

Create Shortcut via VBA

I wonder if it's possible to make a shortcut with multiple targets, because my code doesn't work with 3 paths. Below code and screenshots.
Can you help? Thank you all in advance
https://ibb.co/pWs87P9
I want something, like in the screenshot below (I've done it manually)
https://ibb.co/9tH4Rx5
C:\Windows\System32\notepad.exe "C:\Temp Logs\app.vbs" "C:\Temp Logs\try.bat"
Sub aa()
Dim sShortcutLocation As String
sShortcutLocation = "C:\Temp Logs\AA.lnk"
With CreateObject("WScript.Shell").CreateShortcut(sShortcutLocation)
.TargetPath = "C:\Windows\System32\notepad.exe ""C:\Temp Logs\app.vbs"""
.Arguments = "C:\Temp Logs\try.bat"
.Description = "Shortcut to the file"
.Save
End With
End Sub

Excel 2013 VBA Like condition with colon won't work

The value of gettxt = 'ATCH 6: Page 2 of 2
gettxt values are from text formatted cells.
I cannot get the following Like condition to be true.
If LCase(gettxt) Like "*atch #*:" Then ...
Suggestions on how to fix the Like statement are appreciated.
You're just missing one more "*" after the colon.
Your condition states that there shouldn't be anything AFTER the colon, but your "gettxt" value has something in it after that.
So the correct condition should be:
If LCase(gettxt) Like "*atch #*:*" Then
Full working version:
Sub test()
gettxt = "ATCH 6: Page 2 of 2"
If LCase(gettxt) Like "*atch #:*" Then
Debug.Print "Working!"
Else
Debug.Print "Not Working!"
End If
End Sub
Hope this helps!

Override VBA's Now() function

Is there a way to override Now() in VBA for testing purposes? Something like this here - What's a good way to overwrite DateTime.Now during testing?, but for VBA.
Thus, I would need Now() to return a predefined value and not the current PC time.
What I do not want is something like this:
Option Explicit
Public Function NowDefined() As Date
'NowDefined = Now()
NowDefined = Format("2016-01-15 15:01:01", "yyyy-MM-dd hh:mm:ss")
End Function
and simply changing in the whole code Now() to NowDefined().
Rather than creating a String variable and then making Excel try to cast it to a Date, you would be better off generating the actual date to start with:
Function Now() As Date
Now = DateSerial(2016, 1, 15) + TimeSerial(15, 1, 2)
End Function
Vityata's answer works, but it is a bit fragile and can get cumbersome: you must remember to remove or comment out the entire function declaration when you're done testing; and put it back in whenever you need to do more testing... and then remember to remove it again when you release... etc.
I'd do something like this:
Const IN_TESTING_MODE As Boolean = True ' Set this to false when done testing
Function Now() As Date
If IN_TESTING_MODE Then
Now = ... 'whatever date you want
Else
Now = VBA.Now() ' back to default behavior
End If
End Function
Here you only have to change one thing to go in and out of testing mode: the value of the constant IN_TESTING_MODE. You can reuse this constant in other functions as well for which you want a different behavior while testing.
Actually this works:
Option Explicit
Public Function Now() As Date
Now = Format("2016-01-15 15:01:01", "yyyy-MM-dd hh:mm:ss")
End Function

Adding OR in IF function in Excel

I want to but an OR in this IF statement. For example IF cell="2" OR IF cell="4" OR IF cell="hello".
Can you help me? In the current code i have only an IF cell="2".
Cell.FormulaR1C1 = "=IF(left(RC[2],1)=""2"",""delete"",""1"")"
Try this (untested but hopefully will work):
Cell.FormulaR1C1 = "=IF(OR(left(RC[2],1)=""2"",left(RC[2],1)=""4"",left(RC[2],1)=""hello""),""delete"",""1"")"

Excel VBA - Execute string as code

I am trying to execute the following string as code using Evaluate but get Error 2029. Anyone know why?
Help much appreciated.
Calculation = "Format(""21/08/2012"", ""MMM"")"
Value = Evaluate(Calculation)
Try instead
Calculation = "TEXT(""21/08/2012"", ""MMM"")"
EVALUATE converts formulas to results, and FORMAT is a VBA function. The formula equivalent is TEXT.
You can also skip the evaluate and use the FORMAT function on the date directly.
You can use most worksheet functions in VBA directly with Application.WorksheetFunction. - for example - try this out:
Sub DateExample()
Dim StringTest As String
StringTest = Application.WorksheetFunction.Text("12/08/2012", "MMM")
Cells(1, 1).Value = StringTest
End Sub
Good Luck

Resources