Friday, March 2, 2012

To open Excel or CSV from another Excel macro

Here is the code:

Filt = "Excel Files (*.xlsx),*.xlsx,Excel Files (*.xls),*.xls"
FilterIndex = 5
Title = "File 1"
Filename = Application.GetOpenFilename(FileFilter:=Filt, _
FilterIndex:=FilterIndex, Title:=Title)
 Workbooks.Open (FileName)



 To get the file name


P = WorksheetFunction.Substitute(FileName, Left(FileName, InStrRev(FileName, "\")), "")

Validate URL using Excel Macro

Here is the that you can use whether the set of URLs are valid or not valid:

Sub Validate_URLs()
Dim r As Range
Dim URLs As String
For Each r In ActiveSheet.Range("A2:A5146")
URLs = Trim(r.Value)
If Len(URLs) > 0 Then
r.Offset(0, 1).Value = IIf(HttpExists(URLs), "Valid", "Not Valid")
End If
Next r
End Sub
Function HttpExists(sURL As String) As Boolean
Dim oXHTTP As Object
Set oXHTTP = CreateObject("MSXML2.XMLHTTP")
oXHTTP.Open "HEAD", sURL, False
oXHTTP.send
HttpExists = (oXHTTP.Status = 200)
End Function