Friday, August 23, 2013

Use Split in VBA

Sub useSplitinVBA()
Dim myarry1 As Variant
Dim i As Integer
On Error Resume Next
myarry1 = Split(Sheet1.Cells(1, 1).Value, "\")
    For i = LBound(myarry1) To UBound(myarry1)
 
        If WorksheetFunction.Find(",", myarry1(i)) > 0 Then
        myarry2 = Split(myarry1(i), ",")
            For k = LBound(myarry2) To UBound(myarry2)
            MsgBox myarry2(k)
            Next
        End If
 
    Next
End Sub

Using Resize Property to Change the size of a range


The Resize property enables you to change the size of a range based on the location of the active cell.
You can create a new range as you need it.

For Example

Sub rngresize()

    Set Rng = Range("B1:B16").Find(What:="0", LookAt:=xlWhole, LookIn:=xlValues)
 
    Rng.Offset(, -1).Resize(, 2).Interior.ColorIndex = 15


End Sub


https://docs.google.com/file/d/0B23eJ2xd9ODySGNBdEhITWNKeHc/edit?usp=sharing

Tuesday, August 20, 2013

Print Userform in Landscape Format


Private Declare Sub keybd_event Lib "user32" _
(ByVal bVk As Byte, _
ByVal bScan As Byte, _
ByVal dwFlags As Long, _
ByVal dwExtraInfo As Long)

'dwFlags parameter of keybd_event controls various aspects of function operation. _

'This parameter can be one or more of the following values.

'KEYEVENTF_KEYUP
'if specified, the key is being released.If not specified, the key is being depressed.

'KEYEVENTF_EXTENDEDKEY
'If specified, the scan code was preceded by a prefix _
'byte having the value 0xE0 (224).

Private Const KEYEVENTF_KEYUP = &H2
Private Const KEYEVENTF_EXTENDEDKEY = &H1
'Print Screen key
Private Const VK_SNAPSHOT = &H2C
'Alt Key
Private Const VK_MENU = &H12
'Left Alt Key
Private Const VK_LMENU = &HA4

Private Sub CommandButton1_Click()
    Dim wshTemp As Worksheet
    DoEvents

    ' Simulate pressing ALT+Printscreen to copy the form window (=picture) to
    ' the clipboard
    keybd_event VK_LMENU, 0, KEYEVENTF_EXTENDEDKEY, 0
    keybd_event VK_SNAPSHOT, 0, KEYEVENTF_EXTENDEDKEY, 0
    keybd_event VK_SNAPSHOT, 0, KEYEVENTF_EXTENDEDKEY + KEYEVENTF_KEYUP, 0
    keybd_event VK_LMENU, 0, KEYEVENTF_EXTENDEDKEY + KEYEVENTF_KEYUP, 0
    DoEvents

    ' Add a worksheet named Temp
    ThisWorkbook.Worksheets.Add
    ActiveSheet.Name = "Temp"
    Set wshTemp = ThisWorkbook.Worksheets("Temp")

    ' Paste the picture, set print orientation to landscape en print it
    With wshTemp
     .Paste
     .PageSetup.Orientation = xlLandscape
     .PrintOut
    End With

    ' Delete the worksheet Temp and suppress the not-saved Warning.
    Application.DisplayAlerts = False
    ThisWorkbook.Worksheets("Temp").Delete
    Application.DisplayAlerts = True
End Sub

Sunday, August 18, 2013

Searching Files of All Format in a Folder

Option Explicit
Dim pathname As String
Dim i As Integer
Dim fso As Object
Dim folder1 As Object
Dim file1 As Object
Sub getFileDetailsunderSpecifiedFolder()
pathname = Application.InputBox("Provide path of specified Folder", "Pathname", Type:=2)
i = 2
Set fso = New Scripting.FileSystemObject
Set folder1 = fso.GetFolder(pathname)
    For Each file1 In folder1.Files
        Cells(i, 1) = file1.Name
        Cells(i, 2) = Environ("Username")
        Cells(i, 3) = file1.DateLastAccessed
        Cells(i, 4) = file1.DateLastModified
   
        i = i + 1
   
    Next
End Sub


https://docs.google.com/file/d/0B23eJ2xd9ODyampNbDRmOEd3R3c/edit?usp=sharing


Thursday, August 8, 2013

Compare Strings in Cases Insensitive Cases

When you compare 2 Strings for Case insensitive cases use
Option Compare Text at the top of the sub procedure.

One small example

Option Compare Text

Sub check()
If ("A" = "a") Then

MsgBox "Case insensitive"
End If
End Sub

Wednesday, August 7, 2013

Listing of File Names with Different Folders



Public pathname As String
Public fileformat As String
Dim strfile As String
Dim rowcount As Long
'Parameters are passed from Forms
Sub ListFiles()

rowcount = 2
strfile = Dir(pathname & "\" & fileformat, vbNormal)
    If Len(strfile) = 0 Then
            MsgBox "No file Exists", vbOKOnly

    End If
    Application.ScreenUpdating = False
    Do While Len(strfile) > 0
 
        Cells(rowcount, 3) = strfile
        Cells(rowcount, 4) = FileDateTime(pathname & "\" & strfile)
        rowcount = rowcount + 1
        'get nextfile from Folder
        strfile = Dir
    Loop
    Columns.AutoFit
End Sub

Convert Excel File To PDF




Sub Excel2PDFConverter()
With Application.FileDialog(msoFileDialogFolderPicker)
              .Show
              Path = .SelectedItems(1)
End With
              ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, Filename:=Path & "\" & "exceltopdf"
End Sub

Creating a Folder Inside SubFolder


'take reference of Microsoft Scripting Runtime
Sub createFolderinsideSubFolder()
Dim fso As Scripting.FileSystemObject
Dim parentfolder As Object
Dim subfolder As Object
Dim myfolder As String

Set fso = CreateObject("Scripting.FileSystemObject")
myfolder = "D:\Somu\"
Set parentfolder = fso.GetFolder(myfolder)
For Each subfolder In parentfolder.SubFolders
'searching all SubFolders inside D:\Somu\
myfolder = subfolder.Path & "\2013"
    If Not fso.FolderExists(myfolder) Then
        MkDir (myfolder)
    End If
Next

End Sub

Friday, August 2, 2013

ConnectionString Briefing(Used in SQL Server connectivity)

In computing, a Connectionstring is a string that specifies information about a data source and the means of connecting to it. It is passed in code to an underlying driver or provider in order to initiate the connection. Whilst commonly used for a database connection, the data source could also be a spreadsheetor text file.  ConnectionString property can be set only when the connection is closed. The connection string is parsed immediately after being set. If errors in syntax are found when parsing, a runtime exception, such asArgumentException, is generated. Other errors can be found only when an attempt is made to open the connection.
The connection string may include attributes such as the name of the driver, server and database, as well as security information such as user name and password.
For Reference:
www.connectionstrings.com

For example:
 con_string = "DRIVER={MySQL ODBC 3.51 Driver};user=internalros;password=internalros;database=TBS; server=206.71.169.000;option=18475"
ConnectionString = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=True;Data Source=NEW\SQLEXPRESS;"

ODBC-it is designed for connecting to relational databases.
However, OLE DB can access relational databases as well as nonrelational databases.
List of Parameters for Microsoft OLE-DB Provider:

https://docs.google.com/file/d/0B23eJ2xd9ODyZDg2ZFdCZUM1MWM/edit?usp=sharing