Extracting Data

zaska

Well-known Member
Joined
Oct 24, 2010
Messages
1,046
Hi,

I want to extract Symbol Names In Column " A" From the Following URL's Kindly suggest VBA Code


<table width="582" border="0" cellpadding="0" cellspacing="0"><col width="582"><tr height="17"> <td style="height:12.75pt;width:437pt" width="582" height="17">http://www.nseindia.com/content/indices/histdata/BANK NIFTY30-05-2011-30-05-2011.csv

</td></tr></table>Extract BANKNIFTY

<table width="582" border="0" cellpadding="0" cellspacing="0"><col width="582"><tr height="17"> <td style="height:12.75pt;width:437pt" width="582" height="17">http://www.nseindia.com/content/indices/histdata/CNX 10030-05-2011-30-05-2011.csv</td> </tr></table>
Extract CNX 100

http://www.nseindia.com/content/indices/histdata/CNX ENERGY30-05-2011-30-05-2011.csv

Extract CNX ENERGY

Etc......

Thank you
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
The following function will take the URL string and extract the bit you're interested in. Paste the code into a new general code module.
Code:
Option Explicit
 
Public Function SymbolName(argURL As String) As String
 
  Dim iSlash As Integer
  Dim iHyphen As Integer
  
  iSlash = InStrRev(argURL, "/")
  iHyphen = InStr(argURL, "-")
  
  If iSlash = 0 Then Exit Function
  If iHyphen = 0 Then Exit Function
  If iHyphen < iSlash Then Exit Function
  
  SymbolName = Mid(argURL, iSlash + 1, iHyphen - iSlash - 3)
 
End Function

Call it in your worksheet like this:-
Code:
=SymbolName(A1)

Or in VBA like this:-
Code:
[I]somevariable[/I]=SymbolName(Range("A1").Value)

Test it thoroughly with all possible inputs before relying on it in an operational environment.
 
Upvote 0
Hi, Can you Kindly explain the Logic in your code..

I am unable to understand how u accomplished this task and i am asking it out of curiosity to learn


Thank you
 
Upvote 0
First we look for the last slash in the URL because the symbol name starts in the next position:-

iSlash = InStrRev(argURL, "/")

Then we look for the first hyphen because the symbol ends three positions before this:-

iHyphen = InStr(argURL, "-")


Next we check whether there are any problems with the URL: if there's no slash or no hyphen in it, then we won't be able to find the symbol, so we exit the function immediately. Also, if the hyphen occurs before the slash, something's wrong so again we exit immediately:-

If iSlash = 0 Then Exit Function
If iHyphen = 0 Then Exit Function
If iHyphen < iSlash Then Exit Function


Finally we take the part of the URL starting from the position after the slash and ending three positions before the hypen:-

SymbolName = Mid(argURL, iSlash + 1, iHyphen - iSlash - 3)

Hopefully the colour coding helps you follow the explanation?
 
Upvote 0
That was very nice explanation.

Thank you very much

I understood it completely

Have a nice day
 
Upvote 0

Forum statistics

Threads
1,224,551
Messages
6,179,476
Members
452,915
Latest member
hannnahheileen

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top