+ Reply to Thread
Results 1 to 6 of 6

private function repeating

Hybrid View

plato private function repeating 02-03-2012, 09:19 AM
Mordred Re: private function repeating 02-03-2012, 09:25 AM
Jack in the UK Re: private function repeating 02-03-2012, 10:43 AM
plato Re: private function repeating 02-03-2012, 07:46 PM
plato Re: private function repeating 02-04-2012, 08:26 PM
plato Re: private function repeating 02-04-2012, 08:35 PM
  1. #1
    Registered User
    Join Date
    03-28-2008
    Posts
    73

    private function repeating

    Hello,
    I have a private function which works well.What I am trying to do after the formula has run is to copy and paste values into the next column.What is happening then is every time I try to copy and paste into this next column the formula starts working again calculating the cells.It eventually finishes and the column is filled with only the values.The problem is why is this happening and how do I stop it from recalculating when I try to copy and paste into the next column.As there is a limit on the number of entries I can download using this function by having the function start calculating again means that I run out of opportunities and the original data then changes to #Value.Hoping someone knows what is going on and may be able to assist me please.

  2. #2
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: private function repeating

    Can you provide the procedure for us to look at?
    If you're happy with someone's help, click that little star at the bottom left of their post to give them Reps.

    ---Keep on Coding in the Free World---

  3. #3
    Valued Forum Contributor
    Join Date
    07-21-2008
    Location
    London, UK
    Posts
    326

    Re: private function repeating

    functions should never take actions such as here you explain copy / paste data

    Correctly (IMO only) the function should pass a result and that result acted on ie

    one code procedure Sub and one Function

    that way its easier to maintain and parse

    post the code so we can have a look

  4. #4
    Registered User
    Join Date
    03-28-2008
    Posts
    73

    Re: private function repeating

    Hello Thanks for responses.
    The following is the code I am using in a macro.What I have been doing for example is I have a list of cities in COLA,and a
    second list in COL B.I then place this in COL C
    =GetDistance(A1,B1,"Australia")
    Now this works well and the distances are automatically entered into COL C,but when I then try to copy and paste values only into COL D the formula immediately starts working again and down the bottom left shows cell calculating.After cells have
    calculated again I have the list of distances (values only) in COL D.The problem is that because this has used up twice the requests for distances I go over my limit.(The limit for daily requests is 2400).



    Public Function GetDistance(StartCity As String, Destination As String, Country As String) As Long
    
    Const BaseUrl As String = "http://maps.googleapis.com/maps/api/distancematrix/xml?origins={startcity}+{country}&destinations={destination}+{country}&mode=driving&language=en&sensor=false"
    
    Dim odoc As Object
    Dim Url As String
    
    
    Url = Replace(Replace(Replace(BaseUrl, "{startcity}", StartCity), "{destination}", Destination), "{country}", Country)
    
    
    With CreateObject("MSXML2.DOMDocument")
        .async = False
        .validateOnParse = False
        
        If .Load(Url) = False Then Exit Function
        GetDistance = CLng(Split(Replace(Filter(Split(Replace("~" & .documentElement.XML, "</distance>", "<distance>~"), "<distance>"), "~", False)(0), "<value>", "</value>"), "</value>")(1)) / 1000
        
        .abort
     End With
    
    
    End Function
    
    Sub test()
    
    MsgBox (Format(GetDistance("Perth", "Sydney", "Australia"), "#,###")) & " Km"
    End Sub
    Last edited by Mordred; 02-04-2012 at 12:55 AM.

  5. #5
    Registered User
    Join Date
    03-28-2008
    Posts
    73

    Re: private function repeating

    Hello,
    I realised I should have stated that after the first entry I then use this
    Sub formulaallcolDistanceH()
    Dim LR As Long
    LR = Range("A" & Rows.Count).End(xlUp).Row
    Range("H1").AutoFill Destination:=Range("H1:H" & LR)
    End Su
    to continue the formula down the column as the
    length of the column varies each day .

  6. #6
    Registered User
    Join Date
    03-28-2008
    Posts
    73

    Re: private function repeating

    duplicate entry

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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