+ Reply to Thread
Results 1 to 3 of 3

Problems with data format? My code goes wrong?

Hybrid View

  1. #1
    Registered User
    Join Date
    09-13-2017
    Location
    HK
    MS-Off Ver
    2016
    Posts
    4

    Problems with data format? My code goes wrong?

    I am a new beginnner to excel VBA. I hope to automate the task of importing data from web into excel. However, when I run the following code, the result is far from what it should be :


    Sub data()
    
    ' data Macro
    
    Dim ticker As Variant
    ticker = Sheets("input").Range("A1").Value
    
    Sheets("ps").Select
    Cells.Clear
    
        ActiveWorkbook.Queries.Add _
        Name:="Table_" & ticker & "", Formula:= _
            "let" & Chr(13) & "" & Chr(10) & "    Source = Web.Page(Web.Contents(""https://www.etnet.com.hk/www/eng/stocks/realtime/quote_ci_pl.php?code=" & ticker & """))," & Chr(13) & "" & Chr(10) & "    Data0 = Source{0}[Data]," & Chr(13) & "" & Chr(10) & "    #""Changed Type"" = Table.TransformColumnTypes(Data0,{{""Column1"", type text}, {""Column2"", type text}, {""Column3"", type text}, {""Column4"", type text}, {""Column5"", type text}, {""Column6"", type text}, {" & _
            """Column7"", type text}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & "    #""Changed Type"""
        
        With Sheets("ps").ListObjects.Add(SourceType:=0, Source:= _
            "OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=""Table_" & ticker & """;Extended Properties=""""" _
            , Destination:=Range("$A$1")).QueryTable
            .CommandType = xlCmdSql
            .CommandText = Array("SELECT * FROM [Table 0]")
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .PreserveFormatting = True
            .RefreshOnFileOpen = False
            .BackgroundQuery = True
            .RefreshStyle = xlInsertDeleteCells
            .SavePassword = False
            .SaveData = True
            .AdjustColumnWidth = True
            .RefreshPeriod = 0
            .PreserveColumnInfo = True
            .ListObject.DisplayName = "Query_" & ticker & ""
    '        .Refresh BackgroundQuery:=False
        End With
    
    End Sub
    The result of the above code is shown below and is wrong :

    wrong.png

    Obviously, there is no formating? I have to load the data into the sheet mannually and the expected result should be :


    right.png


    How can I automate both task (import data and load it into the sheet) with VBA? Which part of the code goes wrong? Please give me a hand!
    Last edited by terrryc2115; 09-15-2017 at 05:55 AM.

  2. #2
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: Problems with data format? My code goes wrong?

    Hello terrryc2115,

    Thank you for you quick response in correcting the matter
    Last edited by Winon; 09-15-2017 at 06:40 AM. Reason: Code Tags Added
    Please consider:

    Be polite. Thank those who have helped you. Then Click on the star icon in the lower left part of the contributor's post and add Reputation. Cleaning up when you're done. If you are satisfied with the help you have received, then Please do Mark your thread [SOLVED] .

  3. #3
    Registered User
    Join Date
    09-13-2017
    Location
    HK
    MS-Off Ver
    2016
    Posts
    4

    Re: Problems with data format? My code goes wrong?

    Sorry for not knowing the rule... Thanks for your kind reminder!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] What is wrong with this VBA Code about transposing my data?
    By zicitron in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-25-2016, 05:32 AM
  2. Wrong format of the data field deployment of the pivot table.
    By exceldiser in forum Excel Charting & Pivots
    Replies: 2
    Last Post: 02-18-2015, 11:21 AM
  3. [SOLVED] VBA Code entering Data in wrong cell
    By drsadistic in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 08-07-2014, 03:09 PM
  4. [SOLVED] Data in hours format calculates wrong
    By Cliff Gathern in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 02-04-2014, 12:39 PM
  5. [SOLVED] import web data; end up wrong format in excel
    By Exxcel Noob in forum Excel General
    Replies: 3
    Last Post: 05-27-2012, 12:53 PM
  6. Code is placing data in the wrong range
    By Habanero Time in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-24-2012, 11:25 AM
  7. Conditional Formatting Code - Deletes Wrong Conditional Format
    By RSpecianJr in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-29-2010, 10:53 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1