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

        Else

            Application.EnableEvents = False

            NewVal = Target.Value

            Application.Undo

           OldVal = Target.Value

            Target.Value = NewVal

            If OldVal <> "" Then

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

            End If

        End If

    End If

End Select

 

exitHandler:

  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
            Else
                Set rColored = Union(rColored, rCell)
            End If
        End If
    Next
    If rColored Is Nothing Then
        MsgBox "No cells match the color"
    Else
        rColored.Select
        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


Friday, September 5, 2014

Macro to hide/unhide menu bars in Excel

The 2 macros below are what can be used to show your toolbar, remove all native toolbars and most importantly restore them back when done;


Sub RemoveToolbars()

    On Error Resume Next

        With Application

           .DisplayFullScreen = True

           .CommandBars("Full Screen").Visible = False

           .CommandBars("MyToolbar").Enabled = True

           .CommandBars("MyToolbar").Visible = True

           .CommandBars("Worksheet Menu Bar").Enabled = False

        End With

    On Error GoTo 0

End Sub

------------------------------------------------------------------------------

Sub RestoreToolbars()

    On Error Resume Next

        With Application

           .DisplayFullScreen = False

           .CommandBars("MyToolbar").Enabled = False

           .CommandBars("Worksheet Menu Bar").Enabled = True

        End With

    On Error GoTo 0

End Sub