Excel cell format change

Please post any questions regarding the program here.

Moderator: 2020vision

Excel cell format change

Postby doris_day » Wed Aug 03, 2011 6:56 am

This may be obvious to those who know Excel better than me but I can't seem to change a cell format to the way I need.
Say I'm importing data from a web query and the data reads 2-4 in HTML, it appears in Excel as 02-Apr as it assumes it should be a date. I want it to read as 2-4 but can't find a format change that will do that.
As I said, this may be simple............but then so am I :)
'He was looking for the card so high and wild he'd never need to deal another' - Leonard Cohen
User avatar
doris_day
 
Posts: 968
Joined: Fri Nov 02, 2007 12:34 am

Postby osknows » Wed Aug 03, 2011 7:23 am

Hi doris,

Try formatting the cell as text. However, sometimes even this won't work and you have to force excel to recognize it as text by prefixing an apostrophe when writing to the sheet. Usually, text with characters - or / causes problems.

Eg say you have variable Var = "2-4"

.range("A1").value = "'" & Var
User avatar
osknows
 
Posts: 946
Joined: Wed Jul 29, 2009 12:01 am

Postby Ian » Wed Aug 03, 2011 7:42 am

There's an option to disable date recognition when you first set the web query up. You click the button "options" top right on my old version 2003 of Excel and tick "disable date recognition" - not sure where it is on later versions. You might have to start again as the cells will now be formatted as dates.
Ian
 
Posts: 834
Joined: Sat Nov 19, 2005 8:35 am
Location: Birmingham

Postby doris_day » Wed Aug 03, 2011 10:47 am

Thanks for the replies. The easiest way to do this was using the disable date recognition, so thanks again. Yet another hidden option......
'He was looking for the card so high and wild he'd never need to deal another' - Leonard Cohen
User avatar
doris_day
 
Posts: 968
Joined: Fri Nov 02, 2007 12:34 am

Postby osknows » Wed Aug 03, 2011 12:09 pm

We'll speak again in a few months when none of your dates work :)
User avatar
osknows
 
Posts: 946
Joined: Wed Jul 29, 2009 12:01 am

Postby doris_day » Wed Aug 03, 2011 2:02 pm

Thanks os...btw, I've found a guy in India (arent they all there ?) that is willing to do some VS tutoring on Skype....he seems quite good but then again, you never know...I'll keep you updated on my progress :)
'He was looking for the card so high and wild he'd never need to deal another' - Leonard Cohen
User avatar
doris_day
 
Posts: 968
Joined: Fri Nov 02, 2007 12:34 am

Postby Shaun » Wed Aug 03, 2011 3:48 pm

osknows, maybe you could help me out with a similar problem as i am not sure what you are explaining.

I am importing this webpage http://formguide.cyberhorse.com.au/inde ... d&raceno=8

Now where it has stats like Track 8:1-2-0 somtimes the formating is just numbers even if i change it ti text when i import a bew page they get changed again.

It dose not change them all just some mainly if the first set of numbers is above 10.

Can you give me an example of how to fix this, i will provide my macro for the import.

Code: Select all
Sub Form()
Sheets("Form").Select
    Sheets("Form").Range("B1:J1000").Select
    Selection.ClearContents
    Columns("B:J").Select
    Selection.NumberFormat = "@"
    With Sheets("Form").QueryTables.Add(Connection:= _
        "URL;" & Sheets("Data").Range("A1"), Destination:=Sheets("Form").Range("$B$1"))
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlOverwriteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .WebFormatting = xlWebFormattingNone
        .WebPreFormattedTextToColumns = False
        .WebConsecutiveDelimitersAsOne = True
        .WebSingleBlockTextImport = False
        .WebDisableDateRecognition = True
        .WebDisableRedirections = False
        .Refresh BackgroundQuery:=False
    End With
Columns("B:J").Select
    Selection.Copy
    Application.DisplayAlerts = False
    Sheets("Data").Select
    Range("AA1").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Columns("AA:AA").Select
    Application.CutCopyMode = False
    Selection.TextToColumns Destination:=Range("AA1"), DataType:=xlDelimited, _
        TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
        Semicolon:=False, Comma:=True, Space:=False, Other:=False, FieldInfo _
        :=Array(Array(1, 1), Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1), Array(6, 1), _
        Array(7, 1), Array(8, 1), Array(9, 1), Array(10, 1), Array(11, 1), Array(12, 1), Array(13, 1 _
        ), Array(14, 1), Array(15, 1), Array(16, 1), Array(17, 1), Array(18, 1), Array(19, 1)), _
        TrailingMinusNumbers:=True
        Columns("AC:AC").Select
    Selection.TextToColumns Destination:=Range("AC1"), DataType:=xlDelimited, _
        TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
        Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
        :=Array(1, 5), TrailingMinusNumbers:=True
        Application.DisplayAlerts = True
        Range("A1").Select
       
End Sub


The macro does a few other things but i need to get this fixed as it is part of an automated ratings program i use.
Shaun
 
Posts: 435
Joined: Fri May 09, 2008 11:11 pm
Location: Kellerberrin, Western Australia

Postby osknows » Wed Aug 03, 2011 7:39 pm

Hi Shaun,

Do you have an example of the text that comes out incorrect, both what it should be and what it changes to?

I've tried your code and the link and everything appears to extract correctly.
User avatar
osknows
 
Posts: 946
Joined: Wed Jul 29, 2009 12:01 am

Postby Shaun » Thu Aug 04, 2011 1:07 am

I will run a few tests today and then if i could e-mail you the sheet may give you a better idea, it is part of a large workbook but i will just send the sheet.
Shaun
 
Posts: 435
Joined: Fri May 09, 2008 11:11 pm
Location: Kellerberrin, Western Australia

Postby Shaun » Thu Aug 04, 2011 1:17 am

I am having trouble finding an example today as all the races seam fine, as i said it is a specific event that creates the problem and i guess today is fine.

I will post back when this happens again, thanks for your help.
Shaun
 
Posts: 435
Joined: Fri May 09, 2008 11:11 pm
Location: Kellerberrin, Western Australia

Postby Shaun » Thu Aug 04, 2011 1:48 am

Found an issue with this page from today.

http://formguide.cyberhorse.com.au/inde ... s&raceno=6

If you import to cell A1 the problem is in H79 it should be displayed as 27:9-5-1 but instead is displayed as 40549.13125 in my script i have .WebDisableDateRecognition = True this solves any date issue with some of the other formats but won't solve this.
Shaun
 
Posts: 435
Joined: Fri May 09, 2008 11:11 pm
Location: Kellerberrin, Western Australia

Postby osknows » Thu Aug 04, 2011 12:40 pm

I find Webqueries a nightmare as they tend to do there own thing with very little control over the results. The example below show a quick and 'dirty' way around it by replacing all instances of ":" with "::" thereby ensuring Excel won't recognise these strings as a date

Code: Select all
Sub form()
Sheets("Form").Select
    Sheets("Form").Range("B1:J1000").Select
    Selection.ClearContents
    Columns("B:J").Select
    Selection.NumberFormat = "@"
    formhtml = ExecuteWebRequest(ThisWorkbook.Sheets("Data").Range("A1").Value)
    formhtml = Replace(formhtml, ":", "::")
    outputtext (formhtml)
   
    Set temp_qt = ThisWorkbook.Sheets("Form").QueryTables.Add(Connection:= _
            "URL;" & ThisWorkbook.Path & "\temp.txt" _
            , Destination:=ThisWorkbook.Sheets("Form").Range("A1"))
    With temp_qt
        .Name = "test"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlOverwriteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .WebSelectionType = xlAllTables
        .WebFormatting = xlWebFormattingNone
        .WebPreFormattedTextToColumns = True
        .WebConsecutiveDelimitersAsOne = True
        .WebSingleBlockTextImport = False
        .WebDisableDateRecognition = True
        .WebDisableRedirections = False
        .Refresh BackgroundQuery:=False
    End With
    Set temp_qt = Nothing
    Kill ThisWorkbook.Path & "\temp.txt"
    If ThisWorkbook.Connections.Count > 0 Then ThisWorkbook.Connections.Item(ThisWorkbook.Connections.Count).Delete

Columns("B:J").Select
    Selection.Copy
    Application.DisplayAlerts = False
    Sheets("Data").Select
    Range("AA1").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Columns("AA:AA").Select
    Application.CutCopyMode = False
    Selection.TextToColumns Destination:=Range("AA1"), DataType:=xlDelimited, _
        TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
        Semicolon:=False, Comma:=True, Space:=False, Other:=False, FieldInfo _
        :=Array(Array(1, 1), Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1), Array(6, 1), _
        Array(7, 1), Array(8, 1), Array(9, 1), Array(10, 1), Array(11, 1), Array(12, 1), Array(13, 1 _
        ), Array(14, 1), Array(15, 1), Array(16, 1), Array(17, 1), Array(18, 1), Array(19, 1)), _
        TrailingMinusNumbers:=True
        Columns("AC:AC").Select
    Selection.TextToColumns Destination:=Range("AC1"), DataType:=xlDelimited, _
        TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
        Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
        :=Array(1, 5), TrailingMinusNumbers:=True
        Application.DisplayAlerts = True
        Range("A1").Select
       
End Sub

Public Function ExecuteWebRequest(url As String) As String
    Dim oXHTTP As Object
    Set oXHTTP = CreateObject("MSXML2.XMLHTTP")
    oXHTTP.Open "GET", url, False
    oXHTTP.send
    ExecuteWebRequest = oXHTTP.responseText
    Set oXHTTP = Nothing
End Function

Public Function outputtext(text As String)
Dim MyFile As String, fnum As String
        MyFile = ThisWorkbook.Path & "\temp.txt"
        fnum = FreeFile()
        Open MyFile For Output As fnum
        Print #fnum, text
        Close #fnum
End Function
User avatar
osknows
 
Posts: 946
Joined: Wed Jul 29, 2009 12:01 am

Postby Shaun » Thu Aug 04, 2011 1:38 pm

I will give it a try and let you know.

i downloaded another sheet some time back that get data from the web but they didn't use web queries not sure what they did i will have to dig it out and try to understand it.
Shaun
 
Posts: 435
Joined: Fri May 09, 2008 11:11 pm
Location: Kellerberrin, Western Australia

Postby Shaun » Thu Aug 04, 2011 3:20 pm

Looks to be working, i had to make a few changes as the data import changed a little and i had to compensate for :: but all is good.

Maybe you can answer a question for me, the script has increased in speed, what did i have in mine that would be slowing it down?
Shaun
 
Posts: 435
Joined: Fri May 09, 2008 11:11 pm
Location: Kellerberrin, Western Australia

Postby osknows » Thu Aug 04, 2011 3:39 pm

I expect it will be because MSXML2 is much faster than a webquery at downloading data.
User avatar
osknows
 
Posts: 946
Joined: Wed Jul 29, 2009 12:01 am

Next

Return to Help

Who is online

Users browsing this forum: Google [Bot] and 73 guests

Sports betting software from Gruss Software


The strength of Gruss Software is that it’s been designed by one of you, a frustrated sports punter, and then developed by listening to dozens of like-minded enthusiasts.

Gruss is owned and run by brothers Gary and Mark Russell. Gary discovered Betfair in 2004 and soon realised that using bespoke software to place bets was much more efficient than merely placing them through the website.

Gary built his own software and then enhanced its features after trialling it through other Betfair users and reacting to their improvement ideas, something that still happens today.

He started making a small monthly charge so he could work on it full-time and then recruited Mark to help develop the products and Gruss Software was born.

We think it’s the best of its kind and so do a lot of our customers. But you can never stand still in this game and we’ll continue to improve the software if any more great ideas emerge.