简体   繁体   中英

Import data from Website: Converting Java Script code into VBA code

I need to import some data from this website , for which I have written a small java script code which is mentioned below.

Code for extracting Molecular Weight

x = document.getElementById('summary')
y = x.getElementsByTagName('h2')
count = x.getElementsByTagName('h2').length
flag=0
for (i = 0; i < count; i++){
  if(y.item(i).textContent == 'Molecular Weight'){
    console.log(y.item(i).nextSibling.textContent);
   flag=1;
  }
}
if(flag==0)
console.log("Nothing Found");

Code for extracting data from physical properties table

x=document.getElementById('physical')
y=x.getElementsByClassName('TableRow')
count =x.getElementsByClassName('TableRow').length
for(i=0;i<count;i++){
   z=x.getElementsByClassName('TableRow').item(i)
   z.children[0].textContent
   z.children[1].textContent
}

So Is it possible to import to excel through java script?

I think VBA will be better for this purpose but I am new to VBA and not able to identify the appropriate way to write the code in VBA. Please give me hint so that I can proceed in right direction.

Instead of importing through javascript, you might want to try the following. In excel you can import a website into a spreadsheet by going to File -> Open Url . Assuming the structure of the site is the same regardless of the molecule you are looking at you could create VBA script to extract the data just by looking at the cell address. You can use Tools -> Macro -> Record New Macro to record your actions. Then once done you can view the VBA code by going to Tools -> Visual Basic Editor . Then you can improve the code from there to further automate the process.

Here is an example:

Workbooks.Open Filename:="http://chem.sis.nlm.nih.gov/chemidplus/rn/112-92-5"
Range("A44").Select
Selection.Copy
Sheets.Add
Sheets("Sheet2").Select
Range("B4").Select
ActiveSheet.Paste

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM