Thursday, April 14, 2016

Code to Break Password Protected Sheet

Option Explicit




Private Declare Sub MoveMemory Lib "kernel32" Alias "RtlMoveMemory" _

        (Destination As Long, Source As Long, ByVal Length As Long)


Private Declare Function VirtualProtect Lib "kernel32" (lpAddress As Long, _

        ByVal dwSize As Long, ByVal flNewProtect As Long, lpflOldProtect As Long) As Long


Private Declare Function GetModuleHandleA Lib "kernel32" (ByVal lpModuleName As String) As Long


Private Declare Function GetProcAddress Lib "kernel32" (ByVal hModule As Long, _

        ByVal lpProcName As String) As Long


Private Declare Function DialogBoxParam Lib "user32" Alias "DialogBoxParamA" (ByVal hInstance As Long, _

        ByVal pTemplateName As Long, ByVal hWndParent As Long, _

        ByVal lpDialogFunc As Long, ByVal dwInitParam As Long) As Integer


Dim HookBytes(0 To 5) As Byte

Dim OriginBytes(0 To 5) As Byte

Dim pFunc As Long

Dim Flag As Boolean


Private Function GetPtr(ByVal Value As Long) As Long

    GetPtr = Value

End Function


Public Sub RecoverBytes()

    If Flag Then MoveMemory ByVal pFunc, ByVal VarPtr(OriginBytes(0)), 6

End Sub


Public Function Hook() As Boolean

    Dim TmpBytes(0 To 5) As Byte

    Dim p As Long

    Dim OriginProtect As Long


    Hook = False


    pFunc = GetProcAddress(GetModuleHandleA("user32.dll"), "DialogBoxParamA")



    If VirtualProtect(ByVal pFunc, 6, PAGE_EXECUTE_READWRITE, OriginProtect) <> 0 Then


        MoveMemory ByVal VarPtr(TmpBytes(0)), ByVal pFunc, 6

        If TmpBytes(0) <> &H68 Then


            MoveMemory ByVal VarPtr(OriginBytes(0)), ByVal pFunc, 6


            p = GetPtr(AddressOf MyDialogBoxParam)


            HookBytes(0) = &H68

            MoveMemory ByVal VarPtr(HookBytes(1)), ByVal VarPtr(p), 4

            HookBytes(5) = &HC3


            MoveMemory ByVal pFunc, ByVal VarPtr(HookBytes(0)), 6

            Flag = True

            Hook = True

        End If

    End If

End Function


Private Function MyDialogBoxParam(ByVal hInstance As Long, _

        ByVal pTemplateName As Long, ByVal hWndParent As Long, _

        ByVal lpDialogFunc As Long, ByVal dwInitParam As Long) As Integer

    If pTemplateName = 4070 Then

        MyDialogBoxParam = 1



        MyDialogBoxParam = DialogBoxParam(hInstance, pTemplateName, _

                           hWndParent, lpDialogFunc, dwInitParam)


    End If

End Function


Sub unprotected()

    If Hook Then

        MsgBox "VBA Project is unprotected!", vbInformation, "*****"

    End If

End Sub

Tuesday, May 12, 2015

VBA for Ctrl+Home

Cells(ActiveWindow.SplitRow + 1, ActiveWindow.SplitColumn + 1).Select

Sunday, May 3, 2015

Multiple Selection in data validation

Private Sub Worksheet_Change(ByVal Target As Range)

Dim RngDV As Range

Dim OldVal As String

Dim NewVal As String


If Target.Count > 1 Then GoTo exitHandler


Select Case Target.Column

  Case 5        'this Case line works for column B only

    'Case 2, 5, 6 'this Case line works for multiple columns

    If Cells(Target.Row, 4) = "In" Or Cells(Target.Row, 3) = "Not In" Then

        On Error Resume Next

        'check the cell for data validation

        Set RngDV = Target.SpecialCells(xlCellTypeAllValidation)

        On Error GoTo exitHandler

        If RngDV Is Nothing Then GoTo exitHandler


        If Intersect(Target, RngDV) Is Nothing Then

           'do nothing


            Application.EnableEvents = False

            NewVal = Target.Value


           OldVal = Target.Value

            Target.Value = NewVal

            If OldVal <> "" Then

              If NewVal <> "" Then Target.Value = OldVal & ", " & NewVal

            End If

        End If

    End If

End Select



  Application.EnableEvents = True

End Sub

Sunday, October 26, 2014

Finding Cells Filled with a Particular Color

Finding Cells Filled with a Particular Color

  1. Press Ctrl+F to display the Find tab of the Find and Replace dialog box. (See Figure 1.)
  2. Figure 1. The Find tab of the Find and Replace dialog box.
  3. Make sure there is nothing in the Find What box.
  4. Click Format. (You may need to click Options to see the Format button.) Excel displays the Find Format dialog box.
  5. Make sure the Patterns tab is displayed. (See Figure 2.)
  6. Figure 2. The Patterns tab of the Find Format dialog box.
  7. From the colors available, choose the color you want to find.
  8. Click OK to close the Find Format dialog box.
  9. Click Find All. The Find and Replace dialog box expands to show the addresses of all the cells formatted with the color you specified in step 5. (See Figure 3.)
  10. Figure 3. The expanded Find and Replace dialog box.
  11. Click one of the cell addresses in the bottom of the dialog box. Excel selects the cell within the actual worksheet.
  12. Press Ctrl+A. All of the addresses within the dialog box are selected.
  13. Click Close. All the cells of the desired color are selected.
If you are using Excel 97, Excel 2000, or Excel 2002 the only way to select cells of a particular color is to use a macro. Consider the macro shown here:
Sub SelectColoredCells()
    Dim rCell As Range
    Dim lColor As Long
    Dim rColored As Range

    'Select the color by name (8 possible)
    'vbBlack, vbBlue, vbGreen, vbCyan,
    'vbRed, vbMagenta, vbYellow, vbWhite
    lColor = vbBlue

    'If you prefer, you can use the RGB function
    'to specify a color
    'lColor = RGB(0, 0, 255)

    Set rColored = Nothing
    For Each rCell In Selection
        If rCell.Interior.Color = lColor Then
            If rColored Is Nothing Then
                Set rColored = rCell
                Set rColored = Union(rColored, rCell)
            End If
        End If
    If rColored Is Nothing Then
        MsgBox "No cells match the color"
        MsgBox "Selected cells match the color:" & _
            vbCrLf & rColored.Address
    End If
    Set rCell = Nothing
    Set rColored = Nothing
End Sub  
To use the macro, select a range of cells before running it. The macro then steps through each selected cell and compares its color with whatever color you specify in lColor. If a match is found, then the cell is added to a selection set. When completed, the macro selects only those matching cells, and then exits.

To Customize the Ribbon in Excel 2013

To Customize the Ribbon in Excel 2013

You can customize the Ribbon by creating your own tabs with whichever commands you want. Commands are always housed within a group, and you can create as many groups as you want in order to keep your tab organized. If you want, you can even add commands to any of the default tabs, as long as you create a custom group in the tab.

  1. Right-click the Ribbon and then select Customize the Ribbon... from the drop-down menu.
    Screenshot of Excel 2013
  2. The Excel Options dialog box will appear. Locate and select New Tab.
    Screenshot of Excel 2013
  3. Make sure the New Group is selected, select a command, then click Add. You can also drag commands directly into a group.
  4. When you are done adding commands, click OK. The commands will be added to the Ribbon.
    Screenshot of Excel 2013

If you don't see the command you want, click the Choose commands from: drop-down box and select All Commands.

Screenshot of Excel 2013

Tuesday, September 16, 2014

Find alphabets in a cell

Sub Get_Alpha()
For I = 1 To Range("A1").End(xlDown).Row
    Alp = ""
    For J = 1 To Len(Range("A" & I).Value)
        StrV = Range("A" & I).Value
        If Asc(Mid(StrV, J, 1)) >= 65 And Asc(Mid(StrV, J, 1)) <= 90 Then
            Alp = Alp & Mid(StrV, J, 1)
        ElseIf Asc(Mid(StrV, J, 1)) >= 97 And Asc(Mid(StrV, J, 1)) <= 122 Then
            Alp = Alp & Mid(StrV, J, 1)
        End If
    Next J
    Range("B" & I).Value = Alp
Next I
End Sub