raison d'etre

I have often found bits and snippets of code on the web that I have incorporated into things I'm working on. I take no responsibility for these code samples. Most of them have been laboriously gleaned off of the web, but they're real handy to have quick access to. So bookmark this and enjoy...

JovialJohn

Thursday, May 13, 2010

A Test

Sub Do_The_Recon() ' Dim x As Variant
'Used to hold array of filenames
Dim i As Integer
'Used with x for position in array
Dim J As Integer Dim strFName As String strPath = "\\rgfs\inventory\hall\"
'strFName = ActiveWorkbook.Name
x = GetFileList(strPath & "2010*.xls") iCol = 1 J = 1 Application.DisplayAlerts = False Select Case IsArray(x)
'Application.AlertBeforeOverwriting = False
Case True 'files found For i = LBound(x) To UBound(x) Workbooks.Open FileName:=strPath & x(i) strFName = ActiveWorkbook.Name
' Detail code of process here
ActiveWorkbook.Save ActiveWorkbook.Close Next i Case False 'no files found End Select Beep
' Application.Visible = True
End Sub

Thursday, May 6, 2010

Excel File Formats

You can change the default file save setting, to do that choose Office, Excel Options, then click the Save tab in the dialog box. From there a drop-down list will let you select the file format that you want to use aa a default. This is handy if you work in a mixed environment of Excel 2000, 2003 and 2007 versions. If your default is save as 2003 then everyone in your organization will be able to use the file.

Format
Extension
Description
Excel Workbook
.xlsx
The default Office Excel 2007 XML-based file format. Cannot store Microsoft Visual Basic for Applications (VBA) macro code or Microsoft Office Excel 4.0 macro sheets (.xlm).
Excel Workbook (code)
.xlsm
The Office Excel 2007 XML-based and macro-enabled file format. Stores VBA macro code or Excel 4.0 macro sheets (.xlm).
Excel Binary Workbook
.xlsb
The Office Excel 2007 Binary file format (BIFF12).
Template
.xltx
The default Office Excel 2007 file format for an Excel template. Cannot store VBA macro code or Excel 4.0 macro sheets (.xlm).
Template (code)
.xltm
The Office Excel 2007 macro-enabled file format for an Excel template. Stores VBA macro code or Excel 4.0 macro sheets (.xlm).
Excel 97- Excel 2003 Workbook
.xls
The Excel 97 - Excel 2003 Binary file format (BIFF8).
Excel 97- Excel 2003 Template
.xlt
The Excel 97 - Excel 2003 Binary file format (BIFF8) for an Excel template.
Microsoft Excel 5.0/95 Workbook
.xls
The Excel 5.0/95 Binary file format (BIFF5).
XML Spreadsheet 2003
.xml
XML Spreadsheet 2003 file format (XMLSS).
XML Data
.xml
XML Data format.
Excel Add-In
.xlam
The Office Excel 2007 XML-based and macro-enabled Add-In, a supplemental program that is designed to run additional code. Supports the use of VBA projects and Excel 4.0 macro sheets (.xlm).
Excel 97-2003 Add-In
.xla
The Excel 97-2003 Add-In, a supplemental program that is designed to run additional code. Supports the use of VBA projects.
Excel 4.0 Workbook
.xlw
An Excel 4.0 file format that saves only worksheets, chart sheets, and macro sheets. You can open a workbook in this file format in Office Excel 2007, but you cannot save an Excel file to this file format.

Tuesday, May 4, 2010

Set Zoom

Sometimes you might want to change the zoom on the page, this one is really easy.

Sheets("Sheet1").Select Range("A1").Select ActiveWindow.Zoom = 75

I arbitrarily select A1 to show from the top of the spreadsheet.

Find the last used row in a Column:

Sub LastRowInOneColumn() Dim LastRow As Long With ActiveSheet LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row End With MsgBox LastRow End Sub

Some Useful Functions

The FileExists Function

Private Function FileExists(fname) As Boolean
' Returns TRUE if the file exists
Dim x As String
x = Dir(fname)
If x <> "" Then FileExists = True _
Else FileExists = False
End Function

The FileNameOnly Function
Private Function FileNameOnly(pname) As String
' Returns the filename from a path/filename string
Dim i As Integer, length As Integer, temp As String length = Len(pname) temp = "" For i = length To 1 Step -1 If Mid(pname, i, 1) = Application.PathSeparator Then FileNameOnly = temp Exit Function End If temp = Mid(pname, i, 1) & temp Next i FileNameOnly = pname End Function

The PathExists Function
Private Function PathExists(pname) As Boolean
' Returns TRUE if the path exists
Dim x As String On Error Resume Next x = GetAttr(pname) And 0 If Err = 0 Then PathExists = True Else PathExists = False End Function

The RangeNameExists Function
Private Function RangeNameExists(nname) As Boolean
' Returns TRUE if the range name exists
Dim n As Name RangeNameExists = False For Each n In ActiveWorkbook.Names If UCase(n.Name) = UCase(nname) Then RangeNameExists = True Exit Function End If Next n End Function

The SheetExists Function
Private Function SheetExists(sname) As Boolean
' Returns TRUE if sheet exists in the active workbook
Dim x As Object On Error Resume Next Set x = ActiveWorkbook.Sheets(sname) If Err = 0 Then SheetExists = True Else SheetExists = False End Function