Home » excel » excel – VBA fills whole table instead of defined cell

excel – VBA fills whole table instead of defined cell

Posted by: admin April 23, 2020 Leave a comment

Questions:

I ran into a crazy problem that I don’t understand and for which I did not yet find a workaround.

First I have an excel macro filling various sheets on a workbook depending on information from other workbooks. I want to include a hyperlink to a website, containing the value in a specified cell in one workbook, while only displaying the value. I try to insert the formula as following

=HYPERLINK("https://example.com/value","value")

This hyperlink is only available for a certain cell, so it should only appear where a condition is true.
Now for the crazy part: If I fill the cell where I want to insert the hyperlink with a normal string or int, it works just fine and VBA only fills the cell I specified. When I insert a Hyperlink, it fills up the whole column on my worksheet, although everything else is kept in the same way.
Please find a snippet of the code below:

RequestID = Workbook1.ActiveSheet.Cells(i, 1).Text 'Request number

Workbook2.Sheets("Sheet1").Cells(y, 4) = RequestID    

This only fills cell(y,4) with the correct request number as expected

Workbook2.Sheets("Sheet1").Cells(y, 4) = "RequestID"

This only fills cell(y,4) with the String “RequestID” as expected

  Workbook2.Sheets("Sheet1").Cells(y, 4) = "=HYPERLINK(" & Chr(34) &"https://example.com/" & RequestID & Chr(34) & "," & Chr(34) & RequestID & Chr(34) & ")"

This fills up my whole table except for the header with the correctly working hyperlink.

I dont really understand why and I can’t think of any possible problem. Maybe someone here has a hint for me, or knows a workaround.

How to&Answers:

It worked as expected for me when using a normal cell. But when I tried it in a Formatted Table (aka ListObject) then it fill the entire column, because it is a formula and not a value. If this is the case then try setting

Application.AutoCorrect.AutoFillFormulasInLists = False

before you put the Hyperlink in the cell (and then set it back to True)