Sharepoint link to Excel via a cell variable
Solved/Closed
Dave122
Posts
4
Registration date
Monday July 11, 2011
Status
Member
Last seen
July 12, 2011
-
Jul 11, 2011 at 01:56 AM
Dave122 - Jul 29, 2011 at 07:10 AM
Dave122 - Jul 29, 2011 at 07:10 AM
Related:
- Excel indirect sharepoint link
- Gamevault download link - Download - Online gaming and betting
- Orion stars download link - Download - Online gaming and betting
- Twitter link opener - Guide
- Excel marksheet - Guide
- Excel free download - Download - Spreadsheets
10 responses
Dave122
Posts
4
Registration date
Monday July 11, 2011
Status
Member
Last seen
July 12, 2011
5
Jul 12, 2011 at 01:40 AM
Jul 12, 2011 at 01:40 AM
Thank you for your response, however you can definitely get a value from a remote file(e.g. Sharepoint). The problem I have is to complete part of the path via a variable, thus:-
Works:
='http://teamsite.company.com/corporate/[MySpreadsheet.xlsx]Sheet1'!F4
What I am attempting, is to have the word corporate in the above filled in by a value from another cell. So I could have 'Corporate' or 'UK' to match the different folders in Sharepoint.
Thanks for any assistance you can provide, as this is one of those things that should be so easy, but driving me nuts!!
Cheers
Works:
='http://teamsite.company.com/corporate/[MySpreadsheet.xlsx]Sheet1'!F4
What I am attempting, is to have the word corporate in the above filled in by a value from another cell. So I could have 'Corporate' or 'UK' to match the different folders in Sharepoint.
Thanks for any assistance you can provide, as this is one of those things that should be so easy, but driving me nuts!!
Cheers
rizvisa1
Posts
4478
Registration date
Thursday January 28, 2010
Status
Contributor
Last seen
May 5, 2022
766
Jul 11, 2011 at 03:38 PM
Jul 11, 2011 at 03:38 PM
I am not sure that you can get value for a file via hyperlink. My understanding is that file need to a location that is mapped as a drive.
I tried this on my setup, but didn't work, so I got suspicious to understand what was differnet between out setups.
Now I think I know why. In the example I gave, (naturally I couldn't give the real paths!), the folder names didn't have spaces. But the real one I am worknig with has spaces both in the folder and in the spreadsheet itself.
So if you try your example with say the following (which better represents my real scenario), it unfortunately fails (coming back with #Value!):-
sInstring = "'http://teamsite.company.com/corporate/next level/[My New Spreadsheet.xlsx]Sheet1'!F4 "
Sorry for the bad example I provided originally.
So do you think there is a way round the spaces issue ?
Thanks
Dave
Now I think I know why. In the example I gave, (naturally I couldn't give the real paths!), the folder names didn't have spaces. But the real one I am worknig with has spaces both in the folder and in the spreadsheet itself.
So if you try your example with say the following (which better represents my real scenario), it unfortunately fails (coming back with #Value!):-
sInstring = "'http://teamsite.company.com/corporate/next level/[My New Spreadsheet.xlsx]Sheet1'!F4 "
Sorry for the bad example I provided originally.
So do you think there is a way round the spaces issue ?
Thanks
Dave
rizvisa1
Posts
4478
Registration date
Thursday January 28, 2010
Status
Contributor
Last seen
May 5, 2022
766
Jul 15, 2011 at 05:44 AM
Jul 15, 2011 at 05:44 AM
Well actually the test link that used had space. This is what I did. I uploaded a test file to sharepoint. Then copied the link by removing all %20 (space) characters and made it a formula call to see if it works within my setup. Surprisingly it did return me the value. So then it took that string and simple transplanted in my routine and had it write the formula to the cell (may be you need to say cell(rows.count, columns.count).formula = sinstring ?)
Once that was successful, then I replaced one of the dir in the full path as a variable.
Looking at your example only difference between your and mine is that my file name itself did not had space. May be that was the issue. I have to see at work
Try to copy my steps. May be some thing will jump out. One thing that I did not pay attention then was when I initially put the formula in the cell I saw a #REF error for a sec and then it got replaced by actual value. May be there is some time delay (based on network call etc)
Once that was successful, then I replaced one of the dir in the full path as a variable.
Looking at your example only difference between your and mine is that my file name itself did not had space. May be that was the issue. I have to see at work
Try to copy my steps. May be some thing will jump out. One thing that I did not pay attention then was when I initially put the formula in the cell I saw a #REF error for a sec and then it got replaced by actual value. May be there is some time delay (based on network call etc)
rizvisa1
Posts
4478
Registration date
Thursday January 28, 2010
Status
Contributor
Last seen
May 5, 2022
766
Jul 15, 2011 at 09:07 AM
Jul 15, 2011 at 09:07 AM
Ok I just tried with space in the name and I got the value back
#value says to me that it is able to get to the file but some how there is other issue with data
#value says to me that it is able to get to the file but some how there is other issue with data
Hmmm, now I'm really confused. I think I must be doing something really dumb....!
I've started afresh, created a new spreadsheet, and in Module1 used the following code (basically from your example earlier). So as you'll see just using the raw link, not even sDir at this stage. (pathwise, I've only changed the company name, nothing else). :-
Function getValue()
Dim sInstring As String
Dim sOutAnswer As String
Dim sDir As String
'sDir = "corporate"
sInstring = "='http://teamsites.corp.xxxxplanet.com/corporate/ICT/ICT Team Documents/Reports/[Breakdown of Users Int and Ext 090308.xls]Summary'!$E$5"
sOutAnswer = "A Dummy Value"
Cells(Rows.Count, Columns.Count) = sInstring
sOutAnswer = Cells(Rows.Count, Columns.Count)
Cells(Rows.Count, Columns.Count) = vbNullString
If (sOutAnswer = "A Dummy Value") _
Then
MsgBox "Back to drawing board"
Else
MsgBox sOutAnswer
End If
End Function
Now in one cell (F16), I've entered the following which gives the correct result (which should be All Users in Oracle):-
='http://teamsites.corp.xxxxplanet.com/corporate/ICT/ICT Team Documents/Reports/[Breakdown of Users Int and Ext 090308.xls]Summary'!$E$5
In Cell F17, I've entered the formula, =getValue() to which I get #VALUE!
Can you see where I've cocked up?!!!!! I feel I'm really gonna kick myself when we find out what it is!
Thanks for your patience on this!
Dave
I've started afresh, created a new spreadsheet, and in Module1 used the following code (basically from your example earlier). So as you'll see just using the raw link, not even sDir at this stage. (pathwise, I've only changed the company name, nothing else). :-
Function getValue()
Dim sInstring As String
Dim sOutAnswer As String
Dim sDir As String
'sDir = "corporate"
sInstring = "='http://teamsites.corp.xxxxplanet.com/corporate/ICT/ICT Team Documents/Reports/[Breakdown of Users Int and Ext 090308.xls]Summary'!$E$5"
sOutAnswer = "A Dummy Value"
Cells(Rows.Count, Columns.Count) = sInstring
sOutAnswer = Cells(Rows.Count, Columns.Count)
Cells(Rows.Count, Columns.Count) = vbNullString
If (sOutAnswer = "A Dummy Value") _
Then
MsgBox "Back to drawing board"
Else
MsgBox sOutAnswer
End If
End Function
Now in one cell (F16), I've entered the following which gives the correct result (which should be All Users in Oracle):-
='http://teamsites.corp.xxxxplanet.com/corporate/ICT/ICT Team Documents/Reports/[Breakdown of Users Int and Ext 090308.xls]Summary'!$E$5
In Cell F17, I've entered the formula, =getValue() to which I get #VALUE!
Can you see where I've cocked up?!!!!! I feel I'm really gonna kick myself when we find out what it is!
Thanks for your patience on this!
Dave
Didn't find the answer you are looking for?
Ask a question
Okay, I must be doing something really stupid!
I started a fresh spreadsheet, created a Module1, of which I put the following code (basically your example earlier):-
Function getValue()
Dim sInstring As String
Dim sOutAnswer As String
Dim sDir As String
'sDir = "corporate"
sInstring = "='http://teamsites.corp.xxxplanet.com/corporate/ICT/ICT Team Documents/Reports/[Breakdown of Users Int and Ext 090308.xls]Summary'!$E$5"
sOutAnswer = "A Dummy Value"
Cells(Rows.Count, Columns.Count) = sInstring
sOutAnswer = Cells(Rows.Count, Columns.Count)
Cells(Rows.Count, Columns.Count) = vbNullString
If (sOutAnswer = "A Dummy Value") _
Then
MsgBox "Back to drawing board"
Else
MsgBox sOutAnswer
End If
End Function
As you can see, I've put the entire link in (even commented out sDir), I have only changed the company name, nothing else.
In cell F16, I put the following formula which provides the correct result (All users in Oracle):-
='http://teamsites.corp.xxxplanet.com/corporate/ICT/ICT Team Documents/Reports/[Breakdown of Users Int and Ext 090308.xls]Summary'!$E$5
In cell F17, I entered =getValue() which produces #VALUE!
I can't see the error???
I feel this is a dumb mistake I am making here, but can't see it.
Thanks for your patience on this one!
Dave
I started a fresh spreadsheet, created a Module1, of which I put the following code (basically your example earlier):-
Function getValue()
Dim sInstring As String
Dim sOutAnswer As String
Dim sDir As String
'sDir = "corporate"
sInstring = "='http://teamsites.corp.xxxplanet.com/corporate/ICT/ICT Team Documents/Reports/[Breakdown of Users Int and Ext 090308.xls]Summary'!$E$5"
sOutAnswer = "A Dummy Value"
Cells(Rows.Count, Columns.Count) = sInstring
sOutAnswer = Cells(Rows.Count, Columns.Count)
Cells(Rows.Count, Columns.Count) = vbNullString
If (sOutAnswer = "A Dummy Value") _
Then
MsgBox "Back to drawing board"
Else
MsgBox sOutAnswer
End If
End Function
As you can see, I've put the entire link in (even commented out sDir), I have only changed the company name, nothing else.
In cell F16, I put the following formula which provides the correct result (All users in Oracle):-
='http://teamsites.corp.xxxplanet.com/corporate/ICT/ICT Team Documents/Reports/[Breakdown of Users Int and Ext 090308.xls]Summary'!$E$5
In cell F17, I entered =getValue() which produces #VALUE!
I can't see the error???
I feel this is a dumb mistake I am making here, but can't see it.
Thanks for your patience on this one!
Dave
rizvisa1
Posts
4478
Registration date
Thursday January 28, 2010
Status
Contributor
Last seen
May 5, 2022
766
Jul 15, 2011 at 11:20 AM
Jul 15, 2011 at 11:20 AM
If you notice I am writing the formula to a cell and reading the result back. Now this is all good when it is a stand alone call. However when it is a function that is not possible. Here is one possible way
If you have less than 7 possible dirs, one easy way might be to use an if statement
some thing like this
=IF(B3="Corporate",'http://teamsites.corp.xxxplanet.com/corporate/ICT/ICT Team Documents/Reports/[Breakdown of Users Int and Ext 090308.xls]Summary'!$E$5,IF(B3="Local",'http://teamsites.corp.xxxplanet.com/local/ICT/ICT Team Documents/Reports/[Breakdown of Users Int and Ext 090308.xls]Summary'!$E$5,""))
If you have more than 7, then you keep the function but then you have to use Worksheet_Change event. Here you would call that function. The challenge in that would be to know when to call the function and when not to call the function. Since I dont know this it is hard to tell, But there is a gist of it. In the sample code, If I change some thing in range (a2:a10) in correspoding cell I am putting the result
If you have less than 7 possible dirs, one easy way might be to use an if statement
some thing like this
=IF(B3="Corporate",'http://teamsites.corp.xxxplanet.com/corporate/ICT/ICT Team Documents/Reports/[Breakdown of Users Int and Ext 090308.xls]Summary'!$E$5,IF(B3="Local",'http://teamsites.corp.xxxplanet.com/local/ICT/ICT Team Documents/Reports/[Breakdown of Users Int and Ext 090308.xls]Summary'!$E$5,""))
If you have more than 7, then you keep the function but then you have to use Worksheet_Change event. Here you would call that function. The challenge in that would be to know when to call the function and when not to call the function. Since I dont know this it is hard to tell, But there is a gist of it. In the sample code, If I change some thing in range (a2:a10) in correspoding cell I am putting the result
Private Sub Worksheet_Change(ByVal Target As Range) Dim vValue As Variant Dim cell As Range If (Union(Target, Range("A2:A10")).Address <> Range("A2:A10").Address) Then Exit Sub Application.EnableEvents = False For Each cell In Target If cell.Value = vbNullString _ Then vValue = vbNullString Else vValue = getValue(cell.Value) End If cell.Offset(0, 1) = vValue Next Application.EnableEvents = True End Sub Public Function getValue(sDir As String) As Variant Dim sInstring As String Dim sOutAnswer As String If (Trim(sDir) <> vbNullString) Then sDir = sDir & "/" sInstring = "='http://teamsites.corp.xxxplanet.com/" & sDir & "ICT/ICT Team Documents/Reports/[Breakdown of Users Int and Ext 090308.xls]Summary'!$E$5" sOutAnswer = "Unable to Read remote file" On Error Resume Next Cells(Rows.Count, Columns.Count).Formula = sInstring sOutAnswer = Cells(Rows.Count, Columns.Count) Cells(Rows.Count, Columns.Count) = vbNullString Err.Clear On Error GoTo 0 getValue = sOutAnswer End Function
YOU ARE A STAR!!! Still not quite sure why Getvalue doesn't work direct, but this is a great work around. Did some tweaking, so I could also select the cells I want from the remote file via another part of the spreadsheet (in this case the first row). I made A1, a count of the number of cells filled in row 1, and did a loop, thus:-
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
Dim vValue As Variant
Dim cell As Range
Dim nextCell As Variant
If Target.Column = 1 Then
Application.EnableEvents = False
For Each cell In Target
For nextCell = 1 To Range("A1").Value
vValue = getValue(cell.Value, Range("A1").Offset(0, nextCell).Value)
cell.Offset(0, nextCell) = vValue
Next nextCell
Next
Application.EnableEvents = True
End If
End Sub
Public Function getValue(sDir As String, cellLocation As String) As Variant
Dim sInstring As String
Dim sOutAnswer As String
If (Trim(sDir) <> vbNullString) Then sDir = sDir & "/"
sInstring = "='http://teamsites.corp.xxxplanet.com/" & sDir & _
"ICT/ICT Team Documents/Reports/[Breakdown of Users Int and Ext 090308.xls]Summary'!" & cellLocation
sOutAnswer = ""
On Error Resume Next
Cells(Rows.Count, Columns.Count).Formula = sInstring
sOutAnswer = Cells(Rows.Count, Columns.Count)
Cells(Rows.Count, Columns.Count) = vbNullString
Err.Clear
On Error GoTo 0
getValue = sOutAnswer
End Function
Works brilliantly. Thanks very much. Seriously appreciate your efforts in what I consider to be a strange ommission from Microsoft.
If you're ever in Aberdeen, Scotland, I owe you a few beers!!
Cheers
Dave
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
Dim vValue As Variant
Dim cell As Range
Dim nextCell As Variant
If Target.Column = 1 Then
Application.EnableEvents = False
For Each cell In Target
For nextCell = 1 To Range("A1").Value
vValue = getValue(cell.Value, Range("A1").Offset(0, nextCell).Value)
cell.Offset(0, nextCell) = vValue
Next nextCell
Next
Application.EnableEvents = True
End If
End Sub
Public Function getValue(sDir As String, cellLocation As String) As Variant
Dim sInstring As String
Dim sOutAnswer As String
If (Trim(sDir) <> vbNullString) Then sDir = sDir & "/"
sInstring = "='http://teamsites.corp.xxxplanet.com/" & sDir & _
"ICT/ICT Team Documents/Reports/[Breakdown of Users Int and Ext 090308.xls]Summary'!" & cellLocation
sOutAnswer = ""
On Error Resume Next
Cells(Rows.Count, Columns.Count).Formula = sInstring
sOutAnswer = Cells(Rows.Count, Columns.Count)
Cells(Rows.Count, Columns.Count) = vbNullString
Err.Clear
On Error GoTo 0
getValue = sOutAnswer
End Function
Works brilliantly. Thanks very much. Seriously appreciate your efforts in what I consider to be a strange ommission from Microsoft.
If you're ever in Aberdeen, Scotland, I owe you a few beers!!
Cheers
Dave
Okay, tried to develop this a bit more by trying to get a vlookup performed within the relevant remote file. What I've done is to put certain lookup values in row 2, and paasing that value to GetValue. But even though the values are passing, the vlookup isn't working. Hopefully a much easier problem, haven't used vlookup in VBA before.
Alternatively, storing the lookup range in an array, and pull out the values as required, rather than re-reading the file each time?
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
Dim vValue As Variant
Dim vOldValue As Variant
Dim cell As Range
Dim vNextCell As Variant
Dim cTextSearch As String
If (Union(Target, Range("A2:A500")).Address <> Range("A2:A500").Address) Then Exit Sub
Application.EnableEvents = False
For Each cell In Target
For vNextCell = 1 To Range("A1").Value
vOldValue = cell.Offset(0, vNextCell).Value
cTextSearch = Range("B2").Offset(0, vNextCell).Value
vValue = getValue(cell.Value, cTextSearch)
cell.Offset(0, vNextCell) = IIf(vValue = "", vOldValue, vValue)
Next vNextCell
Next
Application.EnableEvents = True
End Sub
Public Function getValue(sDir As String, sCellText As String) As Variant
Dim sInstring As String
Dim sOutAnswer As String
On Error Resume Next
sInstring = "'http://teamsites.corp.xxxplanet.com/corporate/ICT/ICT Team Documents/Reports/[Breakdown of Users Int and Ext 090308.xls]Summary'!$E$5:$F$10"
Cells(Rows.Count, Columns.Count).Formula = Application.VLookup(sCellText, sInstring, 2, 0)
sOutAnswer = Cells(Rows.Count, Columns.Count)
Cells(Rows.Count, Columns.Count) = vbNullString
Err.Clear
On Error GoTo 0
getValue = sOutAnswer
End Function
Thoughts,
Thanks (yet) again !!!
Alternatively, storing the lookup range in an array, and pull out the values as required, rather than re-reading the file each time?
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
Dim vValue As Variant
Dim vOldValue As Variant
Dim cell As Range
Dim vNextCell As Variant
Dim cTextSearch As String
If (Union(Target, Range("A2:A500")).Address <> Range("A2:A500").Address) Then Exit Sub
Application.EnableEvents = False
For Each cell In Target
For vNextCell = 1 To Range("A1").Value
vOldValue = cell.Offset(0, vNextCell).Value
cTextSearch = Range("B2").Offset(0, vNextCell).Value
vValue = getValue(cell.Value, cTextSearch)
cell.Offset(0, vNextCell) = IIf(vValue = "", vOldValue, vValue)
Next vNextCell
Next
Application.EnableEvents = True
End Sub
Public Function getValue(sDir As String, sCellText As String) As Variant
Dim sInstring As String
Dim sOutAnswer As String
On Error Resume Next
sInstring = "'http://teamsites.corp.xxxplanet.com/corporate/ICT/ICT Team Documents/Reports/[Breakdown of Users Int and Ext 090308.xls]Summary'!$E$5:$F$10"
Cells(Rows.Count, Columns.Count).Formula = Application.VLookup(sCellText, sInstring, 2, 0)
sOutAnswer = Cells(Rows.Count, Columns.Count)
Cells(Rows.Count, Columns.Count) = vbNullString
Err.Clear
On Error GoTo 0
getValue = sOutAnswer
End Function
Thoughts,
Thanks (yet) again !!!
rizvisa1
Posts
4478
Registration date
Thursday January 28, 2010
Status
Contributor
Last seen
May 5, 2022
766
Jul 18, 2011 at 01:06 PM
Jul 18, 2011 at 01:06 PM
Can one do a VLOOKUP on a remote file ?. I tried to do that but I get error. So either it is not possible or I am missing some thing. Have you tried a standalone vlookup on a remote file ? if yes could you paste a sample call.
Sure can. Here's the example I used to know it's possible:-
=VLOOKUP(L2,'http://teamsites.corp.xxxplanet.com/corporate/ICT/ICT Team Documents/Reports/[Breakdown of Users Int and Ext 090308.xls]Summary'!$E$5:$F$10,2,0)
I just need to do the same thing via VBA, bearing in mind the code further up this trail...
Cheers
=VLOOKUP(L2,'http://teamsites.corp.xxxplanet.com/corporate/ICT/ICT Team Documents/Reports/[Breakdown of Users Int and Ext 090308.xls]Summary'!$E$5:$F$10,2,0)
I just need to do the same thing via VBA, bearing in mind the code further up this trail...
Cheers
rizvisa1
Posts
4478
Registration date
Thursday January 28, 2010
Status
Contributor
Last seen
May 5, 2022
766
Jul 19, 2011 at 06:50 AM
Jul 19, 2011 at 06:50 AM
For some reason it was not working for me. I will try on some other PC at work
Just looking at the code, I have doubts about this line
Cells(Rows.Count, Columns.Count).Formula = Application.VLookup(sCellText, sInstring, 2, 0)
Try some thing like this
Cells(Rows.Count, Columns.Count).Formula = "=VLookup(" & sCellText & "," & sInstring & ",2, 0)"
Just looking at the code, I have doubts about this line
Cells(Rows.Count, Columns.Count).Formula = Application.VLookup(sCellText, sInstring, 2, 0)
Try some thing like this
Cells(Rows.Count, Columns.Count).Formula = "=VLookup(" & sCellText & "," & sInstring & ",2, 0)"
Okay, I see how you've done that now! I had to make a slight tweak as for some reason, the quotes around sCellText were left off. Modified thus:-
Cells(Rows.Count, Columns.Count).Formula = "=VLookup(""" & sCellText & """," & sInstring & ",2, 0)"
Wondering if the application.vlookup would have been slightly faster(trying to reduce any overhead), but this works fine so many thanks again. As I have to do various lookups in the said file, would loading the range into an array be much faster? Unfortunately, I would then be massively out of my depth!
Anyway, thanks for all your help.
Dave
Cells(Rows.Count, Columns.Count).Formula = "=VLookup(""" & sCellText & """," & sInstring & ",2, 0)"
Wondering if the application.vlookup would have been slightly faster(trying to reduce any overhead), but this works fine so many thanks again. As I have to do various lookups in the said file, would loading the range into an array be much faster? Unfortunately, I would then be massively out of my depth!
Anyway, thanks for all your help.
Dave
rizvisa1
Posts
4478
Registration date
Thursday January 28, 2010
Status
Contributor
Last seen
May 5, 2022
766
Jul 19, 2011 at 08:40 AM
Jul 19, 2011 at 08:40 AM
MY bad Dave, I never tested it. You did need those extra quotes. Makes perfect sense. About performance part, my first preference would be in this case to use some thing like this (there might be typos etc as I have not tested it)
On error resume next
X = Application.VLookup(""" & sCellText & """," & sInstring & ",2, 0)"
on error goto 0
This would have worked faster, (if is possible)
Every time you select, read or write a value to/from a cell/sheet/range, it takes a bit of time. If the object is in memory, then it is faster. In your case, I am thinking it would be even slower as we have additional layer of intranet traffic.
On error resume next
X = Application.VLookup(""" & sCellText & """," & sInstring & ",2, 0)"
on error goto 0
This would have worked faster, (if is possible)
Every time you select, read or write a value to/from a cell/sheet/range, it takes a bit of time. If the object is in memory, then it is faster. In your case, I am thinking it would be even slower as we have additional layer of intranet traffic.
Hmmm, couldn't get this to quite work. Tried removing the quote at the end (you had an uneven number), and various other combinations of quotes and no quotes, but no success.
Even tried it direct with the http reference directly, i.e.:
X = Application.VLookup(sCellText, "'http://teamsites.corp.xxxplanet.com/corporate/ICT/ICT Team Documents/Reports/[Breakdown of Users Int and Ext 090308.xls]Summary'!$E$5:$F$10",2, 0)
Again no success. Have to put the quotes round, as it doesn't like the apostrophe on its' own. Still quite fast, but as you say this would use memory rather than cell references, which would improve things further.
Cheers
Even tried it direct with the http reference directly, i.e.:
X = Application.VLookup(sCellText, "'http://teamsites.corp.xxxplanet.com/corporate/ICT/ICT Team Documents/Reports/[Breakdown of Users Int and Ext 090308.xls]Summary'!$E$5:$F$10",2, 0)
Again no success. Have to put the quotes round, as it doesn't like the apostrophe on its' own. Still quite fast, but as you say this would use memory rather than cell references, which would improve things further.
Cheers
rizvisa1
Posts
4478
Registration date
Thursday January 28, 2010
Status
Contributor
Last seen
May 5, 2022
766
Jul 22, 2011 at 05:28 PM
Jul 22, 2011 at 05:28 PM
Sorry Dave, I did not had a moment to spare to even look at this at work. Hopefully monday will be better.
Not a problem. In the interim, I got a little carried away with it over the past few days (things are slow here at the moment!). Have altered the code, so you can tell the code whether to do a horizontal or vertical lookup (row 2 of the spreadsheet starting at 'B'), and the range to lookup(row 1, starting at 'B'). Also have the path in A1. In A2, have the number of lookups it needs to cycle through using a basic Counta), and A3 is the row where the lookup phrases are(just to give the users a bit of flexibility if they want to add headers, etc). However(there's always a catch!), there is just one problem left. When you first open up the spreadsheet, it ignores the first entry you make in column 'A' ????!!! You have to enter it a second time for it to work. I think our original code was doing the same. I suspect error handling, but just can't quite figure it out.
The code is:-
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
Dim cell As Range ' Cells that have changed in 'A' Column
Dim vNextCell As Variant ' Counter to sweep along Phrases
Dim sSearch As String ' Phrase to search for
Dim sDataFile As String ' Remote Excel file to search, INCLUDING Range
Dim sResult As String ' Result from vLookup in Excel file
Dim sDefaultPath As String ' Default path
Dim sPhraseStart As String ' Position to start for phrases
' Only changes within the following range will be processed
If (Union(Target, Range("A4:A500")).Address <> Range("A4:A500").Address) Or IsEmpty(Target) Then Exit Sub
' Check if user has put "/" at the end of the path or not
sDefaultPath = "'" & Range("A1").Value & IIf(Right(Range("A1").Value, 1) = "/", "[", "/[")
' Get the position where the phrases are located
sPhraseStart = "A" & Range("A3").Value
' For cells which have changed (in column 'A')
For Each cell In Target
' Lets go through each of the phrases
For vNextCell = 1 To Range("A2").Value
' Find phrase to search for
sSearch = Range(sPhraseStart).Offset(0, vNextCell).Value
' Combine the path and range
sDataFile = sDefaultPath & cell.Value & ".xlsx]" & cell.Value & "'!" & _
Range("A1").Offset(0, vNextCell).Value
' Certain cells have data stored horizontally or vertically
Cells(1, 100).Formula = "=" & Range("A2").Offset(0, vNextCell).Value & _
"Lookup(""" & sSearch & """," & sDataFile & ",2, 0)"
' Pull value into memory, for dropping back into live cell
sResult = ""
On Error Resume Next
sResult = Cells(1, 100).Value
Cells(1, 100).Formula = vbNullString
Err.Clear
If sResult <> "" Then
cell.Offset(0, vNextCell) = sResult
End If
Next vNextCell
Next
End Sub
Any thoughts?
Thanks
Dave
The code is:-
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
Dim cell As Range ' Cells that have changed in 'A' Column
Dim vNextCell As Variant ' Counter to sweep along Phrases
Dim sSearch As String ' Phrase to search for
Dim sDataFile As String ' Remote Excel file to search, INCLUDING Range
Dim sResult As String ' Result from vLookup in Excel file
Dim sDefaultPath As String ' Default path
Dim sPhraseStart As String ' Position to start for phrases
' Only changes within the following range will be processed
If (Union(Target, Range("A4:A500")).Address <> Range("A4:A500").Address) Or IsEmpty(Target) Then Exit Sub
' Check if user has put "/" at the end of the path or not
sDefaultPath = "'" & Range("A1").Value & IIf(Right(Range("A1").Value, 1) = "/", "[", "/[")
' Get the position where the phrases are located
sPhraseStart = "A" & Range("A3").Value
' For cells which have changed (in column 'A')
For Each cell In Target
' Lets go through each of the phrases
For vNextCell = 1 To Range("A2").Value
' Find phrase to search for
sSearch = Range(sPhraseStart).Offset(0, vNextCell).Value
' Combine the path and range
sDataFile = sDefaultPath & cell.Value & ".xlsx]" & cell.Value & "'!" & _
Range("A1").Offset(0, vNextCell).Value
' Certain cells have data stored horizontally or vertically
Cells(1, 100).Formula = "=" & Range("A2").Offset(0, vNextCell).Value & _
"Lookup(""" & sSearch & """," & sDataFile & ",2, 0)"
' Pull value into memory, for dropping back into live cell
sResult = ""
On Error Resume Next
sResult = Cells(1, 100).Value
Cells(1, 100).Formula = vbNullString
Err.Clear
If sResult <> "" Then
cell.Offset(0, vNextCell) = sResult
End If
Next vNextCell
Next
End Sub
Any thoughts?
Thanks
Dave
I should add that in row 2, an 'H' or a 'V' signifies whether horizontal or vertical. Thus turning the formula into vlookup or hlookup.
Cheers
Cheers
rizvisa1
Posts
4478
Registration date
Thursday January 28, 2010
Status
Contributor
Last seen
May 5, 2022
766
Jul 28, 2011 at 04:06 PM
Jul 28, 2011 at 04:06 PM
Sorry Dave, you lost me with your last 2 msgs
Sorry mate, I was just making it more flexible. But even if you stick with the original code you provided, you'll notice that when you first open the spreadsheet, it doesn't populate the cells when you make an entry in column 'A'. Any subsequent entries, do appear. It is just after the open that it doesn't. Not sure if it is a bug in Excel or what? I notice that the cell which temporarily holds the value does have the correct value, but when it is tranferred to the variable, it isn't transferred, which makes me think it is a bug in Excel.?
Thanks
Thanks
rizvisa1
Posts
4478
Registration date
Thursday January 28, 2010
Status
Contributor
Last seen
May 5, 2022
766
Jul 29, 2011 at 04:50 AM
Jul 29, 2011 at 04:50 AM
Sorry I was being thick here. I think the reason is the code with kick in when there is a change in the sheet. I would have thought that when you open the book it will still display what was the picture when you closed it last time. If it does not kick how about adding
That I believe would force full calculation of workbook
On other way might be, what if you dont keep the formula in the cell but rather paste it as values. The upside would be one less formula and downside would be complete disconnect from the other workbook so any update in that workbook would not be reflected in this.
Private Sub Workbook_Open() Application.CalculateFull End Sub
That I believe would force full calculation of workbook
On other way might be, what if you dont keep the formula in the cell but rather paste it as values. The upside would be one less formula and downside would be complete disconnect from the other workbook so any update in that workbook would not be reflected in this.
rizvisa1
Posts
4478
Registration date
Thursday January 28, 2010
Status
Contributor
Last seen
May 5, 2022
766
Jul 29, 2011 at 05:12 AM
Jul 29, 2011 at 05:12 AM
Ok sorry for being idiot,. You dont have the formula in the first place. So my above rant was just a rant. Ok so my solutions will not work. BUT what if you reverse the logic. Instead of putting the value to the cell, you put the formula
sOutAnswer = Cells(Rows.Count, Columns.Count).Formula
Since you are using in you call to see if the return value is NULL or not. I would suggest that modify the function to pass in a parameter to hold either the value or function string (as you may see fit) and then use appropriately
Public Function getValue(sDir As String, sCellText As String, sFormula as variant) As Variant
....
Cells(Rows.Count, Columns.Count).Formula = sInstring
sOutAnswer = Cells(Rows.Count, Columns.Count)
sFormula = mid(sInstring, 2)
Cells(Rows.Count, Columns.Count) = vbNullString
....
End Functiuon
vValue = getValue(cell.Value, cTextSearch, sFormula)
if (vValue <> vbnullstring) _
then
cell.Offset(0, vNextCell).formula = "=" & sFormula
end if
I dont know if that will lead to the initial issue or not. I think it will not. I dont have access to sharepoint from home and I am very much afraid that I will not be able to look at this from work.
sOutAnswer = Cells(Rows.Count, Columns.Count).Formula
Since you are using in you call to see if the return value is NULL or not. I would suggest that modify the function to pass in a parameter to hold either the value or function string (as you may see fit) and then use appropriately
Public Function getValue(sDir As String, sCellText As String, sFormula as variant) As Variant
....
Cells(Rows.Count, Columns.Count).Formula = sInstring
sOutAnswer = Cells(Rows.Count, Columns.Count)
sFormula = mid(sInstring, 2)
Cells(Rows.Count, Columns.Count) = vbNullString
....
End Functiuon
vValue = getValue(cell.Value, cTextSearch, sFormula)
if (vValue <> vbnullstring) _
then
cell.Offset(0, vNextCell).formula = "=" & sFormula
end if
I dont know if that will lead to the initial issue or not. I think it will not. I dont have access to sharepoint from home and I am very much afraid that I will not be able to look at this from work.
Jul 12, 2011 at 06:10 AM
How about trying to use indirect
=INDIRECT("''http://teamsite.company.com/" & A1 & "/[MySpreadsheet.xlsx]Sheet1'!F4 ")
I have not tired it, so not sure if it will work.
Jul 12, 2011 at 06:14 AM
Jul 12, 2011 at 06:45 AM
First I've heard of MOREFUNC, will have to look into that! However, ideally would be rolled out to various users, hence prefer to use the inbuilt functions if poss.
Sorry to set yourselves such a challenge!
Thanks for any advice you can provide.
Jul 12, 2011 at 06:54 AM
Jul 12, 2011 at 07:04 AM