Excel functions don't work with macro - vb.net

So I got this macro to load stuff from another worksheet:
Private Sub Workbook_Open()
Workbooks.Open Filename:= _
"http://DOMAIN/csdav/nodes/5865160/Projektoffertliste.xls"
ThisWorkbook.RefreshAll
ActiveWorkbook.Close
End Sub
and if I try using simple fuctions like =VALUE or =IF it just won't work and it says #VALUE as error code.
=DATEVALUE('http://DOMAIN/csdav/nodes/5865160/[Projektoffertliste.xls]Projektoffertliste'!B2)
Thats one usage example, i tried this too with =VALE because I first thought it was a DATEVALUE problem. the http://.... data getting does work but i can't use it with functions.

Related

Maro to always copy and paste as formulas by default in Excel

I need help to check this macro that intends to copy and paste without formatting. It doesn't work fine.
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
On Error Resume Next
Target.PasteSpecial xlPasteFormulas
Application.CutCopyMode = True
End Sub
How do I make Ctrl-V to paste without any format? I want to keep the excel clean and tidy, so users can copy and paste values and formulas without formatting.
Thanks!
If you want to make your own, custom Ctrl-V, well, you can achieve it this way:
' Code Module ThisWorkbook
Private Sub Workbook_Open()
Application.OnKey "^v", "ThisWorkbook.PasteWithoutFormat"
End Sub
Private Sub PasteWithoutFormat()
If Application.CutCopyMode Then Application.Selection.PasteSpecial xlPasteFormulas
End Sub
Try this: Assigning Macros to Short-Cut Keys in Excel
You'll need to work out how users have access to the macro - there are a few ways to do this and I'm not so sure what will work in your environment, nor your access level to make these options work.
I suppose I'd build a very simple ADD-IN in which I'd embed the macro and there also, assign the macro shortcut to override CTRL + V. Then deploy the ADD-IN according to your company policies.
However, we need more details from you to fully implement the solution.
I went for the easiest way. I set up Ctr-v (in macro's options) to run the following code:
Sub Pstfrmul()
Selection.PasteSpecial xlPasteFormulas
End Sub
Many many thanks for all your answers and comments. Best,
Gerónimo

Run Code In Worksheets Class Code Module In Another Workbook

This code is in a Workbook. ( In a Worksheets Class code Module )
Sub Testie()
Dim FullPathAndName As String
Let FullPathAndName = "'" & ThisWorkbook.Path & "\" & "NeuProAktuelleMakros.xlsm'"
Application.Run Macro:=FullPathAndName & "!FrmProTypeIn", Arg1:=42
End Sub
In another Workbook, named “NeuProAktuelleMakros.xlsm”, which is in the same Folder, I have this code, (in a Normal Code Module):
Sub FrmProTypeIn(MyArg As Long)
MsgBox prompt:="Got Here :). The answer is " & MyArg & " , I forgot the question"
End Sub
If I run the first code, it makes the second code run , ( which tells me I got there and that the answer is 42, I forgot the question )
I would prefer to have the code, FrmProTypeIn() , (which is currently in a Normal Code Module) to be in a Worksheets Code Module. The Worksheet Name is “FoodsLookUpTable”. The Worksheet Code Name is “Tabelle11”
Is this possible and can you give me the syntax?
If that is not possible, what about a simple work around? – The obvious thing I can think of is to have a Call ing code in a normal Module in Workbook “NeuProAktuelleMakros.xlsm” thus:
Sub CallFrmProTypeIn(MyArg As Long)
Application.Run Macro:=Worksheets("FoodsLookUpTable").FrmProTypeIn(MyArg)
End Sub
Then I modify the first code slightly to this:
Sub Testies2()
Dim FullPathAndName As String
Let FullPathAndName = "'" & ThisWorkbook.Path & "\" & "NeuProAktuelleMakros.xlsm'"
Application.Run Macro:=FullPathAndName & "!CallFrmProTypeIn", Arg1:=42
End Sub
That workaround works. But maybe there is a simpler way?
The syntax would be:
Let FullPathAndName = "'" & ThisWorkbook.Path & "\" & "NeuProAktuelleMakros.xlsm'"
Application.Run Macro:=FullPathAndName & "!Sheet1.FrmProTypeIn", Arg1:=42
# Peh
Hi Peh
I had not tried that. That and variations of it do work . Great Thanks. I had been told that this was not possible as I was told that a code cannot be run from a Worksheets code module in another Workbook.
I am wondering if this is telling me that technically I the code has become now a Property of the Worksheets Class module.
So the first three lines here are variations of your suggestion that work.
The last two are typical variation that I had been trying which do not work
Sub PehTesties()
Workbooks("NeuProAktuelleMakros.xlsm").Application.Run Macro:=Worksheets("FoodsLookUpTable").FrmProTypeIn(42)
Application.Run Macro:=Worksheets("FoodsLookUpTable").FrmProTypeIn(42)
Application.Run Macro:=Workbooks("NeuProAktuelleMakros.xlsm").Worksheets("FoodsLookUpTable").FrmProTypeIn(42)
' Application.Run Macro:=Workbooks("NeuProAktuelleMakros.xlsm").Worksheets("FoodsLookUpTable").FrmProTypeIn Arg1:=42
' Application.Run Macro:="'" & ThisWorkbook.Path & "\" & "NeuProAktuelleMakros.xlsm'!FoodsLookUpTable!CallFrmProTypeIn", Arg1:=42
End Sub
Thanks once again
Alan
Summarising for Prosperity: ( Subject to Edits based on any correcting comments. ;) )
It seems that you can call an Excel code routine ( including those in a Worksheets Class code Module ) from another Workbook. You can pass any optional or required arguments to the called routine.
The documentation is not too clear, or does not explain all, or is wrong..
Using the same example, I can ..
_1) do a simple Call
Call Workbooks("NeuProAktuelleMakros.xlsm").Worksheets("FoodsLookUpTable").FrmProTypeIn(42)
Workbooks("NeuProAktuelleMakros.xlsm").Worksheets("FoodsLookUpTable").FrmProTypeIn 42
_2) There is an Application.Run Method.
_2a) But I suggest that this code line, although it “works” , probably does not get a chance to use it..
Macro:=Workbooks("NeuProAktuelleMakros.xlsm").Worksheets("FoodsLookUpTable").FrmProTypeIn(42)
I say this as I could just as well do this and it “works” also
Dim vTemp As IBlogPictureExtensibility
Let vTemp = Workbooks("NeuProAktuelleMakros.xlsm").Worksheets("FoodsLookUpTable").FrmProTypeIn(42)
I expect in the latter I did not get a chance to extend the ability to Extensivibly Blog a Picture as the evaluation set off by the = did an auto intensively initiation of the code as I accidently exposed it at the VB Component ( Tabelle11 ) interface, which pseudo made the Object in a pseudo late binding type stylio
This works also
If Workbooks("NeuProAktuelleMakros.xlsm").Worksheets("FoodsLookUpTable").FrmProTypeIn(42) Then MsgBox prompt:="It wasn't, but it did, so then perhaps was, was but too late - and then .... like did an auto intensively initiation of the routine as I accidently exposed it at the VB Component ( Tabelle11 ) interface, which pseudo made the Object in a pseudo late binding type stylio. It was not Late, it came too late"
Maybe we could refer to the above as a “pseudo” Run Method, way to Run, or Run way
_2b) Aplication.Run “StringReferrenceToMacroName”, MacroArgument1, MacroArgumet2, .....
I think this is using the Application.Run Method correctly; I believe this is wired to take a string reference to the string name of a macro, and also any arguments therefor. The basic syntax therefore would be
Application.Run Macro:="'NeuProAktuelleMakros.xlsm'!Tabelle11.FrmProTypeIn", Arg1:=42
It is important here not to confuse, as I did, a code bit like Tabelle11.FrmProTypeIn with the last string part in that last string, which is the macro name. This macro Name can be seen, for by example, by hitting Alt+F8 to list the literal names of macros
http://imgur.com/KL9pwFq
An interesting advantage of using the Application.Run Method is that the string reference may be extended to include the full path to the Workbook. This has no effect if the Workbook is open. If however, the Workbook is closed, then the workbook is opened by a code line of this form:
Application.Run Macro:="'H:\ALERMK2014Marz2016\NeueBlancoAb27.01.2014\AbJan2016\OutlineGrouping\RoryAppRun\NeuProAktuelleMakros.xlsm'!Tabelle11.FrmProTypeIn", Arg1:=42
_.....
A few last notes:
_A) These two codes are in fact doing the same thing:
Workbooks("NeuProAktuelleMakros.xlsm").Application.Run Macro:=Worksheets("FoodsLookUpTable").FrmProTypeIn(42)
Application.Run Macro:=Worksheets("FoodsLookUpTable").FrmProTypeIn(42)
They only work if Workbooks("NeuProAktuelleMakros.xlsm") is Active. This suggests that Application.Run with unqualified Workbook will go to the Active Workbook. I think that this is one of those occasions showing us that VBA is not really a Object Orientated Programming language. The Application can be called at various levels , but effectively goes “back up” the Hierarchy, so the use of Workbooks("NeuProAktuelleMakros.xlsm") has no effect.
Similarly , if “ProAktuellex8600x2.xlsm” is my workbook with the code in it, ( or any other open workbook for that matter ), then this will still work
Workbooks("ProAktuellex8600x2.xlsm").Application.Run Macro:=Workbooks("NeuProAktuelleMakros.xlsm").Worksheets("FoodsLookUpTable").FrmProTypeIn(42)
The important part is the
Workbooks("NeuProAktuelleMakros.xlsm").Worksheets("FoodsLookUpTable").FrmProTypeIn(42)
_ B) use of Run appears to default to Application.Run
_C ) We do not appear to have a Workbooks or Worksheets Run in such a way as we have , for example, a Worksheets Evaluate in addition to an Application Evaluate. This explains why ...Workbooks("NeuProAktuelleMakros.xlsm").Application.Run Macro:=Worksheets("FoodsLookUpTable").FrmProTypeIn(42) ... is “ignoring” the Workbooks("NeuProAktuelleMakros.xlsm")

Close a workbook at a specific time

I have looked everywhere to get an answer to this and can't figure it out. I'm looking for a way to close and save a workbook automatically at midnight every night because it has important information that people keep losing because they don't save it. I've got the closing and saving part down, if I manually click the X to close Excel, but I can't get it to do it at a predetermined time. I've used Application.OnTime without any success. Here is the code that I'm using to close and save the workbook. I just need the code to have it run at midnight if the workbook is still open.
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Sheet1.Activate
Sheet1.Range("AB18:AD18").Select
Selection.ClearContents
ActiveWorkbook.Save
End Sub
I had trouble referencing the Worksheet .CodeName property but not with the Worksheet .Name property.
Module1 code sheet
Option Explicit
Sub ScheduleClose()
Application.OnTime Now + TimeSerial(0, 0, 4), "Kamikaze"
End Sub
Sub Kamikaze()
Debug.Print "boom"
Application.DisplayAlerts = False
With ThisWorkbook
.Worksheets("Sheet1").Activate
.Worksheets("Sheet1").Range("AB18:AD18").ClearContents
.Save
.Close savechanges:=False
End With
End Sub
Of course, the workbook must be already saved (i.e. not Book1) since no name is provided but a Workbook.SaveAs method could be used if the workbook habitually is not already saved.
This will also leave 'orphaned' VBA projects within the Excel application 'instance'. VBA cannot kill itself as it has to be running to complete the command.

Run-time error '1004': Copy method of Range class failed Excel 2013 when adding a new pivot table

I am getting the error specified in the title of this issue when trying to copy and paste some columns (and it's data) to a new workbook.
The code below used to work till the moment when I add a new sheet with a new pivot table in my workbook, and I don't know the reason:
Sub ExtractData_2()
Workbooks.Add
ActiveWorkbook.SaveAs ThisWorkbook.Path & "\extract_Fcst" & ".csv", 6
ThisWorkbook.Worksheets("Forecast Enrichment").Activate
ThisWorkbook.Sheets("Forecast Enrichment").Range("E:S").Copy Destination:=Workbooks("extract_Fcst.csv").Sheets(1).Range("A:O")
Workbooks("extract_Fcst.csv").Sheets(1).Range("A:O").EntireColumn.AutoFit
End Sub
Does anybody have any idea to how to solve that problem? I have tried a lot of different solutions found in google but any of it works!
Use object variables and assign properly, then break up your copy/Destination to see whether the error raises on the Copy or the Paste, as follows:
Sub ExtractData_2()
Dim csvWorkbook as Workbook
Set csvWorkbook = Workbooks.Add
csvWorkbook.SaveAs ThisWorkbook.Path & "\extract_Fcst" & ".csv", 6
' Unnecessary to "Activate" the sheet...", so you can delete this:
' ThisWorkbook.Worksheets("Forecast Enrichment").Activate
'Try using copy/paste as separate statements to see where the failure may occur
ThisWorkbook.Sheets("Forecast Enrichment").Range("E:S").Copy
csvWorkbook.Sheets(1).Range("A1").Select
csvWorkbook.Sheets(1).Paste
csvWorkbook.Sheets(1).Range("A:O").EntireColumn.AutoFit
End Sub
If it still raises the error, let me know which line it happens.

How to refresh/load RTD Bloomberg function (BDH) in excel in vba

I would like to know if there's a way in VBA code forcing the bloomberg functions (In spreadsheet) to update its value( Any BDH functions)
Targeting Developers have faced similar issue/ have Bloomberg terminal
What have I tried--
Application.RTD.RefreshData
Application.RTD.throttleInterval = 0
Application.CalculateFull
The BDH function do not reload themselve.
The only way I can refresh them now is : I click the "Refresh WorkBook" Button on the Ribbon of the BloomBerg add-in.
Since the Bloomberg Add-in is locked in VBE, I cannot find out the necessary code.
Am I missing any Bloomberg Reference? Can any Bloomberg expert/user point me in the right direction? Thanks.
I did a searching of the keyword "refresh" in the xla by opening it in notepad. Found the following targets:
RefreshAllWorkbooks
blpmain.xla!RefreshAllStaticData
blpmain.xla!'RefreshWorkbookFundamentalsData
blp.xla!IsBlpRefreshAvailable
I tried them out one by one,
the first 2 works by calling:
Application.run "RefreshAllWorkbooks"
Application.run "RefreshAllStaticData"
But not calling them alone ( I guess it's because I somehow can call protected PUBLIC procedure using Application.run)
RefreshAllWorkbooks
or
RefreshAllStaticData
Thanks for all the help
I recently received this answer from bbg chat. I think this is what we are all looking for...
bbg helpdesk: Normally we don''t provide help on VBA on the Help Desk but I have found the below.
You can use the following VBA commands to refresh BDx() formulas:
Refresh based on default option setting: Application.Run "RefreshData"
Refresh current selection: Application.Run "RefreshCurrentSelection"
Refresh current worksheet: Application.Run "RefreshEntireWorksheet"
Refresh current workbook: Application.Run "RefreshEntireWorkbook"
Refresh all workbooks: Application.Run "RefreshAllWorkbooks"
Note: When using VBA macros to refresh Bloomberg formulas,
the formulas cannot complete requesting data while the macro that triggered the refresh is running.
You must use Application.OnTime() to schedule a second function to run after the sub-routine that triggered the refresh exits.
The following code snippet demonstrates the VBA code to refresh all workbooks, followed by a 10 second delay before calling the processSheet sub-routine:
Sub refreshSheet()
Application.Run "RefreshEntireWorksheet"
Application.OnTime (Now + TimeValue("00:00:10")), "processSheet"
End Sub
Sub processSheet()
' perform processing here
End Sub
I've found that changing something in the BDH formula would cause a refresh. Find and replace the = sign would do the tick.
Public Sub Recalc()
Dim ws As Worksheet, FormulaCells As Range, c As Range
Application.Calculation = xlCalculationManual
For Each ws In ThisWorkbook.Worksheets
On Error Resume Next
ws.Activate
Set FormulaCells = ws.UsedRange.SpecialCells(xlCellTypeFormulas).Cells
If Err = 0 Then
For Each c In FormulaCells
c.Formula = Replace(c.Formula, "=", "=")
Next 'c
Else
Err.Clear
End If
Next 'ws
Application.Calculation = xlCalculationAutomatic
End Sub
I have never managed to do what you ask for. The only reliable way I have found to get up-to-date data is by calling the API directly from VBA with BLP_DATA_CTRLLib.BlpData, waiting for the answer, and putting the result into a sheet.
With regards to opening password protected VBA code, a google or stackoverflow search should give you your answer.
This works for me:
WS.Select
WS.Range("A5").Select 'the cell that contains the BDH function
Application.Run "RefreshCurrentSelection"