70 likes | 180 Vues
This comprehensive guide covers the fundamentals of Visual Basic programming, including key concepts like variables, internal functions, conditions, loops, and input/output operations. Learn about essential mathematical functions such as Abs (absolute value), Log (logarithm), and various string manipulation techniques. Explore how to implement conditional statements using If and Select Case methods, along with looping constructs like For and Do Until. The text also demonstrates practical applications in Excel VBA, enhancing your programming skills for effective data handling and processing.
E N D
Introduction to MIS Appendix 12 Visual Basic
Programming Logic Computations Variables Internal functions Conditions Loops Input Output Appendix: Visual Basic Math functions Abs Absolute value Atn Arc Tangent Cos Cosine Exp Exponential Fix Returns integer portion Int Converts to integer Log Logarithm Rnd Random number Sgn Signum (-1, 0, 1) Sin Sine Sqr Square root Tan Tangent String functions StrComp Compare two strings LCase, UCase Convert to lowercase or uppercase Len Find length of a string Format Format a string InStr, Left, LTrim Mid, Right, RTrim, Trim Manipulate strings.
VB: Conditions If (condition) Then statements if true Else statements if false End If Select Case Customer Case Customer = ‘Corporate’ Discount = 0.05 Case Customer = ‘Government’ Discount = 0.10 Case Else Discount = 0.01 End Select If (Sales > 1000) Then Bonus = 100 Else Bonus = 0 End If
VB: Loops total = 0 For month = 1 To 12 total = total + SalesForMonth(month) Next month month = 1 sales = 0 Do Until (sales > 100000) sales = sales + SalesForMonth(month) month = month + 1 Loop
VB: Input and Output Could use: InputBox, MsgBox, and Printer object. Generally just use data in the application. In this example, the form collects the data and displays the result.
Sub Macro1() ' Keyboard Shortcut: Ctrl+Shift+U For Each c In Selection c.Value = PCase(c.Value) Next c End Sub VBA: Excel Example Function PCase(txt) ' Convert a text value to proper case Dim i As Integer txt = LCase(txt) Mid(txt, 1, 1) = UCase(Mid(txt, 1, 1)) i = 2 Do While (i > 0) And (i < Len(txt)) i = InStr(i, txt, " ") If (i > 0) And (i < Len(txt)) Then Mid(txt, i + 1, 1) = UCase(Mid(txt, i + 1, 1)) i = i + 1 End If Loop PCase = txt End Function