I have a summary sheet which I use as an index to access several sheets via a hyperlink. I'm extending it so they link directly to the appropriate row within each sheet.
I'm newish to VBA and not sure on the syntax.
I'm basically trying to use the value from variable j as the row number in the cell reference.
"'!A" is the first part of my cell ref in the code below. I'm then trying to concatenate the string to add j as the row number. Can this be done using the code below? I tried surrounding j with brackets, apostrophe to no avail.
For j = 2 To LastUsedRow
link = "=Hyperlink(""#'" & (ThisWorkbook.Worksheets(i).Name) & "'!A" & j",""" & (ThisWorkbook.Worksheets(i).Name) & """)"
Many thanks
Debug.Print is your friend. This is what you get with your current code (assuming j = 1 and ThisWorkbook.Worksheets(i).Name = "Sheet1"):
=Hyperlink("#'Sheet1'!A1,"Sheet1")
The formula should look like this:
=HYPERLINK("#'Sheet2'!A1","Sheet2")
So...add just add the missing ":
link = "=HYPERLINK(""#'" & (ThisWorkbook.Worksheets(i).Name) & _
"'!A" & j & """,""" & (ThisWorkbook.Worksheets(i).Name) & """)"
The including of double quotes within VBA strings is tricky.
link = "=Hyperlink(""#'" & (ThisWorkbook.Worksheets(i).Name) & "'!A" & j & """,""" & (ThisWorkbook.Worksheets(i).Name) & """)"
will get link as =Hyperlink("#'SheetName'!A2","SheetName")
You need to call on what you want to pull from j, such as: j.Value
Related
It may seem to be an easy one as i am novice at VBA. I am trying to fill the filtered blank visible cells with previous non blank hidden cell value in the same column. The Lookup formula is working fine on excel sheet but using it with variable range in VBA is giving Application defined or object defined Error on lookup formula line.
nlr = Cells(Rows.Count, 9).End(xlUp).Row
With Activehseet
Application.DisplayAlerts = False
Range("A1:K" & nlr).AutoFilter Field:=2, Criteria1:=""
Range("A1:K" & nlr).AutoFilter Field:=1, Criteria1:="P * Suite *"
Range("B2:B" & nlr).SpecialCells(xlCellTypeVisible).ClearContents
For Each c In Range("B1:B" & nlr).Offset(1, 0).SpecialCells(xlCellTypeVisible)
n = c.Row - 1
c.Formula = "=LOOKUP(2,1/($B$2:B&n&<>""),$B$2:B&n&)"
I have already tried it with below too, but it didn't work
c.Formula = "=LOOKUP(2,1/($B$2:B " & n & "<>""),$B$2:B" & n & ")"
Please help me resolve this
EDIT: I have already tried this approach, but it didn't work either
c.Formula = "=LOOKUP(2,1/($B$2:B " & n & "<>""""),$B$2:B" & n & ")"
This is the correct approach for concatenating a string into another string:
c.Formula = "=LOOKUP(2,1/($B$2:B " & n & "<>""),$B$2:B" & n & ")"
However, there is a problem with this part of it:
... & n & "<>""), ...
That will place one double quote into the text. I presume you are expecting it to look like <>"" when parsed, which means the line should look like this:
... & n & "<>""""), ...
Why? Because in a string to put one double quote you need to put a double, double quote: "" = " (in finished string). Therefore a double, double quote becomes: """" = "".
Here is the corrected original code:
c.Formula = "=LOOKUP(2,1/($B$2:B " & n & "<>""""),$B$2:B" & n & ")"
I searched on internet, without any help coming out of that...
I simply would like to be able to have my VBA code to write this formula in a cell :
=IF(C4="-"; "-"; Cars!C4*C4*Data!$C$8)
As you guessed, there is a page called "Cars" and one called "Data" where I pick the informations needed.
Of course, as it is a VBA code, the C4 will be 2 variables, one for the C and one for the 4 that will evolve...
Actually, I tried this :
Worksheets("Calculation").Range(Column & PosStartCalc + 1).Formula = "=" & "IF(" & Column & PosStartCalc & " = " & "" - "" & ";" & " - " & ";" & "Cars!" & Column & PosStart & "*" & Column & PosStartCalc & "*" & "Data!" & "C" & "8" & ")"
(The variable Column contains the column letter and the variable PosStartCalc contains the row number)
This hurts my eyes and apparently VBA's ones too as it gives the error "Run-Time error '13': Type Mismatch'
Could anyone tell me how to do that?
Thanks in advance !
Try the following, assuming the column variable is a string and row a long variable. I might not have all the variables right, but you'll be able to get what I meant to do here.
Sub test()
Dim Col As String: Col = "C"
Dim Rw As Long: Rw = 4
With ThisWorkbook.Sheets("Calculation")
Debug.Print "=IF(" & Col & Rw & "=""-"",""-"",Cars!" & Col & Rw & "*" & Col & Rw & "*Data!$C$8)"
.Cells(Rw + 1, Col).Formula = "=IF(" & Col & Rw & "=""-"",""-"",Cars!" & Col & Rw & "*" & Col & Rw & "*Data!$C$8)"
End With
End Sub
So what you might forget easily is to use the , as parameter delimiter in a VBA programmed formula. When you put this on your sheet Excel will automatically replace that with the appropriate delimiter for your region.
Another thing to keep in mind; whenever you about to use a string value in such an function, don't forget to wrap it in double quotes!
Don't forget to remove the Debug.print .... line. It was merely there to show the output :)
I have a table in which I count the records through Worksheet.Function.Countif.
It is nice because it counts the rows using .Rows.Count and so I am alwasy ensured if my table changes the size.
It looks like that (subset of the code):
endrow = .Cells(.Rows.Count, 20).End(xlUp).Row
ws1.Cells(6, 34).Formula = "=COUNTIF(" & .Range("U6:U" & endrow).Address & ",U6)"
I wish to write the the worksheet.function formula in the same way as above but for 'Countifs'. In excel, I would type it like that:
=COUNTIFS($U$6:$U$144;U6;$T$6:$T$144$;"<>"&T6)
How to write it in vba, using 'endrow' as in the first demonstarted code, i.e. without '144' as the last row but with '& endrow' ?
I was trying multiple times, but I cannot get it to work :/
I will appreciate any help.
Try this:
ws1.Cells(6, 34).Formula = "=COUNTIFS($U$6:$U$" & endrow & ",U6,$T$6:$T$" & endrow & "," & """" & "<>" & """" & "&T6" & ")"
This formula gets the last row of column A:
=IFERROR(LOOKUP(2,1/(NOT(ISBLANK(A:A))),ROW(A:A)),0)
I currently have the following code for copying cells:
Set Feeder = Sheets("Projects").Range("B" & Rows.Count).End(xlUp)
With Sheets("Database")
Set Storage = .Range("C" & .Rows.Count).End(xlUp).Offset(-Masterrow + 1)
Storage.Value2 = "=" & "Projects!" & Feeder.Address
End With
Is there a way to incorporate the formula =IFERROR(B2,0) so that my copy location contains =IFERROR(Projects!B2,0) as opposed to =Projects!B2?
I want erroneous cells to return a 0 as opposed to an error code so I can just run my delete rows code easily.
The fix was straightforward after realizing I had already constructed a formula before.
Code from before:
Storage.Value2 = "=" & "Projects!" & Feeder.Address
Code after:
Storage.Value2 = "=" & "IFERROR(" & "Projects!" & Feeder.Address & ",0)"
Sometimes it really is simple!
I am using the following code to insert a formula into a cell using vba.
The code inserts a hyperlink with some static text leading to a file path and then at the end of my file path I want to be able to add a dynamic cell reference, for instance A and then the number of the row.
In my cell in column A I have the names of folders. I am using DestRow to define the current row number. So my question is how can I correct my formula so that when the link is clicked it opens the link to get the correct folder name of the row clicked? Thanks
ws2.Range("S" & DestRow).Formula = "=HYPERLINK(""\\UKSH000-FILE06\Purchasing\New_Supplier_Set_Ups_&_Audits\ATTACHMENTS\"" & K" & DestRow & ",""Attached"")"
Try,
ws2.Range("S" & DestRow).Formula = "=HYPERLINK(""\\UKSH000-FILE06\Purchasing\New_Supplier_Set_Ups_&_Audits\ATTACHMENTS\" & ws2.Range("K" & DestRow).Value & """,""Attached"")"
FWIW, I hate working with quoted strings as well.
Addendum: This should do for adding a static filename after the dynamic folder:
ws2.Range("S" & DestRow).Formula = "=HYPERLINK(""\\UKSH000-FILE06\Purchasing\New_Supplier_Set_Ups_&_Audits\ATTACHMENTS\" & ws2.Range("K" & DestRow).Value & "\audit.xls"",""Attached"")"
You could try including the INDIRECT() function:
ws2.Range("S" & DestRow).Formula = "=HYPERLINK(""\\UKSH000-FILE06\Purchasing\New_Supplier_Set_Ups_&_Audits\ATTACHMENTS\"" & INDIRECT(""K""&" & DestRow & ",""Attached"")"