For quite some time I have decided to solve some of the C# competition exams of the SoftUni in Excel. The problem for today is called Cheat Sheet and you may find the description here. Long story short, you have to build a multiplication table (a cheat sheet). The input is the number of column and rows. To make it a little more interesting, the start number of the column and the start number of the row are passed as parameters as well. So, if you have 3 rows, 5 columns, vertical start number 4 and horizontal number 8, the result should be like this:
To make the things more fun, I have added a fifth boolean parameter, to display the result with tabs or space between the numbers. This is important, becaues when you would like to use the table in Excel, the tabs of the cell do not work as expected and you need spaces there. Pretty much this is how the differences look like:
The one with the tabs between the numbers is obviously more beautiful, but still you need the second option to show it in excel. Last but not least, I made the parameters optional, because it could be useful.
Enjoy the sample:
Finally, here comes the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
Option Explicit Function fCheatSheet(Optional ByVal iRows As Integer = 20, _ Optional ByVal iColumns As Integer = 20, _ Optional ByVal iVerticalNumber As Integer = 1, _ Optional ByVal iHorizontalNumber As Integer = 1, _ Optional ByVal bMissingTabs As Boolean = False) _ As String Dim i As Integer 'counter of verticals Dim z As Integer 'counter of horizontals For i = iVerticalNumber To (iVerticalNumber + iRows - 1) Step 1 For z = iHorizontalNumber To (iHorizontalNumber + iColumns - 1) Step 1 fCheatSheet = fCheatSheet & i * z If (z < iHorizontalNumber + iColumns - 1) Then If bMissingTabs Then fCheatSheet = fCheatSheet & Chr(32) Else fCheatSheet = fCheatSheet & Chr(9) 'or VbCrLf End If End If Next z fCheatSheet = fCheatSheet & vbCrLf Next i fCheatSheet = fCheatSheet & "vitoshacademy.com" End Function |
Enjoy it! 🙂