Formula Excel find a Sheet by his index - excel-formula

Hy everybody.
I'm trying to control the value of some cells through a reference page …
'On "TRAITEMENT" sheet
=NB.SI('refSheet'!A:A;'MyFirstPage'!I3)
So far so good.
However the name of the page which contains the cell to be checked ('Myfistpage') is not sure. It can change his name and some new sheet can appear between my pages.
I am sure, however, that my maximum page allowed is 15 and that they must all be processed.
I would therefore like to do a treatment for each page according to their index.
I try this code:
=NB.SI('refSheet'!A:A;feuil1!I3)
But the answer is always 0 regardless of the control performed ...
At all, I had to find a method to select a pages without using his name.
Thank all in advance for your answers.

You can create a User Defined function in VBA:
Function SHEETNAME(ShNum As Long) As String
SHEETNAME = Sheets(ShNum).Name
End Function
Then you can use the above function anywhere in your Excel workbook
=NB.SI('refSheet'!A:A; INDIRECT(SHEETNAME(1)&"!I3"))

Related

Creating a Dynamic Range that works for Excel to Save Name Ranges to a Static Web Page

I have a excel document that has several name ranges that are currently save to static web pages. I have recently tried to conver them to dynamic ranges, and have learned the dynamic named ranges work great within the context of the excel sheet itself, but fail once excel tries to save them as the static web page.
For example, I have a range
YardTabletLists!$EC$1:$EE$101
sometimes the content is exceeds the bounds or I have empty rows on my webpage.
the dynamic alternative is
=OFFSET(YardTabletLists!$EC$1,0,0,COUNTA(YardTabletLists!$EC$1:$EC$10000)+1,3)
If i refernce this name range anywhere within the sheet, it works, but when AutoPublish does it thing, I get the following error.
Error Notice
I was thinking of trying to conver the Dynamic range back to a static range somehow, and then direct the name rage to that Cell.... i.e.
Name range is directed to =E4, and E4 contains YardTabletLists!$EC$1:$EE$101, but I get the feeling that will give me the same issue.
Thanks to those who read this.
Not familiar with static webpage feature.
But possible to define and use “table” instead of “named range”?
With “table” Excel internally handles the correct range whenever the table size is modified.
Seems the static webpage feature supports also “filtered ranges”. Could this be a work-around, e.g. to filter out blank lines (while using max line number as static range)?
Indirect references (your example with E4) are a little tricky in Excel. I only used it once to define a list for a dropdown.
Muss be supported by the feature and requires “special” syntax
Update:
I have found some examples for VBA updating name ranges, and one excellent example that appears to answer the question:
https://excelchamps.com/vba/named-range/#Resizing_a_Named_Range_using_VBA_Dynamic_Named_Range
Sub vba_named_range()
Dim iRow As Long
Dim iColumn As Long
iRow = ActiveSheet.Range("A1").End(xlDown).Row
iColumn = ActiveSheet.Range("A1").End(xlToRight).Column
ActiveSheet.Range("myRange") _
.Resize(iRow, iColumn).Name = "myRange"
End Sub
for some reason the program hangs up at
ActiveSheet.Range("myRange") _
.Resize(iRow, iColumn).Name = "myRange"
does anyone see perhaps a syntax error I am missing?
Thank you :)

My Excel workbook with custom formulas breaks when I use other workbooks simultaneously

I´m using an Excel workbook with a custom formula for taking a value from the previous worksheet. I use this formula like INDIRECT(SHEETNAME(SHEET(A1)-1)&"!A1"), so SHEET(A1) returns the current sheet number, and SHEETNAME(SHEET(A1)-1) returns the name of the previous sheet, then I use INDIRECT to take the value A1 from that previous sheet.
Here is the code for the custom sheetname formula:
Function SHEETNAME(number As Long) As String
SHEETNAME = Sheets(number).Name
End Function
The problem is that when I use other workbook at the same time, the mentioned command returns #VALUE!.
Thanks for the help! :)
You should always fully qualify.
So instead of Sheets(number).Name, try ThisWorkbook.Sheets(number).Name
Not doing so can lead to bugs that are difficult to diagnose.
I would always suggest avoiding "ActiveWorkbook" unless you specifically need it.

Issue using IF=() function in Excel 2010

I am new to this forum and I am seeking help with creating a function in Excel for data logging purposes. Basically I am trying to use Excel to write a macro simply by pressing "x" in certain cells. For instance, from cells A3-A6 I will have client's names listed there. In addition, I will have dates (of service) listed on the 2nd cell row and on.
9/20 9/21 9/22 9/24
John Doe x
Sara Mitchell x x
Christopher Acha x
Now, I have the macro, I have everything I need in order to set this up. The only problem I am having is to make Excel write the macro for me. I am trying to use the IF=() function, but it limits me to 255 characters, and it tells me to use the concatenate function. All I am trying to do is basically tell Excel that if a certain cell IS true, I would mark it by hitting x; meaning that if a person came on the 24th for service, I would hit x and from there excel would automatically write what I tell it to (in a different sheet or cell). I am stuck here, any help would be greatly appreciated.
You said you have a macro that works, so I'm assuming you want to know how to trigger it automatically when you enter data into a cell.
In your worksheet code use a change event and call your macro from here and pass the range you sent.
Private Sub Worksheet_Change(ByVal Target As Range)
myMacro(Target)
End Sub
You'll probably want to add in additional code to test that the cell changed is within a certain range before calling your macro.

Simple vba program in Excel

Sub TEST()
If cells(i, "R").Value <> "UK" Then
cells(i, "R").Interior.ColorIndex = 3
End If
End Sub
If I run this program it throws application defined error \
I am new to Excel (beginner)
How to correct this error!!!
Thanks In advance
I think the issue is "R" that I know of the cells method takes 2 parameters one is rows the other is columns (in that order) but this is done by number not letter so if you change it to cell(1,18) then the code above works fine.
This link may also be useful to learn more, among other things it describes how you would normally select a range first as I believe your code above will assume the currently selected page, however you might want to run in on a button click from another page or as soon as the spreadsheet opens.
http://msdn.microsoft.com/en-us/library/office/ff196273.aspx
The problem is that the variable i has not been assigned a value. VBA assumes that it is zero. Since i is used to determine the row of the cell, Excel throws an exception because there is no row 0!
First you have to define i variable
for example: Dim i as variant

Creating a custom hyperlink function in excel

I have searched far and wide, but can't find an answer to this simple question. I want to make a custom function in excel which will create a hyperlink.
Excel has a built in hyperlink function that works like this:
=Hyperlink(link_location, display_text)
I want to create a function called CustomHyperlink which takes one parameter, and returns a hyperlink to a google query with that parameter. Just for the sake of the question, lets assume that the passed parameter is a alphanumeric string, with no spaces.
Essentially, calling
=CustomHyperlink("excel")
should be the same as calling
=Hyperlink("http://www.google.com/search?q=excel", "excel")
This seems like such a simple task, but I absolutely cannot find a way to make this function.
Can anyone offer some quick help?
I can offer a partial solution, one that will update an existing hyperlink. This only makes sence if you are using it like, say
CustomHyperlink(A1)
were A1 contains the required serch term
To use,
enter your UDF formula in a cell, eg =CustomHyperlink(A1)
create a hyperlink on the cell (right click, Hyperlink...) . This can be any hyperlink, valid or invalid
put the required search term in the referenced cell, eg in A1 put excel
When the UDF runs it will update the hyperlink to Google the entered search term
Function CustomHyperlink(Term As String) As String
Dim rng As Range
Set rng = Application.Caller
CustomHyperlink = Term
If rng.Hyperlinks.Count > 0 Then
rng.Hyperlinks(1).Address = "http://www.google.com/search?q=" & Term
End If
End Function
In VBA editor you can use
ThisWorkbook.FollowHyperlink Address:=(strWebsite), NewWindow:=True
Which will take you to that specific website, and just build a function around that to navigate you to the site you need.
Nice idea although this isn't possible.
You seem to want to have the formula of the cell as one thing (your custom function call) and yet have the value as another (the hyperlink / URL) which simply isn't possible.
The correct way through VBA to add a hyperlink is to use the Hyperlinks property but it is not possible to call this property, through a Worksheet UDF (because of the reason above).
What is wrong with just using the the built-in =Hyperlink() worksheet function? You could effectively parameterise your URL as follows (where cell A1 = Excel):
=HYPERLINK("http://www.google.com/search?q="&A1)
You can't do this directly for the reasons creamyegg suggests, but there is a way to achieve the functionality albeit with a bit of a performance consideration.
You could use the Worksheet_Change event to track for the presence of your UDF then process the hyperlink addition there.
You would need to set up an empty function to allow this to happen, otherwise Excel will throw an error whenever you entered =CustomHyperlink... in a cell.
The below should work, not really had time to test.
Private Sub worksheet_change(ByVal target As Range)
Dim SearchValue As String
If LCase(Left(target.Formula, 16)) = "=customhyperlink" Then
SearchValue = Mid(target.Formula, 19, Len(target.Formula) - 20)
target.Value = SearchValue
target.Hyperlinks.Add target, "http://www.google.com/search?q=" & SearchValue, , "Search Google for " & SearchValue, SearchValue
End If
End Sub
The performance consideration is of course the volatile Worksheet_Change event as this can really kill large, complex workbooks.

Resources