August 07, 2008, 07:41:28 PM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length

There are currently 0" users in chat
News: Don't forget to give or take karma if you were helped. Not giving karma will make people less likely to assist you in the future.
 
 
  Website   Home   Help Search Affiliate Chat Calendar Members Tags Links Gallery Media Center Login Register  
G³ Solutions Network
In short, the goal of G³ Solutions is in the title. We attempt to define technology in easily understood terms.
From the end-user to the entrenched and battle scarred professional - we all have questions. And answers.
We attempt to answer these questions - in a round-about fashion - as this: "How can technology help?"
MAIN SITE BLOG Main Site Search HOSTING PRIVACY CONTACT ABOUT



Digg This!
Subject Statistics
Topic: VBA (Visual Basic for Applications) examples Replies: 0 posts
Read 478 times 0 Members and 1 Guest are viewing this topic.
Pages: [1]   Go Down
  Reply  |  New Topic  |  Send this topic  |  Print  
Author Topic: VBA (Visual Basic for Applications) examples  (Read 478 times)
 
0 Members and 1 Guest are viewing this topic.
4fit?
Full Member
***

Karma: +2/-0
Offline Offline

Mood:

Gender: Male
Posts: 36


Gemini   White Dog - Loves HeartElectric

Topic starter
That guy.......4fit?


View Profile WWW
« on: February 02, 2007, 03:58:09 PM »

If you don’t know what VBA (Visual Basic for Applications) is, it is a child of Visual Basic that is physically built in to many programs manufactured today, including Microsoft Office.  To access the VBA editor, select ToolsMacroVisual Basic Editor from the Excel window, or simply press Alt+F11.  This will open the VBA Editor in a new window.  From here, you can insert your code into separate modules or one large one (which I don’t recommend).  Then you can call your routines from the ToolsMacroMacros window.

Sounds complicated right?  Trust me, it isn’t.  It is well worth it to learn how to use VBA.  If you can’t write your own code, that’s ok.  But you should at least be able to launch other code it if has been written by someone else.

Also, there is actually a file built into Excel to store macros for your personal use.  It is aptly called PERSONAL.XLS.  This file does not exist until you create a macro.  So, to create a bogus macro, go to ToolsMacroRecord New Macro.  In the dialog box that popups up, select Personal Macro Workbook from the Store In dropdown box and click OK.  When the macro toolbar appears, click the stop button.  That’s it.  Your PERSONAL.XLS file has been created.  Now you can place your code into that workbook in the VBA Editor and it will be available to you anytime Excel is open.  Simply go to ToolsMacroMacros and pick your desired macro.

Enough rambling. Here are a few Excel VBA subroutines that I have come up with that I use on a very regular basis.

Add full workbook name (including path) to footer
Code:
Sub CustomFooter()
    For Each sht In ActiveWorkbook.Sheets
        sht.PageSetup.LeftFooter = ActiveWorkbook.FullName
    Next sht
End Sub

Delete all empty rows without having to re-sort
Code:
Sub DeleteEmptyRows()
    LastRow = ActiveSheet.UsedRange.Row - 1 + _
        ActiveSheet.UsedRange.Rows.Count
    Application.ScreenUpdating = False
    For r = LastRow To 1 Step -1
        If Application.CountA(Rows(r)) = 0 Then Rows(r).Delete
    Next r
End Sub

Sort by the background color of the active cells column
Code:
Sub ColorSorter()
    y = ActiveCell.Column - 1
    J = Range("IV1").End(xlToLeft).Column
    BotRow = Range("A65536").Offset(0, y).End(xlUp).Row - 1

    Range("A1").Offset(0, J) = "Sort"
    For i = 1 To BotRow
        Range("A1").Offset(i, J) = Range("A1").Offset(i, y).Interior.ColorIndex
    Next
    Cells.Sort Key1:=Range("A1").Offset(0, J), Order1:=xlAscending, Header:=xlYes, _
        OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
    Columns(J + 1).Delete
End Sub

Quickly get a unique count of the active column without having to use Advanced Filter
Code:
Sub UniqueCount()
    Dim Uval As New Collection
    On Error Resume Next
    If Selection.Cells.Count > 1 Then
        For Each MyRange In Selection
            Uval.Add Trim(MyRange), CStr(Trim(MyRange))
        Next
    Else
        SortCol = ColumnLetter(ActiveCell.Column)
        For i = 1 To Range(SortCol & "65536").End(xlUp).Row
            Uval.Add Trim(Range(SortCol & i)), CStr(Trim(Range(SortCol & i)))
        Next
    End If
    MsgBox Uval.Count
End Sub

Move minus (negative) sign from back of number to front
Code:
Sub MoveMinusSign()
    Dim BotRow As Long
    Dim i As Long
    BotRow = Range("A1").Offset(65535, ActiveCell.Column - 1).End(xlUp).Row
    For i = 1 To BotRow - 1
        If Len(Range("A1").Offset(i, ActiveCell.Column - 1)) > 0 Then
            If Mid(Range("A1").Offset(i, ActiveCell.Column - 1), Len(Range("A1").Offset(i, ActiveCell.Column - 1)), 1) = "-" Then _
                Range("A1").Offset(i, ActiveCell.Column - 1) = Mid(Range("A1").Offset(i, ActiveCell.Column - 1), 1, Len(Range("A1").Offset(i, ActiveCell.Column - 1)) - 1) * -1
                Range("A1").Offset(i, ActiveCell.Column - 1).NumberFormat = "0.00_);[Red](0.00)"
        End If
    Next
End Sub

Delete Empty Columns
Code:
Sub DeleteEmptyCols()
    LastCol = ActiveSheet.UsedRange.Column - 1 + _
        ActiveSheet.UsedRange.Columns.Count
    Application.ScreenUpdating = False
   
    For c = LastCol To 1 Step -1
        If IsEmpty(Cells(1, c)) And Cells(1, c).End(xlDown).Row = 65536 Then _
            Cells(1, c).EntireColumn.Delete
    Next c
    Application.ScreenUpdating = True
   
   
End Sub

Create a clickable Table Of Contents sheet for your workbook
Code:
Sub Sheet_TOC()
   
    Dim ws As Worksheet, wsNw As Worksheet, n As Integer
    Set wsNw = ActiveWorkbook.Worksheets.Add(Before:=ThisWorkbook.Sheets(1))
   
    With wsNw
        On Error GoTo 2
1:      .Name = "TOC"
        On Error GoTo 0
        .[A1] = "Table Of Contents"
        .[a2] = ActiveWorkbook.Name & " Worksheets"
        .[A1].Font.Size = 15
        .[a2].Font.Size = 11
        n = 4
        For Each ws In ActiveWorkbook.Worksheets
            If ws.Name <> .Name Then
                .Cells(n, 1) = ws.Name
                 .Hyperlinks.Add _
                    Anchor:=.Cells(n, 1), _
                    Address:="", _
                    SubAddress:="'" & ws.Name & "'!A1"
                n = n + 1
            End If
        Next
    End With
   
    Columns("A:A").EntireColumn.AutoFit
   
    Exit Sub

2:  Application.DisplayAlerts = False
    Sheets("TOC").Delete
    Application.DisplayAlerts = True
    GoTo 1

Error1:
    MsgBox "No workbook open", vbCritical, "Error"
   
End Sub
Report to moderator   Logged

Mustangs Of Burlington Admin

Specialize in MS Office Automation
G³ Solutions - Technology Defined
« on: February 02, 2007, 03:58:09 PM »
Reply with quoteQuote


 Logged
Tags:
Pages: [1]   Go Up
  Reply  |  New Topic  |  Send this topic  |  Print  
 
Jump to:  

+ Quick Reply
With a Quick-Reply you can use bulletin board code and smileys as you would in a normal post, but much more conveniently.

Reminder:
Why not introduce yourself or register?
Powered by SMF 1.1.4 | SMF © 2006-2008, Simple Machines LLC | Sitemap
This page was magically conjured in about 0.144 seconds with 34 spell components. No animals were harmed in the making of this page.

Google visited last this page Yesterday at 05:10:25 PM