简体   繁体   中英

Formula entered in Cells by VBA gives 1004

I'm using code to populate an Excel sheet with field values (eg subject, sender, etc) from Outlook.msg files. The Outlook.msg files are in the same folder as the Excel file.

It would be convenient if I can make a hyperlink to these messages in this Excel file. I want the hyperlink to be 'dynamic' in such a way that if I move the whole folder (with Outlook.msg files and the Excel workbook) to antother location that the link still works.

I use this code for creating the 'dynamic'hyperlinks: Hyperlinkstring = "=HYPERLINK(MID(CELL(""filename"");1;FIND(""["";CELL(""filename""))-1))" & sName

where sName is the Outlook.msg file name Then I populate the cells in a loop with

Worksheet("sheetname").Cells(Row, Column).Formula = Hyperlinkstring

This gives me the 1004 run-time error. When I put an additional space before the "="sign like " =HYPERLINK(MID etc... the code runs well and the cells are filled and when I remove the space manually the hyperlink works like a charm. Why is this not working directly and is there another way?

When using .Formula you need to use commas , instead semicolons ; in formula (even if your local separator is semicolon):

Hyperlinkstring = "=HYPERLINK(MID(CELL(""filename""),1,FIND(""["",CELL(""filename""))-1))" & sName
Worksheet("sheetname").Cells(Row, Column).Formula = Hyperlinkstring

Alternatively you could use .FormulaLocal instead .Formula . In that case you should use your local separator in formula (ie semicolon ; ):

Hyperlinkstring = "=HYPERLINK(MID(CELL(""filename"");1;FIND(""["";CELL(""filename""))-1))" & sName
Worksheet("sheetname").Cells(Row, Column).FormulaLocal = Hyperlinkstring

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM