Wednesday, March 9, 2016

Search Specific File in all Folders/SubFolders

Searching  ''CamRecorder.exe'' in C:\ drive


Sub SearchSpecificFile()
 Dim colFiles As New Collection
     RecursiveDir colFiles, "C:\", "CamRecorder.exe", True
     Dim vFile As Variant
     For Each vFile In colFiles
         MsgBox vFile
     Next vFile

End Sub
Public Function RecursiveDir(colFiles As Collection, _
                              strFolder As String, _
                              strFileSpec As String, _
                              bIncludeSubfolders As Boolean)
On Error Resume Next
     Dim strTemp As String
     Dim colFolders As New Collection
     Dim vFolderName As Variant
     'Add files in strFolder matching strFileSpec to colFiles
     strFolder = TrailingSlash(strFolder)
     strTemp = Dir(strFolder & strFileSpec)
     Do While strTemp <> vbNullString
         colFiles.Add strFolder & strTemp
         strTemp = Dir
     Loop
     If bIncludeSubfolders Then
         'Fill colFolders with list of subdirectories of strFolder
         strTemp = Dir(strFolder, vbDirectory)
         Do While strTemp <> vbNullString
             If (strTemp <> ".") And (strTemp <> "..") Then
                 If (GetAttr(strFolder & strTemp) And vbDirectory) <> 0 Then
                     colFolders.Add strTemp
                 End If
             End If
             strTemp = Dir
         Loop
         'Call RecursiveDir for each subfolder in colFolders
         For Each vFolderName In colFolders
             Call RecursiveDir(colFiles, strFolder & vFolderName, strFileSpec, True)
         Next vFolderName
     End If
End Function

Public Function TrailingSlash(strFolder As String) As String
     If Len(strFolder) > 0 Then
         If Right(strFolder, 1) = "\" Then
             TrailingSlash = strFolder
         Else
             TrailingSlash = strFolder & "\"
         End If
     End If
End Function



Wednesday, February 24, 2016

Press {Enter} key in InputBox of URL to enable Login

Sub loginWebWeX()

Dim htmldoc As HTMLDocument
Dim frm As Object
Dim objCollection As Object
Dim browser As InternetExplorer
Dim link As Object

Dim mainurl
Dim grandtotalinventory As Long
mainurl = ThisWorkbook.Sheets(1).Range("B1")
Set browser = New InternetExplorer
'On Error Resume Next
browser.Visible = True
browser.navigate mainurl
Do While browser.Busy Or browser.ReadyState <> READYSTATE_COMPLETE
DoEvents
   
Loop
  
    Set htmldoc = browser.document
    'Set objCollection = htmldoc.frames.Length
    j = 1
    i = 0
    Do
    Loop Until Not (browser.Busy)
   
   
     Set objCollection = htmldoc.frames(0).document.getElementsByTagName("a")
    
    
    
      
           For Each link In objCollection
                If link.innerHTML = "Log In" Then
                    link.Click
                End If
           Next
       Set htmldoc = browser.document
       Do
    Loop Until Not (browser.Busy)
    Set objCollection = htmldoc.frames(1).document.getElementsByTagName("input")
   
        k = 0
         While k < objCollection.Length
            If objCollection(k).Name = "userName" Then
                objCollection(k).Value = ThisWorkbook.Sheets(1).Range("A1")
                objCollection(k).Focus
                Application.SendKeys ("{ENTER}")
               
            ElseIf objCollection(k).Name = "password" Then
                objCollection(k).Value = ThisWorkbook.Sheets(1).Range("A2")
            ElseIf objCollection(k).ID = "mwx-btn-logon" Then
                Application.Wait (Now + TimeValue("0:00:03"))
                objCollection(k).Click
           
            End If
         k = k + 1
        Wend
       
      
    Set objCollection = Nothing
    Set objElement = Nothing
    Set htmldoc = Nothing
    Set browser = Nothing
    Exit Sub
errorhandler:
  MsgBox Err.Description
   


End Sub

Wednesday, February 17, 2016

Friday, January 15, 2016

VBA code.... insert query for new new records update for existing records

Dim conn As ADODB.Connection, rst As ADODB.Recordset, myarray
Dim rowcount As Long, updatequery As String
Dim apprisaldate1 As String, apprisaldate2 As String, apprisaldate3 As String, apprisaldate4 As String, apprisaldate5 As String, apprisaldate6 As String, apprisaldate7 As String, apprisaldate8 As String, apprisaldate9 As String, apprisaldate10 As String
Dim lastsalary1 As String, lastsalary2 As String, lastsalary3 As String, lastsalary4 As String, lastsalary5 As String, lastsalary6 As String, lastsalary7 As String, lastsalary8 As String, lastsalary9 As String, lastsalary10 As String
Dim revisedsalary1 As String, revisedsalary2 As String, revisedsalary3 As String, revisedsalary4 As String, revisedsalary5 As String, revisedsalary6 As String, revisedsalary7 As String, revisedsalary8 As String, revisedsalary9 As String, revisedsalary10 As String
Sub insertecord()
    Application.ScreenUpdating = False
    ThisWorkbook.Sheets(1).AutoFilterMode = False
   ' On Error GoTo errorHandler
    rowcount = ThisWorkbook.Sheets(1).Range("A" & Rows.Count).End(xlUp).Row
   
    Set conn = New ADODB.Connection
    Set rst = New ADODB.Recordset
    conn.ConnectionString = "Data Source=HRAutomation;Initial Catalog=KoenigDb;uid=sa;pwd=Pa$$w0rd;"
    conn.Open
    'rst.Open "EmpAppraisal", conn, adOpenKeyset, adLockBatchOptimistic, adCmdTable
    For i = 2 To rowcount
    'MsgBox "Emp Id" & ThisWorkbook.Sheets(1).Range("A" & i)
   
    Set rst = conn.Execute("Select * from EmpAppraisal where [Employee ID]='" & ThisWorkbook.Sheets(1).Range("A" & i) & "';")
    If Not rst.EOF Then
        myarray = rst.GetRows
        If UBound(myarray) > 0 Then
            apprisaldate1 = ThisWorkbook.Sheets(1).Range("B" & i)
            apprisaldate2 = ThisWorkbook.Sheets(1).Range("E" & i)
            apprisaldate3 = ThisWorkbook.Sheets(1).Range("H" & i)
            apprisaldate4 = ThisWorkbook.Sheets(1).Range("K" & i)
            apprisaldate5 = ThisWorkbook.Sheets(1).Range("N" & i)
            apprisaldate6 = ThisWorkbook.Sheets(1).Range("Q" & i)
            apprisaldate7 = ThisWorkbook.Sheets(1).Range("T" & i)
            apprisaldate8 = ThisWorkbook.Sheets(1).Range("W" & i)
            apprisaldate9 = ThisWorkbook.Sheets(1).Range("Z" & i)
            apprisaldate10 = ThisWorkbook.Sheets(1).Range("AC" & i)
           
            lastsalary1 = ThisWorkbook.Sheets(1).Range("C" & i)
            lastsalary2 = ThisWorkbook.Sheets(1).Range("F" & i)
            lastsalary3 = ThisWorkbook.Sheets(1).Range("I" & i)
            lastsalary4 = ThisWorkbook.Sheets(1).Range("L" & i)
            lastsalary5 = ThisWorkbook.Sheets(1).Range("O" & i)
            lastsalary6 = ThisWorkbook.Sheets(1).Range("R" & i)
            lastsalary7 = ThisWorkbook.Sheets(1).Range("U" & i)
            lastsalary8 = ThisWorkbook.Sheets(1).Range("X" & i)
            lastsalary9 = ThisWorkbook.Sheets(1).Range("AA" & i)
            lastsalary10 = ThisWorkbook.Sheets(1).Range("AD" & i)
           
           
            revisedsalary1 = ThisWorkbook.Sheets(1).Range("D" & i)
            revisedsalary2 = ThisWorkbook.Sheets(1).Range("G" & i)
            revisedsalary3 = ThisWorkbook.Sheets(1).Range("J" & i)
            revisedsalary4 = ThisWorkbook.Sheets(1).Range("M" & i)
            revisedsalary5 = ThisWorkbook.Sheets(1).Range("P" & i)
            revisedsalary6 = ThisWorkbook.Sheets(1).Range("S" & i)
            revisedsalary7 = ThisWorkbook.Sheets(1).Range("V" & i)
            revisedsalary8 = ThisWorkbook.Sheets(1).Range("Y" & i)
            revisedsalary9 = ThisWorkbook.Sheets(1).Range("AB" & i)
            revisedsalary10 = ThisWorkbook.Sheets(1).Range("AE" & i)
            updatequery = "Update EmpAppraisal set [Appraisal/Increment Date1]='" & apprisaldate1 & "',[Last Salary1]='" & lastsalary1 & "',[Revised Salary1]='" & revisedsalary1 & "',[Appraisal/Increment Date2]='" & apprisaldate2 & "',[Last Salary2]='" & lastsalary2 & "',[Revised Salary2]='" & revisedsalary2 & "',[Appraisal/Increment Date3]='" & apprisaldate3 & "',[Last Salary3]='" & lastsalary3 & "',[Revised Salary3]='" & revisedsalary3 & "',[Appraisal/Increment Date4]='" & apprisaldate4 & "',[Last Salary4]='" & lastsalary4 & "',[Revised Salary4]='" & revisedsalary4 & "',[Appraisal/increment Date5]='" & apprisaldate5 & "',[Last Salary5]='" & lastsalary5 & "',[Revised Salary5]='" & revisedsalary5 & "',[Appraisal/increment Date6]='" & apprisaldate6 & "',[Last Salary6]='" & lastsalary6 & "',[Revised Salary6]='" & revisedsalary6 & "',[Appraisal/increment Date7]='" & apprisaldate7 & "',[Last Salary7]='" & lastsalary7 & "',[Revised Salary7]='" & revisedsalary7 & "',[Appraisal/increment Date8]='" & apprisaldate8 _
& "',[Last Salary8]='" & lastsalary8 & "',[Revised Salary8]='" & revisedsalary8 & "',[Appraisal/increment Date9]='" & apprisaldate9 & "',[Last Salary9]='" & lastsalary9 & "',[Revised Salary9]='" & revisedsalary9 & "',[Appraisal/increment Date10]='" & apprisaldate10 & "',[Last Salary10]='" & lastsalary10 & "',[Revised Salary10]='" & revisedsalary10 & "' where [Employee ID]='" & ThisWorkbook.Sheets(1).Range("A" & i) & "';"
            conn.Execute (updatequery)
        Else
            apprisaldate1 = ThisWorkbook.Sheets(1).Range("B" & i)
            apprisaldate2 = ThisWorkbook.Sheets(1).Range("E" & i)
            apprisaldate3 = ThisWorkbook.Sheets(1).Range("H" & i)
            apprisaldate4 = ThisWorkbook.Sheets(1).Range("K" & i)
            apprisaldate5 = ThisWorkbook.Sheets(1).Range("N" & i)
            apprisaldate6 = ThisWorkbook.Sheets(1).Range("Q" & i)
            apprisaldate7 = ThisWorkbook.Sheets(1).Range("T" & i)
            apprisaldate8 = ThisWorkbook.Sheets(1).Range("W" & i)
            apprisaldate9 = ThisWorkbook.Sheets(1).Range("Z" & i)
            apprisaldate10 = ThisWorkbook.Sheets(1).Range("AC" & i)
           
            lastsalary1 = ThisWorkbook.Sheets(1).Range("C" & i)
            lastsalary2 = ThisWorkbook.Sheets(1).Range("F" & i)
            lastsalary3 = ThisWorkbook.Sheets(1).Range("I" & i)
            lastsalary4 = ThisWorkbook.Sheets(1).Range("L" & i)
            lastsalary5 = ThisWorkbook.Sheets(1).Range("O" & i)
            lastsalary6 = ThisWorkbook.Sheets(1).Range("R" & i)
            lastsalary7 = ThisWorkbook.Sheets(1).Range("U" & i)
            lastsalary8 = ThisWorkbook.Sheets(1).Range("X" & i)
            lastsalary9 = ThisWorkbook.Sheets(1).Range("AA" & i)
            lastsalary10 = ThisWorkbook.Sheets(1).Range("AD" & i)
           
           
            revisedsalary1 = ThisWorkbook.Sheets(1).Range("D" & i)
            revisedsalary2 = ThisWorkbook.Sheets(1).Range("G" & i)
            revisedsalary3 = ThisWorkbook.Sheets(1).Range("J" & i)
            revisedsalary4 = ThisWorkbook.Sheets(1).Range("M" & i)
            revisedsalary5 = ThisWorkbook.Sheets(1).Range("P" & i)
            revisedsalary6 = ThisWorkbook.Sheets(1).Range("S" & i)
            revisedsalary7 = ThisWorkbook.Sheets(1).Range("V" & i)
            revisedsalary8 = ThisWorkbook.Sheets(1).Range("Y" & i)
            revisedsalary9 = ThisWorkbook.Sheets(1).Range("AB" & i)
            revisedsalary10 = ThisWorkbook.Sheets(1).Range("AE" & i)
           
           
insertquery = "insert EmpAppraisal([Employee ID], [Appraisal/Increment Date1],[Last Salary1],[Revised Salary1],[Appraisal/Increment Date2],[Last Salary2],[Revised Salary2],[Appraisal/Increment Date3],[Last Salary3],[Revised Salary3],[Appraisal/Increment Date4],[Last Salary4],[Revised Salary4],[Appraisal/increment Date5],[Last Salary5],[Revised Salary5],[Appraisal/increment Date6],[Last Salary6],[Revised Salary6],[Appraisal/increment Date7],[Last Salary7],[Revised Salary7],[Appraisal/increment Date8],[Last Salary8],[Revised Salary8],[Appraisal/increment Date9],[Last Salary9],[Revised Salary9],[Appraisal/increment Date10],[Last Salary10],[Revised Salary10])" _
& "values('" & empId & "','" & apprisaldate1 & "','" & lastsalary1 & "','" & revisedsalary1 & "','" & apprisaldate2 & "','" & lastsalary2 & "','" & revisedsalary2 & "','" & apprisaldate3 & "','" & lastsalary3 & "','" & revisedsalary3 & "','" & apprisaldate4 & "','" & lastsalary4 & "','" & revisedsalary4 & "','" & apprisaldate5 & "','" & lastsalary5 & "','" & revisedsalary5 & "','" & apprisaldate6 & "'," _
& "'" & lastsalary6 & "','" & revisedsalary6 & "','" & apprisaldate7 & "','" & lastsalary7 & "','" & revisedsalary7 & "','" & apprisaldate8 _
& "','" & lastsalary8 & "','" & revisedsalary8 & "','" & apprisaldate9 & "','" & lastsalary9 & "','" & revisedsalary9 & "','" & apprisaldate10 & "','" & lastsalary10 & "','" & revisedsalary10 & "');"
            conn.Execute (insertquery)
            'ThisWorkbook.Sheets(1).Range("B4") = insertquery
        End If
    Else
            empId = ThisWorkbook.Sheets(1).Range("A" & i)
            apprisaldate1 = ThisWorkbook.Sheets(1).Range("B" & i)
            apprisaldate2 = ThisWorkbook.Sheets(1).Range("E" & i)
            apprisaldate3 = ThisWorkbook.Sheets(1).Range("H" & i)
            apprisaldate4 = ThisWorkbook.Sheets(1).Range("K" & i)
            apprisaldate5 = ThisWorkbook.Sheets(1).Range("N" & i)
            apprisaldate6 = ThisWorkbook.Sheets(1).Range("Q" & i)
            apprisaldate7 = ThisWorkbook.Sheets(1).Range("T" & i)
            apprisaldate8 = ThisWorkbook.Sheets(1).Range("W" & i)
            apprisaldate9 = ThisWorkbook.Sheets(1).Range("Z" & i)
            apprisaldate10 = ThisWorkbook.Sheets(1).Range("AC" & i)
           
            lastsalary1 = ThisWorkbook.Sheets(1).Range("C" & i)
            lastsalary2 = ThisWorkbook.Sheets(1).Range("F" & i)
            lastsalary3 = ThisWorkbook.Sheets(1).Range("I" & i)
            lastsalary4 = ThisWorkbook.Sheets(1).Range("L" & i)
            lastsalary5 = ThisWorkbook.Sheets(1).Range("O" & i)
            lastsalary6 = ThisWorkbook.Sheets(1).Range("R" & i)
            lastsalary7 = ThisWorkbook.Sheets(1).Range("U" & i)
            lastsalary8 = ThisWorkbook.Sheets(1).Range("X" & i)
            lastsalary9 = ThisWorkbook.Sheets(1).Range("AA" & i)
            lastsalary10 = ThisWorkbook.Sheets(1).Range("AD" & i)
           
           
            revisedsalary1 = ThisWorkbook.Sheets(1).Range("D" & i)
            revisedsalary2 = ThisWorkbook.Sheets(1).Range("G" & i)
            revisedsalary3 = ThisWorkbook.Sheets(1).Range("J" & i)
            revisedsalary4 = ThisWorkbook.Sheets(1).Range("M" & i)
            revisedsalary5 = ThisWorkbook.Sheets(1).Range("P" & i)
            revisedsalary6 = ThisWorkbook.Sheets(1).Range("S" & i)
            revisedsalary7 = ThisWorkbook.Sheets(1).Range("V" & i)
            revisedsalary8 = ThisWorkbook.Sheets(1).Range("Y" & i)
            revisedsalary9 = ThisWorkbook.Sheets(1).Range("AB" & i)
            revisedsalary10 = ThisWorkbook.Sheets(1).Range("AE" & i)
           
           
insertquery = "insert EmpAppraisal([Employee ID], [Appraisal/Increment Date1],[Last Salary1],[Revised Salary1],[Appraisal/Increment Date2],[Last Salary2],[Revised Salary2],[Appraisal/Increment Date3],[Last Salary3],[Revised Salary3],[Appraisal/Increment Date4],[Last Salary4],[Revised Salary4],[Appraisal/increment Date5],[Last Salary5],[Revised Salary5],[Appraisal/increment Date6],[Last Salary6],[Revised Salary6],[Appraisal/increment Date7],[Last Salary7],[Revised Salary7],[Appraisal/increment Date8],[Last Salary8],[Revised Salary8],[Appraisal/increment Date9],[Last Salary9],[Revised Salary9],[Appraisal/increment Date10],[Last Salary10],[Revised Salary10])" _
& "values('" & empId & "','" & apprisaldate1 & "','" & lastsalary1 & "','" & revisedsalary1 & "','" & apprisaldate2 & "','" & lastsalary2 & "','" & revisedsalary2 & "','" & apprisaldate3 & "','" & lastsalary3 & "','" & revisedsalary3 & "','" & apprisaldate4 & "','" & lastsalary4 & "','" & revisedsalary4 & "','" & apprisaldate5 & "','" & lastsalary5 & "','" & revisedsalary5 & "','" & apprisaldate6 & "'," _
& "'" & lastsalary6 & "','" & revisedsalary6 & "','" & apprisaldate7 & "','" & lastsalary7 & "','" & revisedsalary7 & "','" & apprisaldate8 _
& "','" & lastsalary8 & "','" & revisedsalary8 & "','" & apprisaldate9 & "','" & lastsalary9 & "','" & revisedsalary9 & "','" & apprisaldate10 & "','" & lastsalary10 & "','" & revisedsalary10 & "');"
            'ThisWorkbook.Sheets(1).Range("B4") = insertquery
            conn.Execute (insertquery)
           
   
   
    End If
  
   
       
    Next
     
           
           
       
        'End If
 'Next
End Sub


Download File

Tuesday, November 24, 2015

Select Query in For Loop using VBA

Dim conn As New ADODB.Connection
Dim rst As New ADODB.Recordset
Dim rowcount As Long, emprng As Range
Dim mycoll As Collection, i As Integer
Dim myArray(), rowcounter As Integer, counter As Integer
Sub updateclaimedtrainingDays()
rowcount = ThisWorkbook.Sheets(1).Range("D" & Rows.Count).End(xlUp).Row
Set emprng = ThisWorkbook.Sheets(1).Range("D2:D" & rowcount)
Set mycoll = New Collection
On Error Resume Next
'startdate = CDate("10/27/2015")
'endDate = CDate("10/28/2015")
conn.ConnectionString = "Data Source=TADA;Initial Catalog=Firdb;uid=kshrNew;pwd=A343jNMS;"
conn.Open
For Each cell In emprng
    mycoll.Add cell, CStr(cell)
Next
counter = 0
For i = 1 To mycoll.Count
        Set rst = conn.Execute("Select t2.DaysQty from tblTADATransDetail t2 where t2.TAId IN( Select t1.TAId  from tblTADATransMaster  t1 where (t1.CreatedDate >= '" & startdate & "' and t1.CreatedDate <='" & endDate & "') And t1.EmpId='" & mycoll(i) & "')and t2.NameOfExpenditure='1. Training Incentives'")
        If Not rst.EOF Then
            'cell.Offset(0, 16).CopyFromRecordset rst.Fields(4).Value
            myArray = rst.GetRows()
            rowcounter = UBound(myArray, 2)
            For j = 0 To rowcounter
            counter = counter + 1
           
                ThisWorkbook.Sheets(1).Range("T" & (1 + counter)) = myArray(0, j)
            Next
        Else
        counter = counter + 1
            ThisWorkbook.Sheets(1).Range("T" & (1 + counter)) = 0
        End If
Next
 rst.Close
Set rst = Nothing
Set conn = Nothing

End Sub

Wednesday, November 18, 2015

Add ValidationList in VBA

ThisWorkbook.Sheets(1).Range("B6").Validation.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Formula1:="=" & validationrng.Address

Thursday, October 8, 2015

Extracting Excel Cell value with Multple Lines

'extracting cell value with multiple lines
        If (InStr(clientname, Chr(10)) > 0) Then
            tempArray = Split(clientname, Chr(10))
            clientname = tempArray(0)
        End If

Thursday, September 17, 2015

Using adOpenStatic in Databse Connectivity(VBA)

Dim conn As New ADODB.Connection
Dim rst As New ADODB.Recordset, rowcount As Long
Dim sqlqyerystr As String, coulumncounter As Long, rowcounter As Long
Sub updaterecord()
'On Error Resume Next
Application.ScreenUpdating = False
rowcount = ThisWorkbook.Sheets(1).Range("A" & Rows.Count).End(xlUp).Row
ThisWorkbook.Sheets(1).Range("A2:S" & rowcount).ClearContents
conn.ConnectionString = "Data Source=TADA;Initial Catalog=Firdb;uid=kshr;pwd=A343jNMS;"
conn.Open
sqlqyerystr = "Select * from tblnewjoineeemployeedetail ;"

rst.Open sqlqyerystr, conn, adOpenStatic
Dim myArray()
myArray = rst.GetRows()
MsgBox "updating File for New Employee.........."
coulumncounter = UBound(myArray, 1)
rowcounter = UBound(myArray, 2)
For j = 0 To rowcounter
    For i = 0 To coulumncounter
   
        ThisWorkbook.Sheets(1).Range("A1").Offset(0, i).Value = rst.Fields(i).Name
        ThisWorkbook.Sheets(1).Range("A1").Offset(j + 1, i).Value = myArray(i, j)
       
    Next
Next
rst.Close
conn.Close
rowcount = ThisWorkbook.Sheets(1).Range("A" & Rows.Count).End(xlUp).Row
ThisWorkbook.Sheets(1).Range("A1:AM1").AutoFilter Field:=39, Criteria1:="<" & Format(Date, "m/d/yyyy")
ThisWorkbook.Sheets(1).Range("AM2:AM" & rowcount).SpecialCells(xlCellTypeVisible).EntireRow.Delete
ThisWorkbook.Sheets(1).AutoFilterMode = False
Set rst = Nothing
Set conn = Nothing
End Sub

Thursday, September 3, 2015

Autofilter Automation inbetween two Dates

Dim myArray(3) As String, lookuprng As Range
Public rowcount As Long, wb As Workbook
Public sheetname As String, newrowcount As Long, datarng As Range, tempval As Integer
Private Sub CommandButton1_Click()
''On Error Resume Next
tempval = 0
    For j = 0 To 3
   
    sheetname = Format(CDate(TextBox1.Value), "mmm") & "'" & Format(CDate(TextBox1.Value), "yy")
   
    rowcount = ThisWorkbook.Sheets(sheetname).Range("B" & Rows.Count).End(xlUp).Row
    Set lookuprng = ThisWorkbook.Sheets(sheetname).Range("A:A").Find(myArray(j), LookIn:=xlValues)
   
        If lookuprng Is Nothing Then
            ThisWorkbook.Sheets(sheetname).Range("A" & rowcount + 3) = myArray(j)
            ThisWorkbook.Sheets(sheetname).Range("B" & rowcount + 3) = TextBox1.Value & "-" & TextBox2.Value
            ThisWorkbook.Sheets(sheetname).Range("A" & (rowcount + 3) & ":B" & (rowcount + 3)).Font.Bold = True
        Exit For
        End If
    Next
    Set wb = Workbooks.Open(ThisWorkbook.Path & "\Onsite Report.xlsx")
    newrowcount = wb.Sheets(sheetname).Range("B" & Rows.Count).End(xlUp).Row
    i = 0
    wb.Sheets(sheetname).AutoFilterMode = False
    wb.Sheets(sheetname).Range("A1:T1").AutoFilter field:=3, Criteria1:=">=" & TextBox1.Value, Operator:=xlAnd, Criteria2:="<=" & TextBox2.Value
   
    Set datarng = wb.Sheets(sheetname).Range("B4:B" & newrowcount).SpecialCells(xlCellTypeVisible)
    For Each cell In datarng
        i = i + 1
        ThisWorkbook.Sheets(sheetname).Range("B" & (rowcount + 5 + i)) = cell
        ThisWorkbook.Sheets(sheetname).Range("C" & (rowcount + 5 + i)) = cell.Offset(0, 7)
        ThisWorkbook.Sheets(sheetname).Range("D" & (rowcount + 5 + i)) = cell.Offset(0, 10)
        ThisWorkbook.Sheets(sheetname).Range("E" & (rowcount + 5 + i)) = cell.Offset(0, 15)
        ThisWorkbook.Sheets(sheetname).Range("F" & (rowcount + 5 + i)) = cell.Offset(0, 6)
        ThisWorkbook.Sheets(sheetname).Range("G" & (rowcount + 5 + i)) = cell.Offset(0, 4)
         tempval = tempval + CInt(ThisWorkbook.Sheets(sheetname).Range("G" & (rowcount + 5 + i)))
    Next
    ThisWorkbook.Sheets(sheetname).Range("G" & (rowcount + 6 + i)) = tempval
    With ThisWorkbook.Sheets(sheetname).Range("B" & (rowcount + 6) & ":G" & (rowcount + 5 + i)).Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With ThisWorkbook.Sheets(sheetname).Range("B" & (rowcount + 6) & ":G" & (rowcount + 5 + i)).Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With ThisWorkbook.Sheets(sheetname).Range("B" & (rowcount + 6) & ":G" & (rowcount + 5 + i)).Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With ThisWorkbook.Sheets(sheetname).Range("B" & (rowcount + 6) & ":G" & (rowcount + 5 + i)).Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With ThisWorkbook.Sheets(sheetname).Range("B" & (rowcount + 6) & ":G" & (rowcount + 5 + i)).Borders(xlInsideVertical)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With ThisWorkbook.Sheets(sheetname).Range("B" & (rowcount + 6) & ":G" & (rowcount + 5 + i)).Borders(xlInsideHorizontal)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    Application.DisplayAlerts = False
    wb.Close
    Unload Me
End Sub
Private Sub UserForm_Initialize()
On Error Resume Next
'Application.ScreenUpdating = False
TextBox1.Value = Format(Date, "d-mmm-yy")
TextBox2.Value = Format(Date, "d-mmm-yy")

myArray(0) = "Week 1"
myArray(1) = "Week 2"
myArray(2) = "Week 3"
myArray(3) = "Week 4"

End Sub

Download File

Tuesday, August 4, 2015

Adding Controls Dynamically in User Form

Private Sub ComboBox1_Change()
Dim searchval As String, thelbl As Object, thelbl1 As Object, txtbox As Object
Application.ScreenUpdating = False
Application.EnableEvents = False
On Error Resume Next
ThisWorkbook.Sheets(2).Range("B2") = ComboBox1.Value
For Each cont In Me.Controls
    Me.Controls.Remove cont.Name
Next
Application.Calculation = xlCalculationAutomatic
If (Application.WorksheetFunction.CountIf(ThisWorkbook.Sheets(2).Range("E:E"), ">0") + 1) > 2 Then
    Set myrng1 = ThisWorkbook.Sheets(2).Range("E2:E" & Application.WorksheetFunction.CountIf(ThisWorkbook.Sheets(2).Range("E:E"), ">0") + 1)
Else
    Set myrng1 = ThisWorkbook.Sheets(2).Range("E2:E3")
End If
ComboBox1.List = myrng1.Value
cellrow = CLng(Application.WorksheetFunction.Match(CInt(ComboBox1.Value), ThisWorkbook.Sheets(1).Range("F:F"), 0))
Set lookupadd = ThisWorkbook.Sheets(1).Range("F3:F" & ThisWorkbook.Sheets(1).Cells(Rows.Count, 6).End(xlUp).Row).Find(Application.WorksheetFunction.Trim(ComboBox1.Value), LookIn:=xlValues)

    Set lblName = Existingemp.Controls.Add("Forms.Label.1", "Name", True)
    Set lblNameval = Existingemp.Controls.Add("Forms.Label.1", "Name", True)
    Set lblStatus = Existingemp.Controls.Add("Forms.Label.1", "Status", True)
    Set lblResignationDate = Existingemp.Controls.Add("Forms.Label.1", "ResignDate", True)
    Set txtBoxResignationDate = Existingemp.Controls.Add("Forms.TextBox.1", "ResignDate", True)
    Set lblNoticeDuration = Existingemp.Controls.Add("Forms.Label.1", "NoticeDuration", True)
    Set txtBoxNoticeDuration = Existingemp.Controls.Add("Forms.TextBox.1", "NoticeDuration", True)
    Set lblRelvDate = Existingemp.Controls.Add("Forms.Label.1", "RelvDate", True)
    Set txtBoxRelvDate = Existingemp.Controls.Add("Forms.TextBox.1", "RelvDate", True)
    Set lblOffLast = Existingemp.Controls.Add("Forms.Label.1", "OffLast", True)
    Set txtBoxOffLast = Existingemp.Controls.Add("Forms.TextBox.1", "OffLast", True)
    Set lblOffEmailIdStatus = Existingemp.Controls.Add("Forms.Label.1", "OffEmailIdStatus", True)
    Set txtBoxOffEmailIdStatus = Existingemp.Controls.Add("Forms.TextBox.1", "OffEmailIdStatus", True)
    Set lblHandoverAssetData = Existingemp.Controls.Add("Forms.Label.1", "HandoverAssetData", True)
    Set cmbboxHandoverAssetData = Existingemp.Controls.Add("Forms.ComboBox.1", "HandoverAssetData", True)
    Set lblExitInterview = Existingemp.Controls.Add("Forms.Label.1", "ExitInterview", True)
    Set cmbExitInterview = Existingemp.Controls.Add("Forms.ComboBox.1", "ExitInterview", True)
    Set lblReleivingLetterStatus = Existingemp.Controls.Add("Forms.Label.1", "ReleivingLetterStatus", True)
    Set cmbReleivingLetterStatus = Existingemp.Controls.Add("Forms.ComboBox.1", "ReleivingLetterStatus", True)
    Set lblFandF = Existingemp.Controls.Add("Forms.Label.1", "FandF", True)
    Set cmbFandF = Existingemp.Controls.Add("Forms.ComboBox.1", "FandF", True)
    Set lblRemark = Existingemp.Controls.Add("Forms.Label.1", "Remark", True)
    Set txtRemark = Existingemp.Controls.Add("Forms.TextBox.1", "Remark", True)
    Set cmbbox = Existingemp.Controls.Add("Forms.ComboBox.1", "cmbbox", True)
   
   
        With lblName
            .BackColor = RGB(141, 180, 226)
            .Caption = "Name"
            .Font.Bold = True
            .Left = 124
            .Width = 36
            .Top = 7
            .BorderStyle = 1
            .Height = 16
        End With
        With lblNameval
           
            .BackColor = RGB(141, 180, 226)
            .Font.Bold = True
            .Caption = ThisWorkbook.Sheets(1).Range(lookupadd.Address).Offset(0, -3) & " " & ThisWorkbook.Sheets(1).Range(lookupadd.Address).Offset(0, -2) & " " & ThisWorkbook.Sheets(1).Range(lookupadd.Address).Offset(0, -1)
            .Left = 160
            .Width = 127
            .Top = 7
            .BorderStyle = 1
            .Height = 16
        End With
        With lblResignationDate
            .BackColor = RGB(255, 153, 102)
            .Caption = "Resignation Date"
            .Font.Bold = True
            .Left = 10
            .Width = 75
            .Top = 55
            .BorderStyle = 1
            .Height = 15
        End With
        With txtBoxResignationDate
           
            .Value = Format(Date, "mm/dd/yyyy")
            .Font.Bold = True
            .Left = 85
            .Width = 75
            .Top = 55
            .BorderStyle = 1
            .Height = 15
        End With
        With lblNoticeDuration
            .BackColor = RGB(255, 153, 102)
            .Caption = "Notice Durartion"
            .Font.Bold = True
            .Left = 10
            .Width = 75
            .Top = 70.5
            .BorderStyle = 1
            .Height = 20
        End With
        With txtBoxNoticeDuration
           
           
            .Font.Bold = True
            .Left = 85
            .Width = 75
            .Top = 70.5
            .BorderStyle = 1
            .Height = 20
        End With
        With lblRelvDate
            .BackColor = RGB(255, 153, 102)
            .Caption = "Releieving Date"
            .Font.Bold = True
            .Left = 10
            .Width = 75
            .Top = 91.5
            .BorderStyle = 1
            .Height = 15
        End With
        With txtBoxRelvDate
           
            .Value = Format(Date + 60, "mm/dd/yyyy")
            .Font.Bold = True
            .Left = 85
            .Width = 75
            .Top = 91.5
            .BorderStyle = 1
            .Height = 15
        End With
        With lblOffLast
            .BackColor = RGB(255, 153, 102)
            .Caption = "Official Last Date"
            .Font.Bold = True
            .Left = 10
            .Width = 75
            .Top = 107
            .BorderStyle = 1
            .Height = 15
        End With
        With txtBoxOffLast
           
            .Value = Format(Date + 60, "mm/dd/yyyy")
            .Font.Bold = True
            .Left = 85
            .Width = 75
            .Top = 107
            .BorderStyle = 1
            .Height = 15
        End With
       
        With lblOffEmailIdStatus
            .BackColor = RGB(255, 153, 102)
            .Caption = "Off. Emailid Status"
            .Font.Bold = True
            .Left = 175
            .Width = 81
            .Top = 55
            .BorderStyle = 1
            .Height = 15
        End With
        With txtBoxOffEmailIdStatus
           
           
            .Font.Bold = True
            .Left = 256
            .Width = 220
            .Top = 55
            .BorderStyle = 1
            .Height = 15
        End With
        With lblHandoverAssetData
            .BackColor = RGB(255, 153, 102)
            .Caption = "Handover Asset & Data"
            .Font.Bold = True
            .Left = 175
            .Width = 81
            .Top = 70.5
            .BorderStyle = 1
            .Height = 20
        End With
        With cmbboxHandoverAssetData
           
            .List = Array("Yes", "No", "Not Required")
            .Font.Bold = True
            .Left = 256
            .Width = 40
            .Top = 70.5
            .BorderStyle = 1
            .Height = 20
        End With
        With lblExitInterview
            .BackColor = RGB(255, 153, 102)
            .Caption = "Exit Interview"
            .Font.Bold = True
            .Left = 175
            .Width = 81
            .Top = 91.5
            .BorderStyle = 1
            .Height = 15
        End With
        With cmbExitInterview
           
            .List = Array("Yes", "No", "Not Required")
            .Font.Bold = True
            .Left = 256
            .Width = 40
            .Top = 91.5
            .BorderStyle = 1
            .Height = 15
        End With
        With lblReleivingLetterStatus
            .BackColor = RGB(255, 153, 102)
            .Caption = "Releiving Letter"
            .Font.Bold = True
            .Left = 175
            .Width = 81
            .Top = 107
            .BorderStyle = 1
            .Height = 15
        End With
        With cmbReleivingLetterStatus
           
            .List = Array("Issued", "Pending", "Not Required")
            .Font.Bold = True
            .Left = 256
            .Width = 40
            .Top = 107
            .BorderStyle = 1
            .Height = 15
        End With
        With lblFandF
            .BackColor = RGB(255, 153, 102)
            .Caption = "F&F Status"
            .Font.Bold = True
            .Left = 10
            .Width = 75
            .Top = 122.5
            .BorderStyle = 1
            .Height = 15
        End With
        With cmbFandF
           
            .List = Array("Yes", "No", "On Hold", "Not Required")
            .Font.Bold = True
            .Left = 85
            .Width = 40
            .Top = 122.5
            .BorderStyle = 1
            .Height = 15
        End With
        With lblRemark
            .BackColor = RGB(255, 153, 102)
            .Caption = "Remark"
            .Font.Bold = True
            .Left = 175
            .Width = 81
            .Top = 122.5
            .BorderStyle = 1
            .Height = 15
        End With
        With txtRemark
           
           
            .Font.Bold = True
            .Left = 256
            .Width = 220
            .Top = 122.5
            .BorderStyle = 1
            .Height = 15
        End With
        With lblStatus
            .Caption = "Status"
            .Font.Bold = True
            .BackColor = RGB(255, 153, 102)
            .Left = 350
            .Width = 75
            .Top = 140
            .BorderStyle = 1
            .Height = 14.5
        End With
       
       
        With cmbbox
            .List = Array("Resigned")
            .Left = 425.5
            .Width = 51
            .Top = 140
            .BorderStyle = 1
            .Height = 14.5
           
        End With
Existingemp.Height = 180
Existingemp.Width = 481

Application.EnableEvents = True
End Sub

Thursday, May 28, 2015

Sending Emails with Image signature in Outlook


Dim outlukApp As Outlook.Application, outlukMailItm As Outlook.MailItem, emailid, myval, i As Integer, fullbody As String
Dim sigstring As String, signature As String
Sub sendMail()
    Set outlukApp = New Outlook.Application
    Set outlukMailItm = outlukApp.CreateItem(olMailItem)
    myval = Application.InputBox("Enter Mutiple  EmailId with ; delimiter", "EmailId")
    On Error Resume Next
    If myval = False Then
        Exit Sub
    Else
         emailid = Split(myval, ";")
        
       
         For i = 0 To UBound(emailid)
          fullbody = "Dear," & "<br>"
          fullbody = fullbody & "<p>Please find the attachment of new joinee details</p><br><br>"
          fullbody = fullbody & "<br> Best Regards,"
             With outlukMailItm
                     .Display
                     .To = emailid(i)
                    
                     .Subject = "New Joinee Details"
                     .HTMLBody = "Dear,<br>" & "<p>Please find the attachment of new joinee details</p><br><br><br> Best Regards," & .HTMLBody
                     .Attachments.Add (ThisWorkbook.Path & "\" & ThisWorkbook.Name)
                     .Display
                     Application.DisplayAlerts = False
                     .Send
             End With
            
            
         Next
     End If
    Set outlukMailItm = Nothing
    Set outlukApp = Nothing
   
End Sub

Saturday, May 23, 2015

Copy filtered Data with Criteria

Dim rowcount As Long, myrng As Range, cell
Sub copyFilteredData()
rowcount = ThisWorkbook.Sheets(1).Cells(Rows.Count, 2).End(xlUp).Row
Set myrng = ThisWorkbook.Sheets(1).Range("B2:B" & rowcount)
For Each cell In myrng
        If cell.Value Like "*LF*" Then
            cell.Offset(0, 1) = 0
        Else
            cell.Offset(0, 1) = 1
        End If
Next
ThisWorkbook.Sheets(1).Range("A1:B" & rowcount).AutoFilter field:=2, Criteria1:="*LF*"
ThisWorkbook.Sheets(1).Range("A1:B" & rowcount).SpecialCells(xlCellTypeVisible).Copy
ThisWorkbook.Sheets("Lost").Range("A" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteAll
ThisWorkbook.Sheets(1).ShowAllData
ThisWorkbook.Sheets(1).Range("A1:B" & rowcount).AutoFilter field:=2, Criteria1:="<>*LF*"
ThisWorkbook.Sheets(1).Range("A1:B" & rowcount).SpecialCells(xlCellTypeVisible).Copy
ThisWorkbook.Sheets("State").Range("A" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteAll

End Sub

Download file
 

Monday, April 13, 2015

Autofit Column using VBA

Sub Hidealternatecolumns()

Dim first As Double
Dim last As Double

first = 4
last = 40

For i = first To last

If Cells(1, i).Column Mod 2 = 0 Then
        Cells(1, i).EntireColumn.Hidden = True
    End If
Next
Range("C8").CurrentRegion.Columns.AutoFit

End Sub


Sunday, April 12, 2015

Send mail with Condition

Dim outlukApp As Outlook.Application, outlukMail, daterng As Range, rowcount As Long, cell


Sub sendMail()
rowcount = ThisWorkbook.Sheets("Data").Range("I" & Rows.Count).End(xlUp).Row

Set daterng = ThisWorkbook.Sheets("Data").Range("I2:I" & rowcount)
For Each cell In daterng
Set outlukApp = New Outlook.Application
Set outlukMail = outlukApp.CreateItem(olMailItem)

        If (cell = Date) Then
         
                With outlukMail
             
                    .Display
                    .To = CStr(cell.Offset(0, 3))
                    .Subject = "Intimation Mail"
                    .HTMLBody = "Dear " & "<b>" & cell.Offset(0, 2) & "</b>,<br>        Please find the attachment of....."
                 
     
                End With
                Application.DisplayAlerts = False
                Application.Wait (Now + TimeValue("00:00:05"))
                'Application.SendKeys "%s"
        End If
        Set outlukApp = Nothing
        Set outlukMail = Nothing
     
Next


End Sub


Monday, February 2, 2015

Tuesday, November 4, 2014

Data Validation With rules


Private Sub Worksheet_Change(ByVal Target As Range)
        If Target.Address = "$C$2" Then
            Select Case Target.Value
                Case "All"
                                   
                       With ThisWorkbook.Sheets(1).Range("D2").Validation
                            .Delete
                            .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:="=allrng"
                       End With
                Case "X"
                        With ThisWorkbook.Sheets(1).Range("D2").Validation
                            .Delete
                            .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:="=xrng"
                       End With
                Case "Y"
                        With ThisWorkbook.Sheets(1).Range("D2").Validation
                            .Delete
                            .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:="=yrng"
                       End With
            End Select
        End If
End Sub


Download file:

Data Validation with rules

Friday, August 22, 2014

Sort Data based on count on the basis of VBA

Dim mycoll As Collection
Dim myrng As Range, assgnrng As Range, rowcount As Long, cell, i As Integer
Sub sortData()
On Error Resume Next

rowcount = ThisWorkbook.Sheets(1).Range("A1").End(xlDown).Row
Set assgnrng = ThisWorkbook.Sheets(1).Range("A1:A" & rowcount)
    For i = 1 To rowcount
        ThisWorkbook.Sheets(1).Range("E" & (i + 1)) = Application.WorksheetFunction.CountIfs(assgnrng, ThisWorkbook.Sheets(1).Range("A" & (i + 1)))
   
    Next
ThisWorkbook.Sheets(1).Range("A1:E" & rowcount).Copy ThisWorkbook.Sheets(2).Range("A1")
ThisWorkbook.Sheets(2).Range("A2:E" & rowcount).Sort key1:=ThisWorkbook.Sheets(2).Range("E2"), order1:=xlDescending
End Sub






Download file

Thursday, August 7, 2014

Advancedfilter Using VBA













Sub testAdvancefilter()
'remove current filter
ThisWorkbook.Sheets("Summary").Range("B10").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Clear
'*********************************
ThisWorkbook.Sheets("Data").Range(ThisWorkbook.Sheets("data").Range("A1").CurrentRegion.Address).AdvancedFilter Action:=xlFilterCopy, criteriarange:=ThisWorkbook.Sheets("Data").Range("M1:P2"), copytorange:=ThisWorkbook.Sheets("Summary").Range("B10"), unique:=True
Columns.AutoFit
End Sub
















Download File

Sample file

Wednesday, May 7, 2014

Application.OnKey Example

Application.Onkey method runs a specified procedure when a particular key or key combination is pressed.


Sub test()
Call onKeyExample(True, True, True, "z", "myprocedure")

End Sub

Sub onKeyExample(shiftkey As Boolean, ctrlkey As Boolean, altkey As Boolean, strkey As String, callfunction As String)
Dim strShift As String
Dim strCtrl As String
Dim stralt As String
    If (shiftkey = True) Then strShift = "+"
    If (ctrlkey = True) Then strCtrl = "^"
    If (altkey = True) Then stralt = "%"
 
 
    Application.OnKey strShift & strCtrl & stralt & "{" & strkey & "}", callfunction
End Sub

Sub myprocedure()
    MsgBox "My Procedure"

End Sub