Adding a hyperlink with VBA--coding syntax - vba

I am trying to add a hyperlink to the workbook data is pulled from to a summary workbook. I can get the filepath to populate the cell no problem but when I enter hyperlink.add I usually get a parameter error.
This provides me with the URL in the selected cell:
.offset(rowcount,112)=sXLFile
I tried this to add a hyperlink but I get a parameter error with the second comma after 'sXLFile' being highlighted:
hyperlinks.Add (anchor:=.Offset(rowcount, 112), address:= sXLFile,,, incidentreport & agentname)
What I think this could would do is make a hyperlink to the filepath saved in sXLFile and read as IncidentReportAgentName. When I reference the exact workbook/sheet in the code below I get a runtime error 13: Type Mismatch.
Workbooks("Incident Reports").Worksheets("Supplementary").Hyperlink.Add anchor:=.Offset(rowcount, 112), Address:=sXLFile, TextToDisplay:=IncidentReport & AgentName
Does anyone see what I am doing wrong?

You've got a couple things going on. You're trying to accomplish too much at once, so we'll need to take a step back.
First issue - you said:
When looking at the arguments needed for the Hyperlink.Add, I didn't want to add a subaddress or screentip and these are optional arguments. I thought I still needed to use the "," as a placemark for those arguments.
Parameters for functions can be entered one of two ways:
FunctionName Parameter1Name:=Parameter1Value, Parameter2Name:=Parameter2Value, Parameter3Name:=Parameter3Value
...is equivalent to:
FunctionName Parameter1Value, Parameter2Value, Parameter3Value
...however the purpose of using the Parameter1Name:= is so that you can skip parameters or include them out-of-order without a problem.
So this:
FunctionName Parameter1Value, , Parameter3Value
...is the same as:
FunctionName Parameter1Name:=Parameter1Value, Parameter3Name:=Parameter3Value
...which is also the same as:
FunctionName Parameter3Name:=Parameter3Value, Parameter1Name:=Parameter1Value
Therefore, when you're specifying parameters with :=, do not add additional commas for missing parameters.
Troubleshooting 101
If the correction above still does not solve you problem try this.
A couple early steps in troubleshooting should be:
Replace all variables and referenced values with the actual values. For example: instead of referring to an Offset to reference the location of the anchor, specify the actual range.
So instead of anchor:=.Offset(rowcount, 112), specify anchor:=Range("A1") or whatever cell the link is supposed to be in.
Instead of address:= sXLFile, use address:= "http://www.google.com (or wherever the destination address is.
Simplify the expression as much as possible to get the basic functionality working. For example, you don't need the "TextToDisplay" label right now to make your link work. Get rid of it, temporarily.
So after those changes, your entire line should be a nice, simplified and functioning:
Workbooks("Incident Reports").Worksheets("Supplementary").Hyperlink.Add Anchor:=Range("A1"), Address:= "http://www.google.com"
If that's indeed doing what it is supposed to, now you can start changing the values to variables and adding more parameters... but make a single change, and then test it. Then make the next change, and test it. One change at a time.
More Information:
MSDN : Hyperlinks.Add Method (Excel)
Chip Pearson : Debugging VBA

Related

Searching for {time} in a message template, without having the workbook parse it as a parameter

I want to create an azure monitor workbook that lets me search for specific message templates. Usually, these message templates contain placeholders. For example:
logger.LogInformation("Finished executing, took {time}ms", time);
Problem is though, if I include the placeholder in the query string for a workbook, it will fail since it considers the placeholder a parameter for the workbook. A very small example can be seen underneath.
AppTraces
| where Properties.MessageTemplate == 'Finished executing, took {time}ms'
This will give me the following error message:
This query could not run because some parameters are not set.
Please set: time
I can't seem to figure out how to escape these characters, since this is not actually related to the kusto language, but more of an issue with the workbook. Can someone please advise?
Unfortunately, we don't (yet) have an escape sequence for parameters in workbooks.
As a workaround, what you can do is split it up so workbooks doesn't detect it as a parameter, something like strcat('Finished executing, took {', 'time}ms'

Error VBA in word code 4605 "can't use SetWidth because the object references the end of a table row."

I'm trying to use a macro in Word in order to adjust the width of a table. But I can't get it to work. When I use the SetWidth method on Selection.Columns in my macro, an error is thrown.
I use a macro that was working previously, but now it doesn't want to do this width adjustment. I already tried to use a variety of other solution like use width or preferredWidth instead of setWidth. I also tried executing that code earlier and it works fine.
This is the code supposed to adjust the column width.
Selection.GoTo what:=wdGoToBookmark, Name:="proj1_cat1_j"
Selection.Columns.SetWidth ColumnWidth:=105, RulerStyle:=wdAdjustFirstColumn
Moreover, the table is a big one. And I'm accessing the first cell of one column in order to adjust it. There is nothing that's attached to that table (like text on the right or something else)
The expected result is simply the table with the column adjusted. But instead I get an error code 4605 saying that I can't use the setWidth method because it's referencing the end of a table row.
Can anybody tell me what I am doing wrong ?
PS : I can't share more of the macro, it's so big it's difficult to know which parts are important to share or not.

Error in formula with predefined variable

I want to add a formula for this range inside my macro and include inside it the variable percentage that I have set in the beginning of the macro. When I run it, it says there is an error "Application or object defined error" in this line. The entire macros runs perfectly, but today I want to add this line and it wont, so obviously the error is in the syntax of the formula. I am providing it below.
ws3.Range("C9:C12").Formula = "=IFERROR(B9/(1- & percentage),"""")"
Everything inside the quotes will appear as a literal string so you will get the word "percentage" appearing in your formula. The spreadsheet doesn't know what this is as you have defined it in your macro (there is no doubt a proper technical term for this).
Amended as per #Peh's suggestion.
ws3.Range("C9:C12").Formula = "=IFERROR(B9/(" & 1-percentage & "),"""")"

Why does Excel stop a macro when using Application.Evaluate?

I'm trying to use the Application.Evaluate function to test if a conditional formatting condition is true. However, what is happening is that the macro just stops - no error message, and the cell in which the UDF is referenced returns #VALUE.
The value of the conditional formula Formula1 property in this instance is "=A1<>VLOOKUP($A1,actWOrders1!$A:$EF,COLUMN(A1),FALSE)"
I've tried replacing Application.Evaluate with ActiveWorksheet.Evaluate, in case it is the Application form is struggling with the context, but the same happens.
Any ideas what might be causing the issue?
Thanks! Screwtape.
If your macro doesn't contain any on error handing statements it would certainly provide you with an error message if at any point it was unable to execute. It could be that your macro is executing completely however is not achieving the result you expect.
Try running it step by step using F8 and observing what is happening on each line to find the culprit, as you step through you can hover your cursor over a number of items such as variables to see what their value is. You might also find reading this webpage will help you to use the tools in VBA to debug your macros.
You created a circular reference.
If you try and type the formula into a cell Excel will tell you that has a circular reference.
You can also step through a formula in the formula bar.
Highlight an expression in the formula that you want the value of
Press [F9] to calculate the expression
The expression will be replaced with its value

Inconsistent recognition of ranges (No errors thrown)

This is code from Excel 2010. All of the code resides within the workbook itself. Information is gathered using internal forms and the code is run.
I have code that retrieves data from a spreadsheet and populates an object with that data. The row number is dynamic as it is dependent on the form input. The column is by the header, not the column number. The following code works perfectly but for two anomalies:
cTank.RowForTankSpecs = rNum
cTank.MP = .Cells(rNum, Range("MP").Column).Value
cTank.Form = .Cells(rNum, Range("formName").Column).Value
cTank.TankProcess = .Cells(rNum, Range("Process").Column).Value
cTank.Location = .Cells(rNum, Range("Location").Column).Value
cTank.TankName = .Cells(rNum, Range("Tanks").Column).Value
cTank.tankID = .Cells(rNum, Range("TankID").Column).Value
First:
The cTank.TankName is retrieving information from a column named "Tanks". That column does not exist. The actual column header is "Tank". But, it is retrieving the correct information. If I change the name to what it really is (Tank), it does not work.
Second:
When the cTank.TankID line is executed, I get the following error on the Range("TankID"):
Runtime Error 1004: Method 'Range' of object '_Global' failed
This one has the appropriate header (column header), but it is not recognizing the range.
I have tried simple things such as changing the order of the code, but it doesn't help. As earlier stated, the other lines work. Later in the program, information is gathered in the same manner but using another worksheet from the same workbook, and none of them are working. I've double checked that strings are strings and integers are integers, etc. I've double checked the column headers match the range names. Nothing seems to jump out at me.
I would appreciate any input you may have on the situation.
Thanks in advance.
Steve
Ok. Being pretty sure my code was correct, I went to the spreadsheet itself. For some reason it was recognizing only certain columns and it was recognizing one of them incorrectly. So I started highlighting the columns that worked and also the columns that didn't. What I noticed was that on the columns that were being recognized, that column header was displayed where the cell location is normally displayed whereas on the columns that were not being recognized, the cell location (i.e. A1, A2, etc.) for the header was being displayed and not the header title itself. The incorrect label was showing up for one of them. As it turns out, the mislabeled column was one that I had used for a form dropdown menu. So, I checked the name manager, and the ones that were working were listed. So anyway, using the name manager, I added named ranges using the headers. Now, when I select the columns, the column header(named range) appears in that window and now, the code works.
Thanks guys for your input. I really appreciate it.
Two things you can do:
Do not use use Range, but as it seems you are using names, use Names("Yourname").Referstorange.
OR
Make sure your names are set up correctly using the Name Manager in Data Ribbon.