Solved

Help changes with this excel macro

Posted on 2012-12-29
12
312 Views
Last Modified: 2013-01-09
Below macro is given by Brian and it checks a URL and find if it shows results Yes/No

Can i get all the URL's that are shown below

Example
http://alturl.com/mseg7

this link shows 9 results. Can i get the Number 9 in a column and then the 9 URL's so i can check them directly (Any format is fine)



Option Explicit

Sub Check_URLS()
Dim i           As Long
Dim xResult     As String
Dim xCell       As Range
Dim xLast_Row   As Long

Sheets("Check URL's").Activate

xLast_Row = Range("A1").SpecialCells(xlLastCell).Row

If xLast_Row < 2 Then
    MsgBox ("No data found - run cancelled.")
    Exit Sub
End If

Range("B2:B" & xLast_Row).ClearContents

For Each xCell In Range("A2:A" & xLast_Row)
   
    If xCell <> "" Then
        i = i + 1
        xResult = Get_URL(xCell.Value)
    Else
        xResult = "N/A"
    End If
   
    xCell.Offset(0, 1) = xResult
   
    Application.ScreenUpdating = True
   
Next

End Sub

Function Get_URL(xUrl As String)
Dim xTempSheet  As Worksheet
Dim xConnection As String
Dim xFound      As Range

Application.ScreenUpdating = False

Set xTempSheet = Sheets.Add
xConnection = "x" & Format(Now(), "hhmmss")

On Error Resume Next

    With xTempSheet.QueryTables.Add(Connection:="URL;" & xUrl, Destination:=Range("$A$1"))
        .Name = xConnection
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .WebSelectionType = xlEntirePage
        .WebFormatting = xlWebFormattingNone
        .WebPreFormattedTextToColumns = True
        .WebConsecutiveDelimitersAsOne = True
        .WebSingleBlockTextImport = False
        .WebDisableDateRecognition = False
        .WebDisableRedirections = False
        .Refresh BackgroundQuery:=False
    End With

On Error GoTo 0
Dim fred
Set fred = xTempSheet.QueryTables(1)

If xTempSheet.Range("A1").SpecialCells(xlLastCell).Row = 1 Then
    Application.DisplayAlerts = False
        xTempSheet.Delete
    Application.DisplayAlerts = True
    Get_URL = "N/A"
    Exit Function
End If

Set xFound = xTempSheet.Range("A:A").Find(What:="Your search yielded no results", LookIn:=xlFormulas, LookAt:=xlPart _
                , SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
       
If xFound Is Nothing Then
    Get_URL = "False"
Else
    Get_URL = "True"
End If

Application.DisplayAlerts = False
    xTempSheet.Delete
Application.DisplayAlerts = True

End Function
0
Comment
Question by:bsharath
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
12 Comments
 
LVL 26

Expert Comment

by:redmondb
ID: 38729694
Hi, bsharath.

Plerase see attached. If a page isn't found then a value of -1 is shown. The code is...
Option Explicit

Sub Check_URLS()
Dim i           As Long
Dim xResult     As String
Dim xCell       As Range
Dim xLast_Row   As Long

Sheets("Check URL's").Activate

xLast_Row = Range("A1").SpecialCells(xlLastCell).Row

If xLast_Row < 2 Then
    MsgBox ("No data found - run cancelled.")
    Exit Sub
End If

Range("B2:B" & xLast_Row).ClearContents

For Each xCell In Range("A2:A" & xLast_Row)
    
    If xCell <> "" Then
        i = i + 1
        xResult = Get_URL(xCell.Value)
    Else
        xResult = "N/A"
    End If
    
    xCell.Offset(0, 1) = xResult
    
    Application.ScreenUpdating = True
    
Next

End Sub

Function Get_URL(xUrl As String) As Long
Dim xTempSheet  As Worksheet
Dim xConnection As String
Dim xFound      As Range
Dim NextCell    As Range

Application.ScreenUpdating = False

Set xTempSheet = Sheets.Add
xConnection = "x" & Format(Now(), "hhmmss")

On Error Resume Next

    With xTempSheet.QueryTables.Add(Connection:="URL;" & xUrl, Destination:=Range("$A$1"))
        .Name = xConnection
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .WebSelectionType = xlEntirePage
        .WebFormatting = xlWebFormattingNone
        .WebPreFormattedTextToColumns = True
        .WebConsecutiveDelimitersAsOne = True
        .WebSingleBlockTextImport = False
        .WebDisableDateRecognition = False
        .WebDisableRedirections = False
        .Refresh BackgroundQuery:=False
    End With

On Error GoTo 0
Dim fred
Set fred = xTempSheet.QueryTables(1)

If xTempSheet.Range("A1").SpecialCells(xlLastCell).Row = 1 Then
    Application.DisplayAlerts = False
        xTempSheet.Delete
    Application.DisplayAlerts = True
    Get_URL = -1
    Exit Function
End If

Category:
Set xFound = xTempSheet.Range("A:A").Find(What:="Category: *", LookIn:=xlFormulas, LookAt:=xlPart _
                , SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
        
If xFound Is Nothing Then
    Get_URL = 0
Else
    
    Set NextCell = xFound
    Get_URL = 1
    
    Do
        Set NextCell = Cells.FindNext(After:=NextCell)
        If Not NextCell.Address = xFound.Address Then
            NextCell.Activate
            Get_URL = Get_URL + 1
        End If
        If Not NextCell Is Nothing Then If NextCell.Address = xFound.Address Then Set NextCell = Nothing
    Loop While Not NextCell Is Nothing
    
End If

Application.DisplayAlerts = False
    xTempSheet.Delete
Application.DisplayAlerts = True

End Function

Open in new window

Regards,
Brian.Check-URL-s-III.xlsm
0
 
LVL 11

Author Comment

by:bsharath
ID: 38729701
Thanks count works but i wanted the URL's of the results as well

Example

www.somename/word has 2 results i want the URL's of those 2 results exact page
0
 
LVL 26

Expert Comment

by:redmondb
ID: 38729739
bsharath,

Apologies, that was careless.

You didn't specify how you wanted the URL's displayed, so the attached is a first cut. The code is...
Option Explicit

Sub Check_URLS()
Dim i           As Long
Dim xResult     As String
Dim xCell       As Range
Dim xLast_Row   As Long

Sheets("Check URL's").Activate

xLast_Row = Range("A1").SpecialCells(xlLastCell).Row

If xLast_Row < 2 Then
    MsgBox ("No data found - run cancelled.")
    Exit Sub
End If

Range("B2:C" & xLast_Row).ClearContents

For Each xCell In Range("A2:A" & xLast_Row)
    
    If xCell <> "" Then
        i = i + 1
        Call Get_URL(xCell)
    Else
        xResult = "N/A"
    End If
    
    Application.ScreenUpdating = True
    
Next

End Sub

Sub Get_URL(xCell As Range)
Dim xTempSheet  As Worksheet
Dim xConnection As String
Dim xFound      As Range
Dim NextCell    As Range
Dim xHyper      As Hyperlink
Dim xUrl        As String
Dim xHold       As String
Dim xCount      As Long

Application.ScreenUpdating = False

xUrl = xCell

Set xTempSheet = Sheets.Add
xConnection = "x" & Format(Now(), "hhmmss")

On Error Resume Next

    With xTempSheet.QueryTables.Add(Connection:="URL;" & xUrl, Destination:=Range("$A$1"))
        .Name = xConnection
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .WebSelectionType = xlEntirePage
        .WebFormatting = xlWebFormattingAll
        .WebPreFormattedTextToColumns = True
        .WebConsecutiveDelimitersAsOne = True
        .WebSingleBlockTextImport = False
        .WebDisableDateRecognition = False
        .WebDisableRedirections = False
        .Refresh BackgroundQuery:=False
    End With

On Error GoTo 0

If xTempSheet.Range("A1").SpecialCells(xlLastCell).Row = 1 Then
    Application.DisplayAlerts = False
        xTempSheet.Delete
    Application.DisplayAlerts = True
    xCell.Offset(0, 1) = -1
    Exit Sub
End If

xHold = ""
On Error Resume Next
    For Each xHyper In xTempSheet.Hyperlinks
        If xHyper.ScreenTip = "Read the rest ..." Then
            xCount = xCount + 1
            xHold = xHold & xHyper.Name & Chr(10)
        End If
    Next
On Error GoTo 0

xCell.Offset(0, 1) = xCount
If Len(xHold) > 0 Then xCell.Offset(0, 2) = Mid(xHold, 1, Len(xHold) - 1)

Application.DisplayAlerts = False
    xTempSheet.Delete
Application.DisplayAlerts = True

End Sub

Open in new window

Regards,
Brian.Check-URL-s-IV.xlsm
0
Office 365 Training for IT Pros

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

 
LVL 11

Author Comment

by:bsharath
ID: 38729745
Can i get those URL's in separate cells in column "B"
0
 
LVL 11

Author Comment

by:bsharath
ID: 38729747
Sorry i mean "C"

Example
http://alturl.com/mseg7

row 2 to 11 i will have the URL's and then next
http://alturl.com/w5579

starts at 12th row
0
 
LVL 11

Author Comment

by:bsharath
ID: 38729751
0
 
LVL 26

Expert Comment

by:redmondb
ID: 38729754
bsharath,

To avoid rewriting the data in column A, is it OK if I write the data as a new sheet?

Thanks,
Brian.
0
 
LVL 11

Author Comment

by:bsharath
ID: 38729757
Yes please
0
 
LVL 26

Accepted Solution

by:
redmondb earned 500 total points
ID: 38729791
Please attached. The code is...
Option Explicit

Dim xDest As Worksheet
Dim xRows As Long

Sub Check_URLS()
Dim i           As Long
Dim xResult     As String
Dim xCell       As Range
Dim xLast_Row   As Long
Dim xSrce       As Worksheet

Set xDest = Sheets.Add
Set xSrce = Sheets("Check URL's")
xSrce.Activate

xLast_Row = Range("A1").SpecialCells(xlLastCell).Row

If xLast_Row < 2 Then
    MsgBox ("No data found - run cancelled.")
    Exit Sub
End If

xDest.Range("A1:B1") = Array("URL", "Links")
xRows = 1

Range("B2:C" & xLast_Row).ClearContents

For Each xCell In Range("A2:A" & xLast_Row)
    
    If xCell <> "" Then
        i = i + 1
        Call Get_URL(xCell)
    End If
    
    Application.ScreenUpdating = True
    
Next

End Sub

Sub Get_URL(xCell As Range)
Dim xTempSheet  As Worksheet
Dim xConnection As String
Dim xFound      As Range
Dim NextCell    As Range
Dim xHyper      As Hyperlink
Dim xUrl        As String
Dim xCount      As Long

Application.ScreenUpdating = False

xUrl = xCell

Set xTempSheet = Sheets.Add
xConnection = "x" & Format(Now(), "hhmmss")

On Error Resume Next

    With xTempSheet.QueryTables.Add(Connection:="URL;" & xUrl, Destination:=Range("$A$1"))
        .Name = xConnection
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .WebSelectionType = xlEntirePage
        .WebFormatting = xlWebFormattingAll
        .WebPreFormattedTextToColumns = True
        .WebConsecutiveDelimitersAsOne = True
        .WebSingleBlockTextImport = False
        .WebDisableDateRecognition = False
        .WebDisableRedirections = False
        .Refresh BackgroundQuery:=False
    End With

On Error GoTo 0

If xTempSheet.Range("A1").SpecialCells(xlLastCell).Row = 1 Then
    Application.DisplayAlerts = False
        xTempSheet.Delete
    Application.DisplayAlerts = True
    xCell.Offset(0, 1) = -1
    Exit Sub
End If

'On Error Resume Next
    For Each xHyper In xTempSheet.Hyperlinks
        If xHyper.ScreenTip = "Read the rest ..." Then
            xCount = xCount + 1
            xRows = xRows + 1
            xDest.Cells(xRows, 1).Formula = "=hyperlink(""" & xUrl & """, """ & xUrl & """)"
            xDest.Cells(xRows, 2) = "=hyperlink(""" & xHyper.Name & """, """ & xHyper.Name & """)"
        End If
    Next
On Error GoTo 0

xCell.Offset(0, 1) = xCount

Application.DisplayAlerts = False
    xTempSheet.Delete
Application.DisplayAlerts = True

End Sub

Open in new window

Regards,
Brian.Check-URL-s-IV.xlsm
0
 
LVL 11

Author Closing Comment

by:bsharath
ID: 38729796
Thanks a lot
0
 
LVL 26

Expert Comment

by:redmondb
ID: 38729856
Thanks, bsharath.
0
 
LVL 11

Author Comment

by:bsharath
ID: 38759604
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
This article describes how to import an Outlook PST file to Office 365 using a third party product to avoid Microsoft's Azure command line tool, saving you time.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question