Deletes every other row in a Microsoft Excel worksheet.

Deletes every other row in a Microsoft Excel worksheet.

July 04, 2011

Deletes every other row in a Microsoft Excel worksheet.
PowerShell
$comments = @' 
Script name: Delete-EveryOtherRow.ps1 
Created on: Sunday, September 02, 2007 
Author: Kent Finkle 
Purpose: How can I use Windows Powershell to 
delete every other row on an Excel worksheet? 
#http://support.microsoft.com/kb/213610/en-us 
'@ 
#----------------------------------------------------- 
function Release-Ref ($ref) { 
([System.Runtime.InteropServices.Marshal]::ReleaseComObject( 
[System.__ComObject]$ref-gt 0) 
[System.GC]::Collect() 
[System.GC]::WaitForPendingFinalizers()  

#----------------------------------------------------- 
$xl = new-object -comobject excel.application 
$xl.Visible = $True 
$wb = $xl.Workbooks.Add()  
$ws = $wb.Worksheets.Item("Sheet1")  
$2d = new-object 'object[,]' 20,1  
# Fill an array so we can put the numbers into Excel all at once. 
for ($i=0; $i -le 19; $i++) { 
    $2d[$i,0] = $i + 1 

$r = $ws.Range("A1:A20")  
# Put the array into the sheet so we have something to work with. 
$r.Value() = $2d  
$y = $false               
# Change this to $True if you want to 
# delete rows 1, 3, 5, and so on. 
$i = 1 
$r = $ws.UsedRange 
$cnt = $r.rows.Count 
# Loop once for every row in the selection. 
for ($x=1; $x -le $cnt$x++) { 
    if ($y -eq $true) { 
        # ...delete an entire row of cells. 
        $a = $r.Cells.Item($i).EntireRow.Delete() 
    } 
    Else { 
        # ...increment $i by one so we can cycle through range. 
        $i++ 
    } 
    # If ($y is true, make it false; if $y is false, make it true.) 
    $y = -not($y

$a = Release-Ref($r
$a = Release-Ref($ws
$a = Release-Ref($wb
$a = Release-Ref($xl
Verified on the following platforms
 
Windows Server 2008 R2No
Windows Server 2008No
Windows Server 2003No
Windows 7No
Windows VistaNo
Windows XPNo
Windows 2000No
This script is tested on these platforms by the author. It is likely to work on other platforms as well. If you try it and find that it works on another platform, please add a note to the script discussion to let others know.
Import a Large Text File

Import a Large Text File

July 04, 2011

Imports a text file into Excel even if the number of lines in that file exceeds Excel's total number of rows limitation.



Visual Basic
'       This script was written for folks trying to import a text file into  
'       Excel 2003 that exceed the row limitations.  
'       This version works on Windows XP and has not been tested on any other OS.  
 
Const ForReading = 1  
Const ForAppending = 2  
 
Set objDialog = CreateObject("UserAccounts.CommonDialog")  
 
objDialog.Filter = "All Files|*.*"  
objDialog.InitialDir = "C:\"  
intResult = objDialog.ShowOpen  
   
If intResult = 0 Then  
    Wscript.Quit  
Else  
    BreakFile =  objDialog.FileName  
End If  
 
Set objFSO = CreateObject("Scripting.FileSystemObject")  
Set objFile = objFSO.OpenTextFile(BreakFile, ForReading)  
 
FiletoSplit = objFSO.GetFileName(BreakFile)  
FolderDest = Mid(objFSO.GetAbsolutePathName(BreakFile),1, _ 
    Len(objFSO.GetAbsolutePathName(BreakFile))-(Len(FiletoSplit)))  
FileSplitName = objFSO.GetBaseName(BreakFile)  
 
 
 
dtmStart = Now()  
strContents = objFile.ReadAll  
FileNum = 1  
fname =  FolderDest & FileSplitName & "Split_" & FileNum & ".txt"  
Set objFile1 = objFSO.OpenTextFile(fname, ForAppending, True)  
 
 
 
CountLines = 0  
arrLines = Split(strContents, vbCrLf)  
 
If ubound(arrLines) < 64500 Then  
        msgbox "This file will fit into Excel already.  No split is necessary.",48,"SplitFile"  
        Wscript.Quit  
End If  
 
        HeaderText = arrLines(0)  
                For i = 0 to ubound(arrlines)                    
                        strLine = arrLines(i) & vbCrLf                   
                        objFile1.Write strLine                   
                        If  (Countlines) < 64500  Then                           
                                countlines = countlines + 1                      
                        ElseIf Countlines >= 64500 Then  
                                objFile1.Close  
                                Countlines = 0                           
                                FileNum = FileNum + 1  
                                fname = FolderDest & FileSplitName & "Split_" & FileNum & ".txt"  
                                Set objFile1 = objFSO.OpenTextFile(fname, ForAppending, True)  
                                objFile1.Write HeaderText & vbCrLf                               
                        End If           
        Next  
            
objFile.Close  
dtmEnd = Now()  
If MsgBox("There were " & FileNum & " files created." & vbcrlf & _  
        "The files were put into this folder:  " & FolderDest & _  
        vbCrLf & "The script took " & DateDiff("s", dtmStart, dtmEnd) & " seconds " & _  
        "to break the " &  FiletoSplit & " file." & vbcrlf & vbcrLF & _  
        "Click OK to open destination folder or CANCEL to quit.",  _  
        1,"SplitFile") = vbOK Then  
        Set objShell = CreateObject("Shell.Application")  
        strPath = FolderDest  
 
        objShell.Explore strPath  
End If 
 
Verified on the following platforms
Windows Server 2008 R2No
Windows Server 2008No
Windows Server 2003No
Windows 7No
Windows VistaNo
Windows XPNo
Windows 2000No


This script is tested on these platforms by the author. It is likely to work on other platforms as well. If you try it and find that it works on another platform, please add a note to the script discussion to let others know.
Copy Data From One Spreadsheet to Another

Copy Data From One Spreadsheet to Another

July 04, 2011

Copies data from one Excel spreadsheet to another.
PowerShell
$comments = @'  
Script name: Copy-ExcelData.ps1  
Created on: Wednesday, August 22, 2007  
Author: Kent Finkle  
Purpose: How can I use Windows Powershell to  
Copy Data From One Spreadsheet to Another?  
'@  
# -----------------------------------------------------  
function Release-Ref ($ref) {  
([System.Runtime.InteropServices.Marshal]::ReleaseComObject(  
[System.__ComObject]$ref-gt 0)  
[System.GC]::Collect()  
[System.GC]::WaitForPendingFinalizers()  
}  
# -----------------------------------------------------  
$xl = new-object -comobject excel.application  
$xl.Visible = $True  
$wb = $xl.Workbooks.Add()  
$ws = $wb.Worksheets.Item("Sheet1")  
  
$2d = new-object 'object[,]' 20,1  
 
for ($i = 0; $i -le 19; $i++) {  
    for ($j = 0; $j -le 0; $j++) {  
        $2d[$i,$j] = $i  
    }  
}  
  
$r = $ws.Range("A1:A20")  
$r.Value() = $2d  
$a = $r.Copy()  
  
$xl2 = new-object -comobject excel.application  
$xl2.Visible = $True  
$wb2 = $xl2.Workbooks.Add()  
$ws2 = $wb2.Worksheets.Item("Sheet1")  
$a = $ws2.Paste()  
  
$a = Release-Ref($ws2)  
$a = Release-Ref($wb2)  
$a = Release-Ref($xl2)  
$a = Release-Ref($ws)  
$a = Release-Ref($wb)  
$a = Release-Ref($xl
Verified on the following platforms
 
Windows Server 2008 R2No
Windows Server 2008No
Windows Server 2003No
Windows 7No
Windows VistaNo
Windows XPNo
Windows 2000No
This script is tested on these platforms by the author. It is likely to work on other platforms as well. If you try it and find that it works on another platform, please add a note to the script discussion to let others know.
Delete duplicate rows in excel using advanced filter

Delete duplicate rows in excel using advanced filter

July 04, 2011
Tags: delete, Excel, duplicate, rows, filter, advanced filter


Visual Basic
'Delete duplicate rows in excel using advanced filter 
'Francis de la Cerna 
' 
'I've been doing this the long way for, well, a long time. 
'Sorry, excel, I didn't know you could do that. 
 
 
 
set xl  = createobject("excel.application"
set wb  = xl.workbooks.add 
set ws1 = wb.worksheets("sheet1"
set ws2 = wb.worksheets("sheet2"
 
xl.visible = true 
 
 
 
'add some data to sheet1 
 
ws1.range("a1:c1").value = array("a""b""c"
ws1.range("a2:c2").value = array( 1  , 2  , 3 ) 
ws1.range("a3:c3").value = array( 4  , 5  , 6 ) 
ws1.range("a4:c4").value = array( 7  , 8  , 9 ) 
ws1.range("a5:c5").value = array( 4  , 5  , 6 ) 
ws1.range("a6:c6").value = array( 1  , 2  , 3 ) 
ws1.range("a7:c7").value = array("a""b""c"
 
 
 
'filter unique records and copy result in sheet2 starting in cell a1 
'i think the 1st row is considered as a header row 
 
xlFilterInPlace = 1 
xlFilterCopy    = 2 
uniqueRecords   = true 
 
ws1.range("a1:c7").advancedFilter xlFilterCopy, , ws2.range("a1"), uniqueRecords 
Verified on the following platforms
 
Windows Server 2008 R2No
Windows Server 2008No
Windows Server 2003No
Windows 7No
Windows VistaNo
Windows XPYes
Windows 2000No
This script is tested on these platforms by the author. It is likely to work on other platforms as well. If you try it and find that it works on another platform, please add a note to the script discussion to let others know.
Computer Hardware Scripts: Retrieving System Information

Computer Hardware Scripts: Retrieving System Information

July 04, 2011
Retrieving System Information



INVENTORY OF A COMPUTER WITH WINDOWS
Pripared by Laxman,

Uses WMI to retrieve the same data found in the System Information applet.
======================



strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colSettings = objWMIService.ExecQuery _
    ("Select * from Win32_OperatingSystem")
For Each objOperatingSystem in colSettings 
    Wscript.Echo "OS Name: " & objOperatingSystem.Name
    Wscript.Echo "Version: " & objOperatingSystem.Version
    Wscript.Echo "Service Pack: " & _
        objOperatingSystem.ServicePackMajorVersion _
            & "." & objOperatingSystem.ServicePackMinorVersion
    Wscript.Echo "OS Manufacturer: " & objOperatingSystem.Manufacturer
    Wscript.Echo "Windows Directory: " & _
        objOperatingSystem.WindowsDirectory
    Wscript.Echo "Locale: " & objOperatingSystem.Locale
    Wscript.Echo "Available Physical Memory: " & _
        objOperatingSystem.FreePhysicalMemory
    Wscript.Echo "Total Virtual Memory: " & _
        objOperatingSystem.TotalVirtualMemorySize
    Wscript.Echo "Available Virtual Memory: " & _
        objOperatingSystem.FreeVirtualMemory
    Wscript.Echo "OS Name: " & objOperatingSystem.SizeStoredInPagingFiles
Next
Set colSettings = objWMIService.ExecQuery _
    ("Select * from Win32_ComputerSystem")
For Each objComputer in colSettings 
    Wscript.Echo "System Name: " & objComputer.Name
    Wscript.Echo "System Manufacturer: " & objComputer.Manufacturer
    Wscript.Echo "System Model: " & objComputer.Model
    Wscript.Echo "Time Zone: " & objComputer.CurrentTimeZone
    Wscript.Echo "Total Physical Memory: " & _
        objComputer.TotalPhysicalMemory
Next
Set colSettings = objWMIService.ExecQuery _
    ("Select * from Win32_Processor")
For Each objProcessor in colSettings 
    Wscript.Echo "System Type: " & objProcessor.Architecture
    Wscript.Echo "Processor: " & objProcessor.Description
Next
Set colSettings = objWMIService.ExecQuery _
    ("Select * from Win32_BIOS")
For Each objBIOS in colSettings 
    Wscript.Echo "BIOS Version: " & objBIOS.Version
Next

======================



Save the script as Filename.vbs, And just double click on that.
Computer Hardware Scripts: Retrieving BIOS Information

Computer Hardware Scripts: Retrieving BIOS Information

July 04, 2011

INVENTORY OF A COMPUTER WITH WINDOWS
Pripared by Laxman,

Retrieves BIOS information for a computer, including BIOS version number and release date.



======================
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colBIOS = objWMIService.ExecQuery _
    ("Select * from Win32_BIOS")
For each objBIOS in colBIOS
    Wscript.Echo "Build Number: " & objBIOS.BuildNumber
    Wscript.Echo "Current Language: " & objBIOS.CurrentLanguage
    Wscript.Echo "Installable Languages: " & objBIOS.InstallableLanguages
    Wscript.Echo "Manufacturer: " & objBIOS.Manufacturer
    Wscript.Echo "Name: " & objBIOS.Name
    Wscript.Echo "Primary BIOS: " & objBIOS.PrimaryBIOS
    Wscript.Echo "Release Date: " & objBIOS.ReleaseDate
    Wscript.Echo "Serial Number: " & objBIOS.SerialNumber
    Wscript.Echo "SMBIOS Version: " & objBIOS.SMBIOSBIOSVersion
    Wscript.Echo "SMBIOS Major Version: " & objBIOS.SMBIOSMajorVersion
    Wscript.Echo "SMBIOS Minor Version: " & objBIOS.SMBIOSMinorVersion
    Wscript.Echo "SMBIOS Present: " & objBIOS.SMBIOSPresent
    Wscript.Echo "Status: " & objBIOS.Status
    Wscript.Echo "Version: " & objBIOS.Version
    For i = 0 to Ubound(objBIOS.BiosCharacteristics)
        Wscript.Echo "BIOS Characteristics: " & _
            objBIOS.BiosCharacteristics(i)
    Next
Next


=========================

Save the script as Filename.vbs, And just double click on that.
Identifying Processor Type & Inventorying Computer Hardware

Identifying Processor Type & Inventorying Computer Hardware

July 04, 2011


INVENTORY OF A COMPUTER WITH WINDOWS
Pripared by Laxman,

Identifying Processor Type




Determines the processor architecture (such as x86 or ia64) for a specified computer.
========================


strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set objProcessor = objWMIService.Get("win32_Processor='CPU0'")

If objProcessor.Architecture = 0 Then
    Wscript.Echo "This is an x86 computer."
ElseIf objProcessor.Architecture = 1 Then
    Wscript.Echo "This is a MIPS computer."
ElseIf objProcessor.Architecture = 2 Then
    Wscript.Echo "This is an Alpha computer."
ElseIf objProcessor.Architecture = 3 Then
    Wscript.Echo "This is a PowerPC computer."
ElseIf objProcessor.Architecture = 6 Then
    Wscript.Echo "This is an ia64 computer."
Else
    Wscript.Echo "The computer type could not be determined."
End If
========================
Save the script as Filename.vbs, And just double click on that.

Inventorying Computer Hardware


Inventorying Computer Hardware



Returns information about the pointing devices installed on a computer. Used as an example of how to retrieve hardware information using WMI.
=========================


strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colMice = objWMIService.ExecQuery _
    ("Select * from Win32_PointingDevice")
For Each objMouse in colMice
    Wscript.Echo "Hardware Type: " & objMouse.HardwareType
    Wscript.Echo "Number of Buttons: " & objMouse.NumberOfButtons    
    Wscript.Echo "Status: " & objMouse.Status
    Wscript.Echo "PNP Device ID: " & objMouse.PNPDeviceID
Next
========================

Save the script as Filename.vbs, And just double click on that.