Tuesday 28 May 2013

EXCEL - Get Values from a Cell by matching values from some other cell in same row


I found that there is a Vlookup function in Excel which returns the value from a column by comparing values from some other column. Followinf function also works like that. User have to enter following values
 
myfile = Excel File path

SheetName = Excel Sheet Name

Columnametocheck = Name of the source column

valtoCheck = Value to match

ColumnNameforReturnValues =   Will return the value from this coulmn


Ex.
getCellValues (myfile,mySheetname, "myColumn1", "B","myColumn3")
will search "B" in mycolumn1  and return the correspoding value from myColumn3 that is "2"

myColumn1
myColumn2
myColumn3
A
X
1
B
Y
2
C
Z
3



Function getCellValues (Excelfile,Sheetname, Columnametocheck, valtocheck, ColumnNameforReturnValues )
    returnvalue =""

   '--connecting Excel---
    set bdayExcel  =CreateObject("Excel.Application")
    bdayExcel.Visible = true
    bdayExcel.Workbooks.Open(Excelfile)

    '---connect with sheet --
    Set xlSheet =bdayExcel.Sheets(Sheetname)

   '-- get SourceColumn Number--
    colnumber = xlSheet.Range("A1:z7").find(Columnametocheck).column

   ' ---- Get Return Coulumn Number---
    returncolnumber = xlSheet.Range("A1:z7").find(ColumnNameforReturnValues).column


'--LOGIC---
    UsedRows= xlSheet.UsedRange.rows.count

    for i = 1 to usedRows
        val=xlSheet.Cells(i,colnumber).value
     
        if val = valtocheck then

            if len(returnvalue) = 0 then
                returnvalue = xlSheet.Cells(i,returncolnumber).value
             Else
                returnvalue =  returnvalue & "," & xlSheet.Cells(i,returncolnumber).value
            End If
        End IF
    Next


'---return found value---
getRowNumbers = returnvalue

End Function

Monday 20 May 2013

Print some charaters from a string

'Set printlength for total number of character to be displayed
printLength = 4

'Source String 
myString ="12345678ABCDEFGHIJ"

'Logic
stringLength = len(myString)

for i =0 to stringLength / printLength
     myAns = myAns & vbcrlf & mid(myString  ,i*printLength +1,printLength)
next
msgbox myAns


OUTPUT -  message box with these output
=======
1234 
5678
ABCD
EFGH
IJ

Lock Current Machine

'Create Shell Object
Set wsh= CreateObject("Wscript.Shell")


'Run user32.dll with argument Lockworkstation
wsh.run "rundll32.exe user32.dll, LockWorkStation"

Output
======

- Machine will be locked

Getting Emailid and Username from Outlook



'Creating outlook application object
Set olapp=createobject("Outlook.application") 

'Get Namespace
Set objNS =olApp.GetNamespace("MAPI")  


'Current EMail ID 
msgbox objNS.Session.CurrentUser.AddressEntry.GetExchangeUser.PrimarySmtpAddress

'Current UserName
msgbox objNS.Session.CurrentUser


Tuesday 14 May 2013

VBSCRIPT - GET LOGIN USERNAME

'Create Shell Object
Set shell = CreateObject("wscript.shell")

'Execute command Prompt(DOS) to get current username

Set oExec = shell.Exec("cmd.exe /c echo %username%")

'Print All output
msgbox oExec.Stdout.ReadAll
 
set oExec =nothing
set shell = nothing



OUTPUT
=======
 Current username will get displayed

VBSCRIPT - Get every information about each process




MAIN CODE

On Error Resume Next
strComputer = "."

' ---CREATE OBJECTS----
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_Process",,48)

'---GET TITLES OF ALL PROCESS AND DISPLAY---
Titles=""
For Each objItem in colItems    
Titles = Title1 & UCASE(objItem.Caption) & " , "
Next
msg = vbtab & "Process List" & vbcrlf & "-----------------------------------" & vbcrlf & Titles
msgbox msg
msg =""

'--- DISPLAY PROCESS DETAILS 1 BY 1 ----
Set colItems = objWMIService.ExecQuery("Select * from Win32_Process",,48)
For Each objItem in colItems    
msg = "Process : " & UCASE(objItem.Caption)
   msg = msg & vbcrlf & "--------------------------------------------------------------"
msg = msg & vbcrlf &  "CommandLine: " & objItem.CommandLine    
msg = msg & vbcrlf &  "CreationClassName: " & objItem.CreationClassName    
msg = msg & vbcrlf &  "CreationDate: " & objItem.CreationDate    
msg = msg & vbcrlf &  "CSCreationClassName: " & objItem.CSCreationClassName    
msg = msg & vbcrlf & "CSName: " & objItem.CSName    
msg = msg & vbcrlf & "Description: " & objItem.Description    
msg = msg & vbcrlf & "ExecutablePath: " & objItem.ExecutablePath    
msg = msg & vbcrlf & "ExecutionState: " & objItem.ExecutionState    
msg = msg & vbcrlf & "Handle: " & objItem.Handle    
msg = msg & vbcrlf & "HandleCount: " & objItem.HandleCount 
msg = msg & vbcrlf & "InstallDate: " & objItem.InstallDate    
msg = msg & vbcrlf & "KernelModeTime: " & objItem.KernelModeTime    
msg = msg & vbcrlf & "MaximumWorkingSetSize: " & objItem.MaximumWorkingSetSize    
msg = msg & vbcrlf & "MinimumWorkingSetSize: " & objItem.MinimumWorkingSetSize    
a= msgbox(msg & vbcrlf & vbcrlf & vbtab & "......continue with next process....?",vbYESNO)
if a= VBNO then
Exit For
End IF
Next

CODE  TO GET MORE INFORMATION

Wscript.Echo "Name: " & objItem.Name
    Wscript.Echo "OSCreationClassName: " & objItem.OSCreationClassName
    Wscript.Echo "OSName: " & objItem.OSName
    Wscript.Echo "OtherOperationCount: " & objItem.OtherOperationCount
    Wscript.Echo "OtherTransferCount: " & objItem.OtherTransferCount
    Wscript.Echo "PageFaults: " & objItem.PageFaults
    Wscript.Echo "PageFileUsage: " & objItem.PageFileUsage
    Wscript.Echo "ParentProcessId: " & objItem.ParentProcessId
    Wscript.Echo "PeakPageFileUsage: " & objItem.PeakPageFileUsage
    Wscript.Echo "PeakVirtualSize: " & objItem.PeakVirtualSize
    Wscript.Echo "PeakWorkingSetSize: " & objItem.PeakWorkingSetSize
    Wscript.Echo "Priority: " & objItem.Priority
    Wscript.Echo "PrivatePageCount: " & objItem.PrivatePageCount
    Wscript.Echo "ProcessId: " & objItem.ProcessId
    Wscript.Echo "QuotaNonPagedPoolUsage: " & objItem.QuotaNonPagedPoolUsage
    Wscript.Echo "QuotaPagedPoolUsage: " & objItem.QuotaPagedPoolUsage
    Wscript.Echo "QuotaPeakNonPagedPoolUsage: " & objItem.QuotaPeakNonPagedPoolUsage
    Wscript.Echo "QuotaPeakPagedPoolUsage: " & objItem.QuotaPeakPagedPoolUsage
    Wscript.Echo "ReadOperationCount: " & objItem.ReadOperationCount
    Wscript.Echo "ReadTransferCount: " & objItem.ReadTransferCount
    Wscript.Echo "SessionId: " & objItem.SessionId
    Wscript.Echo "Status: " & objItem.Status
    Wscript.Echo "TerminationDate: " & objItem.TerminationDate
    Wscript.Echo "ThreadCount: " & objItem.ThreadCount
    Wscript.Echo "UserModeTime: " & objItem.UserModeTime
    Wscript.Echo "VirtualSize: " & objItem.VirtualSize
    Wscript.Echo "WindowsVersion: " & objItem.WindowsVersion
    Wscript.Echo "WorkingSetSize: " & objItem.WorkingSetSize
    Wscript.Echo "WriteOperationCount: " & objItem.WriteOperationCount
    Wscript.Echo "WriteTransferCount: " & objItem.WriteTransferCount