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.
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)
Related
Is it possible to have VBA return the location of a checkbox, the linked cell, and the name of the checkbox?
I am building a spreadsheet that has 100's of check boxes. It is crazy linking all these cells, then having to have helper cells to fill data in a report. What I am thinking is having VBA return the location, linked cell, and checkbox name. If I can get those, I can automate most of this. But I am hoping to have one macro work on all checkboxes. Kind of an on change type of command and if that change is a checkbox being marked then it would pull that data and write it where it needs to be.
So in turn, I get rid of the helper cells, and Only have to name the checkbox. That name would be the value I want filled.
I can modify code and work with macros, but not sure how to get the data. Any help is appreciated, or if there's a post somewhere that would help, please point me in the right direction.
Yes, it is possible.
This is an extremely basic bit of code that gets them for you, what you do with that is up to you.
Public Sub PrintCheckboxes()
Dim objShape As Shape
For Each objShape In Sheet1.Shapes
With objShape
If .FormControlType = xlCheckBox Then
Debug.Print "Checkbox Name = " & .Name & ", Title = " & .AlternativeText & ", Linked Cell = " & .ControlFormat.LinkedCell & ", Top Left Cell = " & .TopLeftCell.Address
End If
End With
Next
End Sub
Adapt accordingly.
I have a button that creates a table, (report generator)
in this table i link to various pages with the same syntax.
Worksheets("Engine").Range("E" & EngineStatus).Formula = "=" & newSheetName & "!I3"
It is working, and it is running on a loop.
However, when
I am linking the value from each sheet, in the same cell on each sheet.
but of course only for selected sheets.
The problem occur on the report, as seen on the screenshot.
Sheetname in this case is ACDTCM0137 and cell i3 is what i put in my code..
The output is it counts i as a increased number which it should not.
And it overwrites ALL rows in this column with it's LAST value..
So the last sheet might be called BDMETHR0148 and same cell..
But the last one in, is the one it shows for ALL rows.
How do i ensure that for each row it keeps the formatting from the cove above?
Meaning it should always bee by this syntax
You should read about relative and absolute reference in Excel.
Instead:
Worksheets("Engine").Range("E" & EngineStatus).Formula = _
"=" & newSheetName & "!I3"
Use:
Worksheets("Engine").Range("E" & EngineStatus).Formula = _
"=" & newSheetName & "!$I$3" ' dollar sign $ before column and row locks it
You wrote that you have button to create report, so maybe the code below will be even better (not recalculating until report generated again - values only, not formula):
' all vars before loop "dimmed" only once
(...)
Dim rngEngine As Range
Dim rngStatus As Range
' And in your loop
EngineStatus = ...
newSheetName = ...
Set rngEngine = Worksheets("Engine").Range("E" & EngineStatus)
Set rngStatus = Worksheets(newSheetName).Range("I3")
rngEngine.Value = rngStatus.Value
I have created a code that adds a new line to my spreadsheet with the information that users enter into a user-form. The problem I'm having is that I have formulas in two of the columns that I need to be carried into the new rows. I have tried several different ways to do this but keep receiving an error message. The formula I need carries is
=IFERROR(INDEX(Equipment!$E$3:$E$1000,MATCH($B5&"-"&$A5,INDEX(Equipment!$B$3:$B$1000&"-"&Equipment!$A$3:$A$1000,0),0)),"")
The values that change line to line are the B and A cells after the MATCH command (in this case $B5 and $A5). Can anyone help me figure out how to convert this to VBA syntax so that I can add it to my code? Thanks in advance!
I Converted this formula, and tested it in a loop.
The formula is copied to Column D from rows 11 to 20, where $B5 and $A5 change according to whatever row they are (I hope I understand what you meant).
Sub ConvertFormula()
' modify to where (worksheet) you want to copy this formula
Set eqp_sht = ActiveWorkbook.Worksheets("Equipment")
' modify this according to whatever rows and column you want this formula copied
For i = 11 To 20
eqp_sht.Cells(i, 4).Formula = "=IFERROR(INDEX(Equipment!$E$3:$E$1000,MATCH($B" & i & "&-$A" & i & ",INDEX(Equipment!$B$3:$B$1000&-Equipment!$A$3:$A$1000,0),0))," & Chr(34) & Chr(34) & ")"
Next
End Sub
I've been stuck on this problem for a very long time. I've come up with ways around it but each one is bringing more problems to the table.
My macro copys and pastes info from Microsoft Word into Excel and then formats the excel sheet.
Some of the cells however show a #NAME? error. The reason for this is that the cell copies in " =------- List SC". I need to get into this cell and remove the "=------" and leave only the string List SC.
I cannot just over ride it however because each time it changes. It might be "List FL" or "List BP" or any other variation.
I cannot do an InStr or any other function because the VBA code only reads it as an error and doesn't read the formula bar. It only reads "#NAME?" or recognizes an error.
Does anyone have suggestions as to what I can do?
I have included a picture of the issue. The highlights are the error and formula bar.
Thank you!
Jonathan
My macro copys and pastes info from Microsoft Word into Excel...Some of the cells however show a #NAME? error.
You must edit the string you get from Word before you paste it into Excel. You must remove all invalid characters from the string. Then you can paste it as a formula.
If ----List is the value you want in the cell, then precede it with a single quote: '----List so it will be interpreted as text.
In VBA, use the .Formula method to access the actual formula used and do the string replace on it.
With Cells(1,3)
.Formula = Replace(.Formula, "=------ ", "")
End With
So it turns out that I can store the formula into a string and add an " ' " as the first character in the string. I can then replace the existing formula in the cell with the string (below called stfF).
This is my code.
Dim strF As String
For x = 4 To 100
strF = "'" & Cells(x, 1).Formula
Cells(x, 1) = strF
Next
I'm trying to set the formula for a cell using a (dynamically created) sheet name and a fixed cell address. I'm using the following line but can't seem to get it working:
"=" & strProjectName & "!" & Cells(2, 7).Address
Any advice on why this isn't working or a prod in the right direction would be greatly appreciated.
Not sure what isn't working in your case, but the following code will put a formula into cell A1 that will retrieve the value in the cell G2.
strProjectName = "Sheet1"
Cells(1, 1).Formula = "=" & strProjectName & "!" & Cells(2, 7).Address
The workbook and worksheet that strProjectName references must exist at the time that this formula is placed. Excel will immediately try to evaluate the formula. You might be able to stop that from happening by turning off automatic recalculation until the workbook does exist.
Try:
.Formula = "='" & strProjectName & "'!" & Cells(2, 7).Address
If your worksheet name (strProjectName) has spaces, you need to include the single quotes in the formula string.
If this does not resolve it, please provide more information about the specific error or failure.
Update
In comments you indicate you're replacing spaces with underscores. Perhaps you are doing something like:
strProjectName = Replace(strProjectName," ", "_")
But if you're not also pushing that change to the Worksheet.Name property, you can expect these to happen:
The file browse dialog appears
The formula returns #REF error
The reason for both is that you are passing a reference to a worksheet that doesn't exist, which is why you get the #REF error. The file dialog is an attempt to let you correct that reference, by pointing to a file wherein that sheet name does exist. When you cancel out, the #REF error is expected.
So you need to do:
Worksheets(strProjectName).Name = Replace(strProjectName," ", "_")
strProjectName = Replace(strProjectName," ", "_")
Then, your formula should work.
If Cells(1, 1).Formula gives a 1004 error, like in my case, changes it to:
Cells(1, 1).FormulaLocal
If you want to make address directly, the worksheet must exist.
Turning off automatic recalculation want help you :)
But... you can get value indirectly...
.FormulaR1C1 = "=INDIRECT(ADDRESS(2,7,1,0,""" & strProjectName & """),FALSE)"
At the time formula is inserted it will return #REF error, because strProjectName sheet does not exist.
But after this worksheet appear Excel will calculate formula again and proper value will be shown.
Disadvantage: there will be no tracking, so if you move the cell or change worksheet name, the formula will not adjust to the changes as in the direct addressing.