PDA

View Full Version : I need help with this VBA exam example



Cassie_
01-29-2016, 03:46 AM
I don't even know where to start...1531615317

SamT
01-29-2016, 06:40 AM
See the help file for these KeyWords: For, Loop, End, Columns, Rows, Set, Min, Max, Date, DateDiff, and WorksheetFunction.

MS has an Excel 2010 Developer Documentation.chm help file for download, or you can search the internet for "VBA" + the KeyWord.

Although you don't really need End, Rows, and Columns for the first example, it is Best Practice to avoid the use of Magic Numbers.

With Excel, DateTime values are displayed as Dates and Times, depending on the Cell Format, but are stored as Doubles.



Experiment: Enter two dates in Ranges A1 and B1. Format C1 as a Number. Enter "= A1-B1" in C1.

Format C1 as a number with 11 decimal places, In A1, type "=NOW()".

Put this code in the Worksheet's code page and run it

Sub Test
Cells(2, 1) = Cells(1, 2)
End Sub



Final hint: Never use a KeyWord, (a Reserved word,) as a variable name.
Use: Rng vice Range
Cel vice Cell
Rw vice Row
Col vice Column
Sht vice sheet
WkBk vice WorkBook
Etc

Kenneth Hobs
01-29-2016, 07:10 AM
Most forums have rules about not helping with homework. http://www.vbaexpress.com/forum/faq.php?faq=psting_faq_item#faq_hom_faq_item
If you want help with homework, make the attempt to solve it first. There is nothing wrong asking for help with concepts though. That is part of problem solving, understanding what concepts are involved.

You are in a programming class and don't know how to even get started? Obviously, your first step is to put the data into a file in the cells shown. Make sure that your dates are the numeric value and not the Formatted string value. Press Ctrl+1 to set formats. Same concept applies to strings with dollar or comma characters, those are formats and seldom should be entered as strings. Most teachers would prefer that your learn concepts and not just solve and not know why.

In VBE, put your cursor in a command word or next to it and press F1 to get specific help. Used F2 to browse for help in general.
Command words here might be:
Max or WorksheetFunction.Max
Min or WorksheetFunction.Min
If
Then
Else
Do
While
Single
String
Range
Set
For
ForEach
Array
Offset
Function

Search the web or this forum for UDF.

These words can be in a Sub or a Function in the Code window or the Immediate Window in the VBE.

If you view forum threads and try to solve them and then view the solution(s), you learn more. The best way to learn, is to teach. So, replay to a thread even if you can only solve part of it. You will be notified of replies so you can view the solutions and say wow, I never thought to solve it that way, I am saving it for my toolbox.

Keep in mind that professors scan the web for cheating. In the end, it is you that gets cheated when cheating.