Intermediate VBA course work singapore
Beautiful Art pixelated with VBA Programming.
Project Duration (hours)
Funding:
Cost
You will need basic VBA programming knowledge to complete this intermediate level VBA course work. Full VBA codes are provided but instructions will be sparse. Your challenge is to assemble a working VBA application in Excel to demonstrate a basic knowledge of VBA programming in Excel. The completed Excel VBA application will import an image and create a worksheet of colored cells to replicate the image. You will start from an empty Excel workbook.
Our Advanced VBA workshops are thematic, project-oriented aimed at a very specific outcome that allows you to demonstrate high VBA language proficiency with confidence to employers and interviewers.
They are designed to be quickly delivered to students with high hand-on ability from the get-go. Hence, we require our attendees to complete this intermediate VBA course work as a prerequisite for the Advanced VBA master classes. If these VBA codes look unfamiliar then we would recommend that you consider our Beginner VBA course as a start.
Option Explicit
'*****************************************************************
'Purpose: insert an image onto the active worksheet
'*****************************************************************
Sub Browse_File()
'Step 1: delete any existing shapes on the active worksheet
Dim shpImage As Shape
For Each shpImage In ActiveSheet.Shapes
shpImage.Delete
Next
'Step 2: set all cells to no fill
With ActiveSheet.Cells.Interior
.Pattern = xlNone
.TintAndShade = 0
.PatternTintAndShade = 0
End With
'Step 3: make square grids
Cells.EntireColumn.ColumnWidth = 0.5 'adjust this value to make square grids
Cells.EntireRow.RowHeight = 5 'adjust this value to make square grids
'Step 4: ask user to browse for a file
Dim vntFileNameFull As Variant
On Error Resume Next
ChDir ThisWorkbook.Path 'change current directory to workbook location
On Error GoTo 0
vntFileNameFull = Application.GetOpenFilename("Image files(*.jpg;*.png;*.gif;*.bmp),*.jpg;*.png;*.gif;*.bmp", , "Select an image file") 'file browser
If (vntFileNameFull = False) Then 'if cancel button pressed
End 'stop everything
End If
'Step 5: insert the image as a shape, at top left, orig size
Set shpImage = ActiveSheet.Shapes.AddPicture( _
Filename:=vntFileNameFull, _
LinkToFile:=msoFalse, _
SaveWithDocument:=msoTrue, _
Left:=0, _
Top:=0, _
Width:=-1, _
Height:=-1)
End Sub
This is the logic in the first VBA module.
Option Explicit
'Step 1:declare some Windows APIs
#If VBA7 Then
'for 64-bit Excel
Declare PtrSafe Function GetDC Lib "user32" (ByVal hwnd As LongPtr) As LongPtr
Declare PtrSafe Function GetDeviceCaps Lib "gdi32" (ByVal hDC As LongPtr, ByVal nIndex As LongPtr) As LongPtr
Declare PtrSafe Function ReleaseDC Lib "user32" (ByVal hwnd As LongPtr, ByVal hDC As LongPtr) As LongPtr
#Else
Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Declare Function GetDeviceCaps Lib "gdi32" (ByVal hDC As Long, ByVal nIndex As Long) As Long
Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hDC As Long) As Long
#End If
'Step 2: declare some public variables to store some image stats
Public lgImageLeftPx As Long
Public lgImageTopPx As Long
Public lgImageRightPx As Long
Public lgImageBtmPx As Long
Public lgImageWidthPx As Long
Public lgImageHeightPx As Long
'*****************************************************************
'Step 3:
'Purpose: get the screen dpi. ie: how many pixels in an inch on the screen
'*****************************************************************
Function ScreenPixelPerInch() As Long
Const LOGPIXELSX = 88 'parameter for GetDeviceCaps: Ask for Number of pixels per logical inch along the screen width.
Dim lgDevContext As Long 'device context for the screen
lgDevContext = GetDC(0)
ScreenPixelPerInch = GetDeviceCaps(lgDevContext, LOGPIXELSX) 'device capability for Number of pixels per logical inch
lgDevContext = ReleaseDC(0, lgDevContext)
End Function
'*****************************************************************
'Step 4:
'Purpose: calculate points to pixels. ie: 72 points per inch in Excel
'*****************************************************************
Function ExcelPointToScreenPixel(lgPoint As Long) As Long
ExcelPointToScreenPixel = lgPoint / 72 * ScreenPixelPerInch
End Function
'*****************************************************************
'Step 5:
'Purpose: get the image pixel width, height.
'*****************************************************************
Sub GetImagePixelStatistics()
'Step 6: check for image
If ActiveSheet.Shapes.Count <> 1 Then
MsgBox ("Please have only 1 image on the worksheet")
End
End If
'Step 7: get reference to Excel's window
Dim wndExcel As Window
Set wndExcel = ActiveSheet.Cells(1, 1).Parent.Parent.Windows(1)
'Step 8: get pixel locations of the Excel Window
Dim lgExcelWindowLeftPx As Long
Dim lgExcelWindowTopPx As Long
lgExcelWindowLeftPx = wndExcel.PointsToScreenPixelsX(0)
lgExcelWindowTopPx = wndExcel.PointsToScreenPixelsY(0)
'Step 9: get reference to the image
Dim shpImage As Shape
Set shpImage = ActiveSheet.Shapes(1)
'Step 10: image left pixel location
lgImageLeftPx = ExcelPointToScreenPixel(shpImage.Left * wndExcel.Zoom / 100) + lgExcelWindowLeftPx
'Step 11: image top pixel location
lgImageTopPx = ExcelPointToScreenPixel(shpImage.Top * wndExcel.Zoom / 100) + lgExcelWindowTopPx
'Step 12: image width in pixels
lgImageRightPx = ExcelPointToScreenPixel(shpImage.Width * wndExcel.Zoom / 100) + lgImageLeftPx
'Step 13: image height in pixels
lgImageBtmPx = ExcelPointToScreenPixel(shpImage.Height * wndExcel.Zoom / 100) + lgImageTopPx
'Step 14: image width in pixels
lgImageWidthPx = lgImageRightPx - lgImageLeftPx
'Step 15: image height in pixels
lgImageHeightPx = lgImageBtmPx - lgImageTopPx
Debug.Print "width:"; lgImageWidthPx, "height"; lgImageHeightPx; "total:"; lgImageWidthPx * lgImageHeightPx, _
"left:"; lgImageLeftPx; "right:"; lgImageRightPx; "top:"; lgImageTopPx; "btm:"; lgImageBtmPx; "zoom:"; wndExcel.Zoom
End Sub
The module logic in the second VBA module in English.
Option Explicit
#If VBA7 Then
Declare PtrSafe Function GetPixel Lib "gdi32" (ByVal hDC As LongPtr, ByVal X As LongPtr, ByVal Y As LongPtr) As LongPtr
#Else
Declare Function GetPixel Lib "gdi32" (ByVal hDC As Long, ByVal X As Long, ByVal Y As Long) As Long
#End If
'*****************************************************************
'Purpose:
'*****************************************************************
Sub ScanImage()
'set all cells to no fill
With ActiveSheet.Cells.Interior
.Pattern = xlNone
.TintAndShade = 0
.PatternTintAndShade = 0
End With
ActiveSheet.Cells(1, 1).Activate
'image stats stored in public variables
Call GetImagePixelStatistics
'give warning for big image
If lgImageWidthPx * lgImageHeightPx > 10000 Then
Dim intReply As Integer
intReply = MsgBox("The image is more than 10000 pixels, it can take longer to generate. Proceed?", vbYesNo)
If intReply = vbNo Then
End
End If
End If
'get device context for the screen
Dim lgDevContext As Long
lgDevContext = GetDC(0)
'scan the image
Dim lgHortPxCurr As Long, lgVertPxCurr As Long
Dim lgPxColour As Long
Dim lgRowCurr As Long, lgColCurr As Long
lgRowCurr = 1
lgColCurr = 1
For lgVertPxCurr = lgImageTopPx To lgImageBtmPx
For lgHortPxCurr = lgImageLeftPx To lgImageRightPx
lgPxColour = GetPixel(lgDevContext, lgHortPxCurr, lgVertPxCurr)
Call ColourCell(Cells(lgRowCurr, lgColCurr), lgPxColour)
lgColCurr = lgColCurr + 1 'increment cell column
Next
lgColCurr = 1 'reset cell column
lgRowCurr = lgRowCurr + 1 'increment cell row
DoEvents
Next
'release device context
lgDevContext = ReleaseDC(0, lgDevContext)
'MsgBox "Done!"
End Sub
'*****************************************************************
'Purpose: colour a cell
'*****************************************************************
Sub ColourCell(rngCell As Range, lgColour As Long)
Dim byteRed As Byte
Dim byteGreen As Byte
Dim byteBlue As Byte
byteRed = lgColour And &HFF&
byteGreen = (lgColour And &HFF00&) / 256
byteBlue = (lgColour And &HFF0000) / 65535
rngCell.Interior.Color = RGB(byteRed, byteGreen, byteBlue)
End Sub
Aeternus Consulting is the premier training centre in Singapore for Excel Courses – Basic Excel, Advanced Excel and Excel VBA Macro courses. For more Microsoft Excel training courses, please visit our Excel training page.
Aeternus Consulting is now on Instagram! So take a look and follow us on Instagram.