TomGee.us

Programming

Microsoft Excel: Count the number of unique values in list

by Tom Gee on Apr.19, 2010, under Excel

To count the number of unique or distinct values in a list in Microsoft Excel, use the following formula.  A2:A177 is the range of cells you want to count the unique values in:

=SUM(IF(FREQUENCY(A2:A177,A2:A177)>0,1))

Leave a Comment :, , , , , , , more...

Sequentially run all VB Script (.vbs) files in a directory

by Tom Gee on Apr.06, 2010, under C#, Uncategorized

Create a new C# Console Application in Visual Studio and compile the code.  Copy the compiled .exe file to the directory containing the .vbs scripts you want to run in sequence.

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Diagnostics;
/*————————————————————————–
* Developer: Tom Gee
* Date: April 6, 2010
* Description: This .exe file will run every .VBS file in the same directory
*              as the .exe, but will wait for the current .VBS script to end
*              before beginning the next.
————————————————————————–*/
namespace RunAllVBSinDirectory
{
class Program
{
static void Main(string[] args)
{
try
{
string[] scripts = Directory.GetFiles(Directory.GetCurrentDirectory(), “*.vbs”);
int i = 0;
foreach (string script in scripts)
{
i++;
Console.WriteLine(“Running: ” + script);
using (Process exeProcess = Process.Start(script))
{
exeProcess.WaitForExit();
exeProcess.Close();
exeProcess.Dispose();
}
}
Console.WriteLine(“Ran all ” + i + ” script files in current directory (” + Directory.GetCurrentDirectory() + “)“);
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine(“ERROR! ” + ex.Message + Environment.NewLine + “Inner Exception: ” + ex.InnerException + Environment.NewLine + “Stack Trace: ” + ex.StackTrace);
Console.ReadLine();
}
}
}
}
Leave a Comment :, , , , , , , , , , , , , , , , more...

How to generate a random number in C#

by Tom Gee on Mar.19, 2010, under C#, Programming

Here’s a quick and dirty way to use Microsoft’s “Random” class for generating a random (or as you will soon find out, not-so-random) number.

Because I believe in code reuse, I started by creating a new function that would return the random number as an integer datatype.

static int RandomNumber(int min, int max)

{

Random random = new Random();

return random.Next(min, max);

}

The only things you have to pass to the function are the upper and lower boundaries that you want the random number to fall between.

int zip = RandomNumber(10000, 99999);

In the above example, a random number between 10000 and 99999 will be assigned to the “zip” variable.

As I stated above, the random number isn’t actually very random and, therefore, might not be a perfect fit for what your trying to use it for.  However, the Random class serves its purpose for quick data generation for application testing.

Happy coding!

Leave a Comment :, , , , , , , more...

VB: Disable all controls on a form

by Tom Gee on Jan.06, 2010, under Programming, VB.NET

I use this code to cycle through all controls on a form and disable them.  After each control has been disabled, you can then specify individual controls that you wish to be enabled.  This is a handy way to force users to click specific buttons or enter data in specific fields before the rest of the controls on a form become active (enabled).

Private Sub LockControls()
‘ Disable all the controls on the form
Dim
ctrl As Control

For Each ctrl In Me.Controls
ctrl.Enabled = False
Next

Me.txt_Log.Enabled = True ‘ re-enable the log box
Me.progBar.Enabled = True ‘ re-enable the progress bar
Me.lst_ItemGroups.Enabled = True ‘ re-enable the listbox
End Sub

To enable all controls on the form, use the following function:

Private Sub UnlockControls()
‘ Enable all the controls on the form
Dim ctrl As Control

For Each ctrl In Me.Controls
ctrl.Enabled = True
Next
End Sub

‘ Disable all the controls on the form
Leave a Comment :, , , , , , , more...

VB.NET LEFT and RIGHT functions

by Tom Gee on Jan.06, 2010, under Programming, VB.NET

VBA and VB6 developers are used to the familiar LEFT and RIGHT string manipulation functions.  However, VB.NET no longer has these functions built-in. As a result, when I create a new VB.NET application, I create the following two functions which I can then use to replicate the “old” LEFT and RIGHT functionality.

Public Function Left(ByVal Value As String, ByVal Length As Integer) As String
‘ Rereate a LEFT function for string manipulation
If Value.Length >= Length Then
Return Value.Substring(0, Length)
Else
Return
Value
End If
End Function

Public Function Right(ByVal Value As String, ByVal Length As Integer) As String
‘ Recreate a RIGHT function for string manipulation
If Value.Length >= Length Then
Return Value.Substring(Value.Length – Length, Length)
Else
Return
Value
End If
End Function

Leave a Comment :, , , , , , , , more...

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!