VBA

My Photo
Name:
Location: Hyderabad, Andhra Pradesh, India

Software Engineer , professional expertise in J2ee

Tuesday, August 16, 2005

Converts the Excel column number to its string equivalent

'//Converts the Excel column number to its string equivalent

Function ColumnLetter(ColumnNumber As Integer) As String
If ColumnNumber > 26 Then
'//1st character: Subtract 1 to map the characters to 0-25,
' but you don't have to remap back to 1-26
' after the 'Int' operation since columns
' 1-26 have no prefix letter

'//2nd character: Subtract 1 to map the characters to 0-25,
' but then must remap back to 1-26 after
' the 'Mod' operation by adding 1 back in
' (included in the '65')

ColumnLetter = Chr(Int((ColumnNumber - 1) / 26) + 64) & _
Chr(((ColumnNumber - 1) Mod 26) + 65)

Else
'//Columns A-Z
ColumnLetter = Chr(ColumnNumber + 64)
End If
End Function

Cheaks the given file is there in specified location

'Cheaks the given file is there in specified location
Function DoesFileExist(strFileSpec As String) As Boolean
' Return True if file specified in the
' strFilespec argument exists.
' Return False if strFileSpec is not a valid
' file or if strFileSpec is a directory.
Const INVALID_ARGUMENT As Long = 53
On Error GoTo DoesfileExist_Err
If (GetAttr(strFileSpec) And vbDirectory) <> vbDirectory Then
DoesFileExist = CBool(Len(Dir(strFileSpec)) > 0)
Else
DoesFileExist = False
End If
DoesfileExist_End:
Exit Function
DoesfileExist_Err:
DoesFileExist = False
Resume DoesfileExist_End
End Function

Browse the file

'Browse the file
Sub BrowseNewFile()
Dim filename
Dim wksh As Excel.Worksheet

Set wksh = ActiveWorkbook.Worksheets("dashboard")

'Opens browse dialog box to browse the template
filename = Application.GetOpenFilename("Excel Files(*.xls), *.*", , "Select any file in the desired folder to catch the path.")
If Not filename = "" Then
wksh.Cells(9, 1).Value = filename
End If
End Sub