Saturday, 20 March 2010

Excel Picture Quiz stage 2

In stage 2 we get the quiz to mark the questions, there are several ways to do this however for this tutorial I am going to use an IF statement. This allows us to have two responses, correct and incorrect, these can be substituted for your own message.

I am going to have the answer in cell B4 and the responce in B5, so the code for B5 is

=IF(B4="Johnny Depp", "Correct", "Wrong")

the italicised parts are the ones you will have to change for each picture.

B4 - the cell reference for the answer you are checking.

"Johnny Depp" - the correct answer.

It is important to note that for the counter I am going to create later to work the 'correct' message must be the same for every picture.

Set the answer cells to change colour using conditional formatting, in Office 2007 this is to the right on the home tab.











Simply set the cell to change colour when the cell is equal to your 'correct' message.

Excel Picture Quiz

So I thought it was about time I put something on here and I thought what better than to start with some instructions for creating a simple and fun picture quiz in Excel.

The instructions presume you have some basic knowledge of Excel to start with, but if there are any steps which you want explaining in more detail feel free to contact me using the quick contact on my website www.zypher.co.uk

I will split this into the following stages / lessons
  1. Set out the quiz
  2. Add functions and conditional formatting
  3. Add a score counter
  4. block access to the answers
We start by setting up the layout, set the column widths so you have a thin column followed by a wider one and repeating across the screen, you should start and end with thin columns. The wider columns will hold the images so set these appropriately.

Next set the height of some of the rows to allow them to hold the images, leaving at least 2 rows normal height, this is where the answer and correct or incorrect message will go.

Set the background colour of the cells and add a title to the centre of your top row.

Insert your images into the larger cells and add instructions to the side, I have used a text box though you could just as easily merge some cells for this.

Tuesday, 9 March 2010

Transpose an array

If you have ever used the paste special transpose option within an Excel worksheet you will know how handy it is.  The transpose option allows you copy a list, for example 10 rows with 2 columns, and turn it sideways, leaving you with 2 rows and 10 columns.

TransposeArray1 

Well below is a simple function you can copy straight into any VBA module.  It allows you to pass in an array of any size and passes back the ‘transposed’ array results.

Public Function TransposeArray(v As Variant) As Variant
' Custom Function to Transpose a 0-based array (v)

    Dim x As Long, y As Long, Xupper As Long, Yupper As Long
    Dim tempArray As Variant

    Xupper = UBound(v, 2)   'rows
    Yupper = UBound(v, 1)   'columns

    ReDim tempArray(Xupper, Yupper)
    For x = 0 To Xupper
        For y = 0 To Yupper
            tempArray(x, y) = v(y, x)
        Next y
    Next x

    TransposeArray = tempArray

End Function

I hope it proves useful.  If you have any questions or ideas for future blog posts please let us know via the comments section.

I’d like to add a thank you to a possible future Zypher-Blog colleague Niall for the example code.

Sunday, 21 February 2010

Current User and Current PC

In this post I thought I'd show two very useful functions which use Windows API calls, the username and PC name functions.  Windows API calls allow you to get information or features from the operating system which Excel and Access do not have.
The first step in using an API call is to tell VBA the function exists, where to find it, what arguments it takes and what data type it returns. This is done using the Declare statement, such as the example below.

Private Declare Function GetUserNameA _
  Lib "advapi32.dll" _
  (ByVal lpBuffer As String, nSize As Long) As Long

This statement tells the VBA interpreter that there is a function called GetUserNameA located in the file advapi32.dll that takes two arguments, the first a string and the second a Long value and returns a Long value. Once defined, we can call GetUserNameA in exactly the same way as if it is the VBA function:

  lng = GetUserNameA(strUserName, intLength)

The Function below uses the GetUserNameA API function and turns the results into a string which shows the current users Windows login.

Public Function CurrentUser() As String
'-- used to get current user's login id --
    Dim strUserName As String
    Dim lngLength As Long
    Dim lngResult As Long
    ' Set up the buffer
    strUserName = VBA.String(255, 0) 
    lngLength = 255
    ' Make the call 
    lngResult = GetUserNameA(strUserName,  lngLength)
    ' Assign the value
    CurrentUser = Left(strUserName, _
        InStr(1, strUserName, VBA.Chr(0)) - 1)
End Function

You can add this this code to any normal VBA module within either Excel or Access.  The API ‘Private Declare Function’ should be placed at the top of the module, before any other Sub Routines or Functions.
The current PC function, below, works in much the same way.

Private Declare Function GetComputerNameA Lib "kernel32" _
    (ByVal lpBuffer As String, nSize As Long) As Long

Public Function CurrentPC() As String
'-- used to get current user's login id --
    Dim strPCName As String
    Dim lngLength As Long
    Dim lngResult As Long
    ' Set up the buffer
    strPCName = VBA.String(255, 0)
    lngLength = 255
    ' Make the call
    lngResult = GetComputerNameA(strPCName, lngLength)
    ' Assign the value
    CurrentPC = Left(strPCName, _
        InStr(1, strPCName, VBA.Chr(0)) - 1)
End Function

Google / Yahoo as always will be able to provide more information on using API calls, this site is also probably worth a read.
I’ll add a sorry about the colour scheme, guess that’s what happens when you choose black as a background.

Monday, 15 February 2010

Access and conditional formatting reports

This isn't the important, in-depth blog post I'd hoped to start with, but is a subject which has come up in discussions a couple of times recently, so thought it worth posting.
Access 2007 allows you to apply conditional formatting to reports.  This means you can highlight one field, either because of it's value, or the values within in other fields.  You do this by right clicking the relevant field and selecting ‘Conditional Formatting’.
ConditionalFormatting1 The next step is to choose which field(s) you want the formatting to rely upon.  To do this choose either ‘Field Value Is’ or ‘Expression Is’.  The first choice makes the conditional format dependant on the selected field, the second allows you to enter a formula which can then look at other fields.
ConditionalFormatting2 In the example that follows I have chosen to highlight the ‘Product’ field where the ‘Quantity’ is higher then 2 using ‘Expression Is’.
ConditionalFormatting3When using IF statements, Access, unlike Excel wants to see IIF.
And now for the reason I’m posting this.  When choosing to change the back colour you need to ensure that the control’s (Label, TextBox etc) back style is set to ‘Normal’.  When creating a report the default back style is ‘Transparent’, this means the back colour format is not shown. 
ConditionalFormatting4
Below are two pics, the first where the control’s back style is ‘Transparent’.
ConditionalFormatting5And the  second after changing the back style to ‘Normal.
ConditionalFormatting4
Well, I hope that proves useful to someone, if you have any comments or questions let us know.

Saturday, 13 February 2010

Brand new blog.

Hi and welcome to the brand new blog from the guys at Zypher.co.uk.  
This blog will predominantly be about automating Microsoft Office and building MS Office tools and applications.  The occasional post may well cover over areas of ICT (Information Communication Technology) including, but not limited to our ICT care package for home and business - ICT support, Network design, management and upgrades, Database development, Website development and ICT Training.

We'll be putting our first few articles up over the coming days so hope you visit us again soon.

The team at Zypher.


You can also visit our site by clicking here.