+ Reply to Thread
Results 1 to 26 of 26

macro to detect domain names (in url's) ?

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    05-10-2011
    Location
    London, England
    MS-Off Ver
    Excel 2019
    Posts
    145

    macro to detect domain names (in url's) ?

    Hello friends,
    I have an excel file with a single column, this column has 32 rows, each row has a url in it
    If you look at column A you will see that sometimes the same domain name appear more than a single time (for example: domain1.com/kjh54asfdasd.html, domain1.com/asd.html, and so on...) while some other times the domain name appear only once (for example: domain3.com/kjhgfjasfdasd.html).


    My question:

    Is there a way to make a macro that will do the following 2 things:

    1. Detect all domain names on column A from within the url's (for example: if the url is example.net/aaaaa.asp - then the macro detect example.net as the domain of the url), the detection should support the following domain type: .com, .net, .info, .org, .edu

    2. Extract the first 3 instances of each domain name on column A (together with the entire url) and copy them to column B
    If some domains has less than 3 instances - the macro will copy them as well


    Please see file attached so you see what I mean.
    Attached Files Attached Files
    Last edited by sami770; 01-07-2013 at 06:18 AM.

  2. #2
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,625

    Re: macro to detect domain names (in url's) ?

    your sample list does not match your description.
    Ben Van Johnson

  3. #3
    Forum Contributor
    Join Date
    05-10-2011
    Location
    London, England
    MS-Off Ver
    Excel 2019
    Posts
    145

    Re: macro to detect domain names (in url's) ?

    The sample file (as could be understood by its name) is just a sample, but the idea is the same.
    However, I just edited the description to fit the sample file.

    My simple question remains the same:
    Is it possible to do the following 2 things with a single macro?

    1. Detect all domain names on column A from within the url's (for example: if the url is example.net/aaaaa.asp - then the macro detect example.net as the domain of the url), the detection should support the following domain type: .com, .net, .info, .org, .edu

    2. Extract the first 3 instances of each domain name on column A (together with the entire url) and copy them to column B
    If some domains has less than 3 instances - the macro will copy them as well


    Again, see the sample file attached on my first post...


    Your help is highly appreciated!



    Quote Originally Posted by protonLeah View Post
    your sample list does not match your description.
    Last edited by sami770; 01-07-2013 at 06:17 AM.

  4. #4
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: macro to detect domain names (in url's) ?

    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please mark your Thread as SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  5. #5
    Forum Contributor
    Join Date
    05-10-2011
    Location
    London, England
    MS-Off Ver
    Excel 2019
    Posts
    145

    Re: macro to detect domain names (in url's) ?

    Yes, I really needed a quick answer so I posted my question also there, I hope its Ok.

    Quote Originally Posted by jaslake View Post

  6. #6
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: macro to detect domain names (in url's) ?

    No, not OK...read the Forum Rules on the proper etiquette. You MAY do so if you follow the proper etiquette. The other Forum has the same rule.

  7. #7
    Forum Contributor
    Join Date
    05-10-2011
    Location
    London, England
    MS-Off Ver
    Excel 2019
    Posts
    145

    Re: macro to detect domain names (in url's) ?

    OK,
    I got you & agree
    It will NOT happened again!
    Sami

    Quote Originally Posted by jaslake View Post
    No, not OK...read the Forum Rules on the proper etiquette. You MAY do so if you follow the proper etiquette. The other Forum has the same rule.

  8. #8
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: macro to detect domain names (in url's) ?

    As requested:

    Public Sub FindTopDomains()
    
    '#
    '# declare private variables
    '#
         Dim pvt_lng_RowNumber As Long
         Dim pvt_int_FindCharacter As Integer
         Dim pvt_lng_TargetRow As Long
         Dim pvt_dct_Domain As Object
         Dim pvt_str_DomainName As String
         Dim pvt_str_Extension As String
         
    '#
    '# initialise
    '#
         Set pvt_dct_Domain = CreateObject("Scripting.Dictionary")
         pvt_lng_TargetRow = 1
         
    '#
    '# execute a loop on all filled rows on the worksheet - assuming that all
    '# data is provided on Sheet1 - change if necessary
    '#
         With ThisWorkbook.Worksheets("Sheet1")
              For pvt_lng_RowNumber = 2 To .Range("A2").CurrentRegion.Rows.Count
              
              '#
              '# isolate the domain name before the first occurence of the '/' character
              '#
                   pvt_int_FindCharacter = InStr(1, .Cells(pvt_lng_RowNumber, "A").Value, "/", vbTextCompare)
                   
              '#
              '# isolate the domain name
              '#
                   If pvt_int_FindCharacter > 0 Then
                        pvt_str_DomainName = Left$(.Cells(pvt_lng_RowNumber, "A").Value, (pvt_int_FindCharacter - 1))
                   Else
                        pvt_str_DomainName = .Cells(pvt_lng_RowNumber, "A").Value
                   End If
                        
              '#
              '# only process the domain if the extension is in a list of provided extensions
              '#
                   pvt_str_Extension = Mid$(pvt_str_DomainName, (InStr(1, pvt_str_DomainName, ".") - 1))
                   If InStr(1, ".com.net.info.org.edu", pvt_str_Extension, vbTextCompare) > 0 Then
                   
                   '#
                   '# add an entry to the dictionary object used to count occurences if this is the first occurence
                   '# of the domain
                   '#
                        If Not pvt_dct_Domain.Exists(pvt_str_DomainName) Then
                             pvt_dct_Domain.Add pvt_str_DomainName, 0
                        End If
                   
                   '#
                   '# raise the occurence counter and output the full url when the domain has been encountered
                   '# <= 3 times
                   '#
                        pvt_dct_Domain.Item(pvt_str_DomainName) = pvt_dct_Domain.Item(pvt_str_DomainName) + 1
                        If pvt_dct_Domain.Item(pvt_str_DomainName) < 4 Then
                             pvt_lng_TargetRow = pvt_lng_TargetRow + 1
                             .Cells(pvt_lng_TargetRow, "B").Value = .Cells(pvt_lng_RowNumber, "A").Value
                        End If 'counter <= 3
                   End If 'valid extension
                   
              Next pvt_lng_RowNumber
         End With
    
    End Sub
    If you like my contribution click the star icon!

  9. #9
    Forum Contributor
    Join Date
    05-10-2011
    Location
    London, England
    MS-Off Ver
    Excel 2019
    Posts
    145

    Re: macro to detect domain names (in url's) ?

    does not work

    Quote Originally Posted by OllieB View Post
    As requested:

    Public Sub FindTopDomains()
    
    '#
    '# declare private variables
    '#
         Dim pvt_lng_RowNumber As Long
         Dim pvt_int_FindCharacter As Integer
         Dim pvt_lng_TargetRow As Long
         Dim pvt_dct_Domain As Object
         Dim pvt_str_DomainName As String
         Dim pvt_str_Extension As String
         
    '#
    '# initialise
    '#
         Set pvt_dct_Domain = CreateObject("Scripting.Dictionary")
         pvt_lng_TargetRow = 1
         
    '#
    '# execute a loop on all filled rows on the worksheet - assuming that all
    '# data is provided on Sheet1 - change if necessary
    '#
         With ThisWorkbook.Worksheets("Sheet1")
              For pvt_lng_RowNumber = 2 To .Range("A2").CurrentRegion.Rows.Count
              
              '#
              '# isolate the domain name before the first occurence of the '/' character
              '#
                   pvt_int_FindCharacter = InStr(1, .Cells(pvt_lng_RowNumber, "A").Value, "/", vbTextCompare)
                   
              '#
              '# isolate the domain name
              '#
                   If pvt_int_FindCharacter > 0 Then
                        pvt_str_DomainName = Left$(.Cells(pvt_lng_RowNumber, "A").Value, (pvt_int_FindCharacter - 1))
                   Else
                        pvt_str_DomainName = .Cells(pvt_lng_RowNumber, "A").Value
                   End If
                        
              '#
              '# only process the domain if the extension is in a list of provided extensions
              '#
                   pvt_str_Extension = Mid$(pvt_str_DomainName, (InStr(1, pvt_str_DomainName, ".") - 1))
                   If InStr(1, ".com.net.info.org.edu", pvt_str_Extension, vbTextCompare) > 0 Then
                   
                   '#
                   '# add an entry to the dictionary object used to count occurences if this is the first occurence
                   '# of the domain
                   '#
                        If Not pvt_dct_Domain.Exists(pvt_str_DomainName) Then
                             pvt_dct_Domain.Add pvt_str_DomainName, 0
                        End If
                   
                   '#
                   '# raise the occurence counter and output the full url when the domain has been encountered
                   '# <= 3 times
                   '#
                        pvt_dct_Domain.Item(pvt_str_DomainName) = pvt_dct_Domain.Item(pvt_str_DomainName) + 1
                        If pvt_dct_Domain.Item(pvt_str_DomainName) < 4 Then
                             pvt_lng_TargetRow = pvt_lng_TargetRow + 1
                             .Cells(pvt_lng_TargetRow, "B").Value = .Cells(pvt_lng_RowNumber, "A").Value
                        End If 'counter <= 3
                   End If 'valid extension
                   
              Next pvt_lng_RowNumber
         End With
    
    End Sub

  10. #10
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: macro to detect domain names (in url's) ?

    Quote Originally Posted by sami770 View Post
    does not work
    Sami, while I have tested it myself without any problems using your own example data and I do appreciate feedback, a bit more information than "does not work" would be appreciated.

  11. #11
    Forum Contributor
    Join Date
    05-10-2011
    Location
    London, England
    MS-Off Ver
    Excel 2019
    Posts
    145

    Re: macro to detect domain names (in url's) ?

    still looking for a solution that support the 5 domain types i specified...
    Last edited by sami770; 01-07-2013 at 02:04 PM.

  12. #12
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: macro to detect domain names (in url's) ?

    as an option
    Sub ertert()
    Dim x, i&, j&, k&, s$
    With Range("A1", Cells(Rows.Count, 1).End(xlUp)(2, 1))
        x = .Value
        For i = 1 To UBound(x) - 1
            s = Split(x(i, 1), "/")(0)
            Do While InStr(x(i, 1), s)
                k = k + 1
                If k < 4 Then j = j + 1: x(j, 1) = x(i, 1)
                i = i + 1
            Loop
            i = i - 1: k = 0
        Next i
        .Offset(, 1).Resize(j) = x
    End With
    End Sub

  13. #13
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: macro to detect domain names (in url's) ?

    @nilem, as long as the entries are sorted alphabetically that would work too. Your solution does not check on a valid extension though, but I am not sure whether that is really required.

  14. #14
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: macro to detect domain names (in url's) ?

    Why don't you provide a copy of the work with the example data and my code so I can check why it does not work?

  15. #15
    Forum Contributor
    Join Date
    05-10-2011
    Location
    London, England
    MS-Off Ver
    Excel 2019
    Posts
    145

    Re: macro to detect domain names (in url's) ?

    Sorry, you are right about
    Please see a sample file that your solution did not work with ;-(

    Quote Originally Posted by OllieB View Post
    Why don't you provide a copy of the work with the example data and my code so I can check why it does not work?
    Attached Files Attached Files

  16. #16
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: macro to detect domain names (in url's) ?

    Sam,

    I found a typing error. Please change the statement below

    pvt_str_Extension = Mid$(pvt_str_DomainName, (InStr(1, pvt_str_DomainName, ".") - 1))
    to

    pvt_str_Extension = Mid$(pvt_str_DomainName, (InStr(1, pvt_str_DomainName, ".")))
    apologies

  17. #17
    Forum Contributor
    Join Date
    05-10-2011
    Location
    London, England
    MS-Off Ver
    Excel 2019
    Posts
    145

    Re: macro to detect domain names (in url's) ?

    Thanks,
    I replaced the lines according to your instructions but I still get an error from the excel debugger...
    The error message refer to this line in the macro: pvt_str_Extension = Mid$(pvt_str_DomainName, (InStr(1, pvt_str_DomainName, ".")))
    Could you take another look at this please?

    Quote Originally Posted by OllieB View Post
    Sam,

    I found a typing error. Please change the statement below

    pvt_str_Extension = Mid$(pvt_str_DomainName, (InStr(1, pvt_str_DomainName, ".") - 1))
    to

    pvt_str_Extension = Mid$(pvt_str_DomainName, (InStr(1, pvt_str_DomainName, ".")))
    apologies

  18. #18
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,625

    Re: macro to detect domain names (in url's) ?

    Your post does not comply with Rule 12 of our Forum RULES. Don't quote whole posts -- it's just clutter. If you are responding to a post out of sequence, limit quoted content to a few relevant lines that makes clear to whom and what you are responding.

  19. #19
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: macro to detect domain names (in url's) ?

    @protonLeah

    Give the kid a break...I just hammered her!

  20. #20
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: macro to detect domain names (in url's) ?

    Sam,

    the statement attempts to find the extension part of the domain name. If it returns an error then I assume you have domain names without an extension.

    replace the single statement by:

         pvt_str_Extension = vbNullString
         If InStr(1, pvt_str_DomainName, ".") > 0 Then
              pvt_str_Extension = Mid$(pvt_str_DomainName, InStr(1, pvt_str_DomainName, "."))
         End If

  21. #21
    Forum Contributor
    Join Date
    05-10-2011
    Location
    London, England
    MS-Off Ver
    Excel 2019
    Posts
    145

    Re: macro to detect domain names (in url's) ?

    I followed this advice as well...
    Now I did not get debugger error but the final result is not whatI am looking for.
    Instead of filling column B with the first 3 url's of each domain - I got only 3 results total
    If you run your solution with THE SECOND SAMPLE I attached to this thread (http://www.excelforum.com/attachment...8&d=1357583395) you will see what I mean

    Quote Originally Posted by OllieB View Post
    Sam,

    the statement attempts to find the extension part of the domain name. If it returns an error then I assume you have domain names without an extension.

    replace the single statement by:

         pvt_str_Extension = vbNullString
         If InStr(1, pvt_str_DomainName, ".") > 0 Then
              pvt_str_Extension = Mid$(pvt_str_DomainName, InStr(1, pvt_str_DomainName, "."))
         End If

  22. #22
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: macro to detect domain names (in url's) ?

    Sam,

    Many things were wrong with the code as it did not expect (or check for) http://, https:// prefixes and it also expected the data to start on row 2 as per your previous example. Please check the enclosed workbook
    Attached Files Attached Files

  23. #23
    Forum Contributor
    Join Date
    05-10-2011
    Location
    London, England
    MS-Off Ver
    Excel 2019
    Posts
    145

    Re: macro to detect domain names (in url's) ?

    Finally you did it right
    PERFECT, SIMPLY PERFECT
    Thank you so much for your time and efforts, I do appreciate it!
    Quote Originally Posted by OllieB View Post
    Sam,

    Many things were wrong with the code as it did not expect (or check for) http://, https:// prefixes and it also expected the data to start on row 2 as per your previous example. Please check the enclosed workbook

  24. #24
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: macro to detect domain names (in url's) ?

    Quote Originally Posted by sami770 View Post
    PERFECT, SIMPLY PERFECT
    Thank you so much for your time and efforts, I do appreciate it!
    You are very welcome. Please mark your thread as SOLVED when you have a moment.

+ 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