Search code examples
excelvbaselenium-chromedriver

Retain format "dd/mm/yy" of date scraped from web by VBA Selenium


I want to scrape date under format of "dd/mm/yy" from web to Excel by VBA Selenium library.

Sheets("Sheet1").range("A1").value = driver.FindElementById("table-body-scroll").FindElementsByTag("tr").FindElementsByTag("td")(3).Text ' to get the 3rd col 

The date value on web is formatted as "dd/mm/yyyy". When I scrape to Excel, it turns to "mm/dd/yyyy".

How do I force Excel to understand the date as "dd/mm/yyyy"?

The URL is: https://iboard.ssi.com.vn/bang-gia/chung-quyen

The column that contains the date is the third col named "GDCC".


Solution

  • Dim DateAry() As String
    DateAry = Split(driver.FindElementById("table-body-scroll").FindElementsByTag("tr").FindElementsByTag("td")(3).Text, "/")
    Range("A1").Value = DateAry(1) & "/" & DateAry(0) & "/" & DateAry(2)
    Range("A1").NumberFormat = "dd/mm/yyyy"
    

    or

    Dim DateAry() As String
    DateAry = Split(driver.FindElementById("table-body-scroll").FindElementsByTag("tr").FindElementsByTag("td")(3).Text, "/")
    Range("A1").Value = DateAry(1) & "-" & DateAry(0) & "-" & DateAry(2)
    Range("A1").NumberFormat = "DD-MM-YYYY"