Monday, May 26, 2008

Extract the RGB colour of a shape using VBA.

There are times when we want to replicate a Shapes RBG value in some other shape or at least know the Red, Green and Blue components distinctly.

Hear is function that will help us extract the RBG value of shape is any MS Office Application (Word, Excel, and PowerPoint).

The function returns two values, one as Text string and the other one as Hexadecimal value depending on the Boolean type you specify.

Paste the codes as below in any VBA mobule:







Function RBGExtract(myShape As Shape, IsHex As Boolean) As Variant

c = myShape.Fill.ForeColor.RGB

If IsHex = False Then

redComponent = c Mod 256

greenComponent = c \ 256 Mod 256

blueComponent = c \ 65536 Mod 256

RBGExtract = "RGB components: " & redComponent & _

", " & greenComponent & ", " & blueComponent

Else

RBGExtract = c

End If

End Function





Now, its time to test the function that we have created, we will test it in PPT through following subroutine:








Sub Test()

With ActivePresentation.Slides(1)

MsgBox RBGExtract(.Shapes(1), True), vbInformation,

'To test the Hexadecimal value
MsgBox RBGExtract(.Shapes(1), False), vbInformation, 'To test the Text value

End With

End Sub




Friday, January 25, 2008

Collecting Data from Multiple Excel Workbooks into a Single Text File using VBScript

Introduction: It’s often a very tedious ask to collect data through copy and pate from multiple MS Excel files. There are ways in VBA to automate the process, but unfortunately, some time ill spreadsheet practices like uses of merged cell may trigger cumbersome situations. Using Vbscript and DOS bat file we can automate this process in just few seconds.

Before we start with the cods, I am assuming that all the Excel files are in same folder as in my case it’s in “C:\Documents and Settings\ritwik.shukla\Desktop\Excel Training\Xl”
Now, we need to create one more folder for storing CSV files in example below the path is “C:\Documents and Settings\ritwik.shukla\Desktop\Excel Training\CSV\”. You need to change both these paths per your requirement.

Step One: Open Notepad paste the codes as below; make sure you change the CSV path per your requirement.






cd C:\Documents and Settings\ritwik.shukla\Desktop\Excel Training\CSV
copy *.xls all.txt
del *.xls



Step Two: Save this file in C drive as ConsolidateCSV.bat

Step Three: Open Notepad again paste codes as below; make sure you replace the CSV and Xl path with the folder which you have created to store the CSV file and Folder where your MS Excel files are located.







strPath = "C:\Documents and Settings\ritwik.shukla\Desktop\Excel Training\Xl"
'Path of Excel File, you may use standard or special folder names as per your need
csvpath = "C:\Documents and Settings\ritwik.shukla\Desktop\Excel Training\CSV\"
'Path of CSV File
Set objExcel = CreateObject("Excel.Application")
'Ceating a MS Excel Object instance
Set WSHShell = CreateObject("Wscript.Shell")
'Ceating a Windows Shell object to run the bat file
objExcel.Visible = False
'Better to keep Excel hidden as it might eat up some more memory
objExcel.DisplayAlerts = False
'switch off the Display Alerts in Excel
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFso.GetFolder (strPath)

For Each objFile In objFolder.Files
'Looping through all the Excel files in folder
If objFso.GetExtensionName (objFile.Path) = "xls" Then
Set objWorkbook = objExcel.Workbooks.Open(objFile.Path)
objWorkbook.SaveAs csvpath & objFile.name, 6, True o
bjWorkbook.Close
'MsgBox objFile.name & " Saved", 64,"Example"
'You may uncheck this option if you want to notify the user after each file is saved
End If
Next
objExcel.DisplayAlerts = True
'Switch on the Excel Display Alerts
WSHShell.Run ("C:\ConsolidateCSV.bat")
' Run the ConsolidateCSV.bat file
Set objExcel = Nothing
Set WSHShell = Nothing
'Free the ojects
MsgBox "All.txt is created!",64,"Example"
'Noify the user about all.txt file that has all the data




Step Four: Save the File with .vbs extension.

If you have performed every step appropriately, double clicking the .VBS file run a script to consolidate data from first worksheet of all workbooks into a text file with name all.txt.

Ps: In you have comments or a better way of doing it please post it.