+ Reply to Thread
Results 1 to 2 of 2

Replace Cell with Value

  1. #1
    Registered User
    Join Date
    12-20-2016
    Location
    US
    MS-Off Ver
    2013, 365
    Posts
    1

    Question Replace Cell with Value

    Hello,
    I am new to Excel and this Forum. I am a student and currently utilizing a User Defined Function I found code for that geocode's addresses utilizing the Google maps api. Below is the code for the function I am using. I was hoping someone could help me have this function replace the cell's contents with it's value at the end the function executing. I have tried various methods I have seen while scouring countless forums and have gotten nothing but errors.

    =IF(E1<>"",IF(E1="INT",mygeocode(F1&" "&G1),mygeocode(E1&" "&F1&" "&G1)),"")

    I am using this formula down the whole column to determine if there is address data and if so, execute the function, otherwise, leave the cell blank. Unfortunately, I have too many addresses and my spreadsheet crashes when it tries to geocode every time it opens. Also, Google Maps API has usage restrictions as well. So, if someone knows how to have this function replace the cell with the value at the end of each use, I should be able to resolve these issues. Any help would be greatly appreciated.

    Function MyGeocode(address As String) As String
    Dim strAddress As String
    Dim strQuery As String
    Dim strLatitude As String
    Dim strLongitude As String
    strAddress = URLEncode(address)
    'Assemble the query string
    strQuery = "http://maps.googleapis.com/maps/api/geocode/xml?"
    strQuery = strQuery & "address=" & strAddress
    strQuery = strQuery & "&sensor=false"
    'define XML and HTTP components
    Dim googleResult As New MSXML2.DOMDocument
    Dim googleService As New MSXML2.XMLHTTP
    Dim oNodes As MSXML2.IXMLDOMNodeList
    Dim oNode As MSXML2.IXMLDOMNode
    'create HTTP request to query URL - make sure to have
    'that last "False" there for synchronous operation
    googleService.Open "GET", strQuery, False
    googleService.send
    googleResult.LoadXML (googleService.responseText)
    Set oNodes = googleResult.getElementsByTagName("geometry")
    If oNodes.Length = 1 Then
    For Each oNode In oNodes
    strLatitude = oNode.ChildNodes(0).ChildNodes(0).Text
    strLongitude = oNode.ChildNodes(0).ChildNodes(1).Text
    MyGeocode = strLatitude & "," & strLongitude

    Application.Wait (Now + TimeValue("0:00:01"))


    Next oNode
    Else
    MyGeocode = "Not Found (try again, you may have done too many too fast)"
    End If

    End Function
    Public Function URLEncode(StringVal As String, Optional SpaceAsPlus As Boolean = False) As String
    Dim StringLen As Long: StringLen = Len(StringVal)
    If StringLen > 0 Then
    ReDim result(StringLen) As String
    Dim i As Long, CharCode As Integer
    Dim Char As String, Space As String
    If SpaceAsPlus Then Space = "+" Else Space = "%20"
    For i = 1 To StringLen
    Char = Mid$(StringVal, i, 1)
    CharCode = Asc(Char)
    Select Case CharCode
    Case 97 To 122, 65 To 90, 48 To 57, 45, 46, 95, 126
    result(i) = Char
    Case 32
    result(i) = Space
    Case 0 To 15
    result(i) = "%0" & Hex(CharCode)
    Case Else
    result(i) = "%" & Hex(CharCode)
    End Select
    Next i
    URLEncode = Join(result, "")
    End If
    End Function

  2. #2
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,926

    Re: Replace Cell with Value

    A couple of things will help get this request processed:

    First:

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Click on Edit to open your thread, then highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here

    (This thread should receive no further responses until this moderation request is fulfilled, as per Forum Rule 7)

    Code tags help with reading the code.

    Second:
    Attach a sample workbook. Make sure there is just enough data to demonstrate your need. Include a BEFORE sheet and an AFTER sheet in the workbook if needed to show the process you're trying to complete or automate. Make sure your desired results are shown, mock them up manually if necessary.

    Remember to desensitize the data.

    Click on GO ADVANCED and then scroll down to Manage Attachments to open the upload window.

    Good luck with your project.
    One spreadsheet to rule them all. One spreadsheet to find them. One spreadsheet to bring them all and at corporate, bind them.

    A picture is worth a thousand words, but a sample spreadsheet is more likely to be worked on.

+ 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. Macro to find and replace specific strings in a cell link by using cell references
    By beginningcoder in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 02-15-2016, 09:37 PM
  2. Replies: 16
    Last Post: 12-02-2014, 03:38 PM
  3. [SOLVED] multiple find and replace cell content based on another cell
    By betatony in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 03-27-2014, 03:07 PM
  4. Find and replace but replace value of cell 3 columns along
    By batchjb69 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-05-2014, 10:57 AM
  5. [SOLVED] Need help writing macro to replace one cell based on contents of adjacent cell
    By TheRaptMuse in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-24-2012, 12:54 PM
  6. Replace portion of data in 1 cell, with all contents of different cell & save as CSV?
    By 1lowbowtie in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 09-17-2011, 11:51 PM
  7. Find, Replace with and then replace adjacent cell
    By Craig2097 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-18-2009, 12:42 PM

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