Thursday, September 22, 2011

Search selected cell from excel using your favorite browser and search engine

This macro will help you to search with the content of the selected cell from word doc directly in google or any other site easily.
Simply select the text and run the macro your search result will appear in your favorite browser.
Note:
1.Change the Path to your browser (firefox, IE, chrome etc.,) installation path.
2.To search other website change the Link to your favorite search engine.
3.Select only single cell.

Code:
*********************************************************************
Sub googlesearch()

Dim x As Variant
Dim Path As String
Dim Link As String
Dim TheTerm As String

TheTerm = excel.selection

    Path = "C:\Program Files\Mozilla Firefox\firefox.exe"

    'for internet explorer change the path to "C:\Program Files\Internet Explorer\iexplore.exe"

    Link = "http://www.google.com/search?num=20&hl=en&q=" & TheTerm
    'for searching with Bing change the Link to "http://www.bing.com/search?q="

    x = Shell(Path + " " + Link, vbNormalFocus)

End Sub
*********************************************************************

Tuesday, September 20, 2011

Search selected term from word doc using your favorite browser and search engine

This macro will help you to search the selected word from word doc directly in google or any other site easily.
No need to copy the text from word open google and paste the word in search box and search.
Its very simple select the text and run the macro your search result will appear in your favorite browser.

Note:
1.Change the Path to your browser (firefox, IE, chrome etc.,) installation path.
2.To search other website change the Link to your favorite search engine.
Code:

*********************************************************************
Sub googlesearch()

Dim x As Variant
Dim Path As String
Dim Link As String
Dim TheTerm As String

If Selection.Type = wdSelectionIP Then
TheTerm = Selection.Words(1).Text
Else
TheTerm = Selection.Text
End If
TheTerm = Trim(TheTerm)

    Path = "C:\Program Files\Mozilla Firefox\firefox.exe"

    'for internet explorer change the path to "C:\Program Files\Internet Explorer\iexplore.exe"

    Link = "http://www.google.com/search?num=20&hl=en&q=" & TheTerm
    'for searching with Bing change the Link to "http://www.bing.com/search?q="

    x = Shell(Path + " " + Link, vbNormalFocus)

End Sub
*********************************************************************

Log in to google or facebook account using excel





Login to Google Account using VBA


Have you ever thought of automating your login to google account or any other account using excel macros?
Hear is the simple macro to log in to google account.

Note: Change to your user name and password.
Code: For google account
****************************************************
Sub Login_google()
    
    Dim ie As Object
    
    Set ie = CreateObject("InternetExplorer.Application")
    
    ie.navigate "https://www.google.com/accounts/Login"
    
    ie.Visible = True
    
    Do While ie.Busy And Not ie.readyState = 4
        DoEvents
    Loop
    
    DoEvents
    
    ie.document.all.Item("Email").Value = "Rajendiran"
    ie.document.all.Item("passwd").Value = "******"
    ie.document.all.Item("signIn").Click
    
End Sub

****************************************************

 Code: For Facebook account

****************************************************
Sub Login_facebook()
    
    Dim ie As Object
    
    Set ie = CreateObject("InternetExplorer.Application")
    
    ie.navigate "http://www.facebook.com/login.php"
    
    ie.Visible = True
    
    Do While ie.Busy And Not ie.readyState = 4
        DoEvents
    Loop
    
    DoEvents
    
    ie.document.all.Item("email").Value = "rajendiran@mymail.com"
    ie.document.all.Item("pass").Value = "*******"
    ie.document.all.Item("login").Click
    
End Sub

****************************************************

Friday, September 16, 2011

Remove Duplicates From Selected Cells


This macro is useful for removing duplicates on the selected data.

*************************************************************************
Code:

Sub RemoveDuplicate()


Dim ConstRange As Range, FrmRange As Range
Dim FullRange As Range, rCell As Range
Dim iCount As Long
Dim strAdd As String

DialogStyle = vbYesNo + vbCritical + vbDefaultButton2
Title = "Deleting Duplicates"
Msg = "Are you sure you want to delete entire rows?"
deleteDupRows = MsgBox(Msg, DialogStyle, Title)

If deleteDupRows = vbYes Then
   
    On Error Resume Next

    Set FullRange = Selection
        If WorksheetFunction.CountA(FullRange) < 2 Then
            MsgBox "Select more than one cell", vbInformation
            On Error GoTo 0
            Exit Sub
        End If

    Set ConstRange = FullRange.SpecialCells(xlCellTypeConstants)
    Set FrmRange = FullRange.SpecialCells(xlCellTypeFormulas)

    If Not ConstRange Is Nothing And Not FrmRange Is Nothing Then
        Set FullRange = Union(ConstRange, FrmRange)
    ElseIf Not ConstRange Is Nothing Then
        Set FullRange = ConstRange
    ElseIf Not FrmRange Is Nothing Then
        Set FullRange = FrmRange
    Else
        MsgBox "Invalid Selection", vbInformation

        On Error GoTo 0
       
        Exit Sub
   
    End If

    Application.Calculation = xlCalculationManual

    For Each rCell In FullRange

        strAdd = rCell.Address
        strAdd = FullRange.Find(What:=rCell, After:=rCell, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False).Address

        If strAdd <> rCell.Address Then
            rCell.Clear
        End If

    Next rCell

End If
 

    Application.Calculation = xlCalculationAutomatic

    On Error GoTo 0

End Sub

*************************************************************************

Create Hyperlink for filenames or folder names from the cells in excel

This macro is to create hyperlinks to a list of file names in the excel to a specific folder containing the list of files

For example I have a folder containing thousands of subfolders and I have the list of sub folders present in that folder.
I have to specific folders and open it for which i would generally use to search the "Main folder" for the "Sub folder" and open it each time since the folder has thousands of sub folder the search takes a lot of time.
Here is a easy way to solve my problem.
I create a hyper link in excel to only the specific sub folders which i have to check and open them by clicking the hyperlink.


Note: Please take care that the the list of folder names you want to create hyperlink is present in "Sheet1" and in Column "A" or else you can modify the code according to your need.
Provide the Main folder path in the Input Box when it pop ups.

For any assistance please post me.

Here is the code for hyperlinking.

****************************************************************

Code:

Sub Create_Hyperlink()

Dim Path As String
Dim lastRow As Long
Dim rOffset As Long
Dim partialPath As String
Dim linkPath As String

Path = InputBox("Please paste the path to which the list should be hyperlinked")
 
  Const SheetToPutLinksOn = "Sheet1"
  Const ColumnWithFileNames = "A"
  Const firstFilenameRow = 1

  ThisWorkbook.Worksheets(SheetToPutLinksOn).Activate
  Application.ScreenUpdating = True
  partialPath = Path & "\"
  lastRow = Range(ColumnWithFileNames & Rows.Count).End(xlUp).Row - _
   firstFilenameRow
  Range(ColumnWithFileNames & firstFilenameRow).Select
  Application.ScreenUpdating = True
  For rOffset = 0 To lastRow
     If Not IsEmpty(ActiveCell.Offset(rOffset, 0)) Then
      linkPath = partialPath & ActiveCell.Offset(rOffset, 0).Text
      ActiveSheet.Hyperlinks.Add anchor:=ActiveCell.Offset(rOffset, 0), _
       Address:=linkPath
    End If
  Next
  ThisWorkbook.Saved = True
  Application.ScreenUpdating = True

End Sub

****************************************************************

Flip selected cells in excel (Macro)

This Macro will flip the selected cells in a row or column.

Sometimes we need to reverse the order of the cells in excel for which this macro will be helpful.

The code is given below which you can copy to your excel macro.

For those who don't know about macro, here is a step wise instruction.
1. Open the excel in which u want to reverse the cell order.
2. From excel tools menu select Macros and then visual basic editor. Or simply press Alt+F11 in excel which will open Microsoft Visual Basic editor.
3. Select the excel u want to save the macro form the project list and choose the "ThisWorkbook"
4. Copy the code form this page and paste it in the editor.
5. Save the project and close the Visual basic editor.
6. You can run the macro from the excel form Tools>Macro>Macros which will display you the list of macros present in the excel form which you can select Flip_Cells and press Run.

***********************************************************
Code:

Sub Flip_Cells()
Dim Arr() As Variant
Dim Range As Range
Dim i As Range
Dim Row As Long
Dim Column As Long
On Error GoTo EndMacro
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Application.EnableEvents = False
Set Range = Selection
Row = Selection.Rows.Count
Column = Selection.Columns.Count
If Row > 1 And Column > 1 Then
MsgBox "Must select either a range of rows or columns, but not simultaneaously columns and rows.", _
vbExclamation, "Reverse Rows or Columns"
Exit Sub
End If
If Range.Cells.Count = ActiveCell.EntireRow.Cells.Count Then
MsgBox "Can't select an entire row.", vbExclamation, _
"Reverse Rows or Columns"
Exit Sub
End If
If Range.Cells.Count = ActiveCell.EntireColumn.Cells.Count Then
MsgBox "Can't select an entire column.", vbExclamation, _
"Reverse Rows or Columns"
Exit Sub
End If
If Row > 1 Then
ReDim Arr(Row)
Else
ReDim Arr(Column)
End If
Row = 0
For Each i In Range
Arr(Row) = i.Formula
Row = Row + 1
Next i
Row = Row - 1
For Each i In Range
i.Formula = Arr(Row)
Row = Row - 1
Next i
EndMacro:
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Application.EnableEvents = True
End Sub
***********************************************************