I'm trying to generate a list of hyperlinks in excel and link them dynamically to another cell in a different sheet. Could someone explain how I need to format the reference? Currently, it looks like this:
'p2r is one sheet
'sh is another
'Subaddress is currently linking to the correct location, wrong sheet.
'(It's linking to [p2r]'s cells, not [sh]'s cells)
p2r.Hyperlinks.Add_
Anchor:=p2r.Cells(p2rIndex, 1), _
Address:="", _
SubAddress:=sh.Cells(round2, 2).Address, _
TextToDisplay:=PrevRow
Thanks for the help! I've seen others use a format like: Sheet!A1, but when I tried something like:
SubAddress:=sh & "!" & Cells(round2, 2).Address
I got no results of value.
There are two modifications you could use to get your code working:
You could use
SubAddress:="'" & Replace(sh.Name, "'", "''") & "'!" & Cells(round2, 2).Address
in order to generate something of the form SubAddress:="'Sheet name'!$A$1".
(The bits in the formula dealing with 's are to allow it to still work if there are spaces, etc, in the sheet name. If you know that won't occur, e.g. because you defined the sheet names and no-one else will be changing them, you could simplify it to SubAddress:=sh.Name & "!" & Cells(round2, 2).Address)
You could use
SubAddress:=sh.Cells(round2, 2).Address(External:=True)
which would generate something of the form SubAddress:="'[Book1.xlsx]Sheet name'!$A$1"
Related
So what I would like to do is add a sum formula to a cell, so it can be edited later(normally, not through vba). It sums up some cells, but the amount of cells is not always the same. Sometimes it's 4 cells, sometimes it's 10. So I'm trying to have:
lastrow = Sheets(7).Cells(Sheets(7).Rows.Count, "A").End(xlUp).Row
then using it:
Sheets(7).Range("B" & lastrow + 1).Formula = "=SUM(b2:b&"lastrow")"
My problem is the syntax actually, I can't seem to make it right. How to add lastrow to this formula?
Hope it's understandable, English is not my native language.
You're missing to concatenate.
Sheets(7).Range("B" & lastrow + 1).Formula = "=SUM(b2:b" & lastrow & ")"
Instead of using the .End(xlUp) method, I'd suggest that using Named Ranges and in particular the built-in structured referencing of Excel Tables/ListObjects would make for more robust and simpler code.
Using Named Ranges avoids hard-coding references in your code. If you hard code cell address into your code, those references will be pointing at the wrong place if you (or a user) later adds new rows/columns above/to the left of those hard-coded references. Using Names avoids this, and makes for more robust code.
I almost always use Excel Tables aka ListObjects to hold any data that VBA interacts with for the same reason...ListObjects are dynamic named ranges that Excel automatically expands/contracts to suit the data, and unlike .End(xlUp) they won't throw your code off if the column contains blanks.
So I'd do it like this:
Range("SomeNamedRange") = "=SUM(" & Range("SomeTable[SomeColumn]").Address & ")"
...or more likely, I'd use the [] shorthand notation:
[SomeNamedRange] = "=SUM(" & [SomeTable[SomeColumn]].Address & ")"
I have a folder on the desktop
C:\Users\ME\Desktop\folder1\folder2\
In workbook 1 have column A which was used to create folders within folder 2.
Column N was then used to create filenames for each newly created workbook.
Every Row became a new workbook. (~3200 Workbooks, each in their own folder)
How can I either write a VBA or a Formula to quickly create the hyperlinks.
The reasoning is that the Master workbook will be used to narrow down the the search results and give immediate access to that location of the database.
the path I have is,
C:\Users\ME\Desktop\folder1\folder2\ & Column A & "/" & Column N & ".xlsm"
I tried using a formula
=HYPERLINK('C':\Users\ME\Desktop\folder1\folder2\ & [#SITE] & "/" & [#FULLNAME] & ".xlsm",[#FULLNAME])
This doesn't seam to work, the Cells I'm referencing is stated as the column headers. When I hit enter the entire column downward was then filled with "#NAME?" and a error stating "the formula contains unrecognized text"
Flip "/" into a "\"
Also look at this example for guidance:
=HYPERLINK(C10&"\"&A10&"\"&B10&".xlsx")
Perhaps use formula like this in New column and use it for the whole range:
=HYPERLINK("C:\Users\ME\Desktop\folder1\folder2\ & $A2 & "/" & $N2 & ".xlsm","PathToFile_" & $N2)
I've tried a variety of concats and uses of "" but I cant get .formula to work in the following code. The macro runs without error but it does not populate my sheet. I feel like this: Excel VBA formula with variables is what I am going for but main_row does not appear to be getting assigned to the variable when it is inside the .formula.
main_row = main.Range("b6").Row
calc_col = main.Range("k6").Column
main.Cells(main_row, calc_col).Formula = " = j & main_row / i & main_row "
Is it possible to use .formula with cells object?
Would rather not use application.worksheetfunction because i want end user to see the formula
Anyone have a good source to explain the proper way to qualify variables within .formula?
Try the below, I believe you were nearly there, but you inserting an unnecessary space before the equals sign + the quotes were out.
main.Cells(main_row, calc_col).Formula = "= J" & main_row & " / I" & main_row
I'm having trouble with the usage of Indirect function.
Here's what i'm looking for, I'm trying to create a dynamic vlookup based on the current tab.
=VLOOKUP(B3;'NH BBC'!$E$1:$Z$188;MATCH("Share Outstanding";'NH BBC'!$E$1:$Z$1;0);0)
My plan is to modify the 'NH BBC' by 'NH ' & RIGHT(CELL("filename");3) Supposing that the name of my tab is XXX_BBC.
I've tried to use indirect function but I'm not sure I'm on the good way.
Here's what I've tried:
=VLOOKUP(B3;INDIRECT("'" "NH " & "RIGHT(CELL("'" & "filename" & "'" & ");3)" & "!" & "E1:Z188");MATCH("Share Outstanding";'NH BBC'!$E$1:$Z$1;0);0)
Hope I've been clear.
Thanks in advance !
You are trying to concatenate some text with the results returned from a formula, but you are sticking the formulas in quotes, turning them into text. Furthermore, you are not keeping very good track of your text. There are quotes all over the place. Take this bit by bit in a seperate cell if need, slowly growing your formula from the inside out so you can insure everything is as expected. Right now it's a mess.
INDIRECT("'" "NH " & "RIGHT(CELL("'" & "filename" & "'" & ");3)" & "!" & "E1:Z188")
Should be:
INDIRECT("'NH " & RIGHT(CELL("filename");3) & "'!E1:Z188")
No need for all the complication.
I've finally found and this formula is working perfectly.
VLOOKUP($B3;INDIRECT("'NH "&RIGHT(CELL("filename");3)&"'!$G$1:$ZZ$9999");MATCH("SHARE_OUTSTANDING";INDIRECT("'NH "&RIGHT(CELL("filename");3)&"'!$G$1:$ZZ$1");0))
By the way the issue i've got is that the cell are changing when i'm using the formula in another tab. Is this possible to look the value i've obtained ?
Something like a F9 ?
I'm trying to get this code to work for summing cells:
Worksheets("Sheet2").Range("C3").Offset(i, j).Formula = "=Sum("
&Worksheets("Sheet1").Range("A3").Offset(2*i,j).Address & ":" &
Worksheets("Sheet1").Range("A7").Offset(2*i,j).Address & ")"
It keeps giving me the right cells but from the wrong sheet. So for the first iteration I get sum(A3:A7) in cell C3 of Sheet2 but the A3:A7 stays referenced to Sheet2 not Sheet1.
Thanks!
You need to specify the name of the sheet in the formula too. Your code will work if you write it like this:
Worksheets("Sheet2").Range("C3").Offset(i, j).Formula = "=Sum(Sheet1!" & _
Worksheets("Sheet1").Range("A3").Offset(2 * i, j).Address & ":" & _
Worksheets("Sheet1").Range("A7").Offset(2 * i, j).Address & ")"
Try this code - it uses the External:=True parameter of .Addressto retrieve the full address. While this also includes the workbook name, Excel will remove this automatically so you end up with Sheet1!A3. Also note that I used the range A3:A7 as source as .Address can handle multi-cell ranges and you don't need to take care of it manually:
Sheets("Sheet2").Range("C3").Offset(i, j).Formula = "=SUM(" & _
Sheets("Sheet1").Range("A3:A7").Offset(2 * i, j).Address(External:=True) & ")"
Be aware that hard coding references such as A3 can lead to bugs in the long run, as the user (or even the developer at some stage) might modify the sheet structure. It is best practice to use named ranges, i.e. create a named range for each cell/range you refer to and then access it in VBA with SheetX.Range("rngStartCell") or similar!