Advertisement
maciejms

VBA 3

May 15th, 2019
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Option Explicit
  2. Sub do1()
  3.     Dim bot As Integer, user As Integer, c As Integer
  4.         bot = CInt(Rnd * 10)
  5.         c = 0
  6.         Do
  7.         user = InputBox("Zgadnij liczbe:")
  8.         If (user > bot) Then
  9.             MsgBox "Za dużo"
  10.         ElseIf (user < bot) Then
  11.             MsgBox "Za mało"
  12.         End If
  13.         c = c + 1
  14.         Loop Until user = bot
  15.         MsgBox "Liczba prawidłowa, zgadłeś za " & c & " razem"
  16. End Sub
  17. Sub do2()
  18.     Dim i As Integer, sum As Integer, a As Integer
  19.     i = 1
  20.     a = 5
  21.     sum = 0
  22.     Do
  23.         Cells(i, 1) = a
  24.         sum = sum + a
  25.         i = i + 1
  26.         a = a + 1
  27.     Loop Until sum > 100
  28. End Sub
  29. Sub do3()
  30.     Dim a As Integer, i As Integer
  31.         a = 3
  32.         i = 1
  33.         Do
  34.             If (a Mod 3 = 0 Or a Mod 5 = 0) Then
  35.                 Cells(i, 1) = a
  36.                  i = i + 1
  37.             End If
  38.         a = a + 1
  39.         Loop While i <= 15
  40. End Sub
  41. Sub do4()
  42. Dim i As Integer, x As Single, Xk As Single, P As Single, Rok As Integer
  43.     x = InputBox("Podaj kapitał początkowy")
  44.     P = InputBox("Podaj oprocentownie")
  45.     i = 1
  46.     Xk = 0
  47.     Do
  48.         Xk = x * (1 + P / 100) ^ i
  49.         i = i + 1
  50.     Loop Until Xk >= 2 * x
  51.     MsgBox i - 1
  52. End Sub
  53. Sub do5a()
  54.     Dim dec As Integer, bin As Integer, i As Integer
  55.     dec = InputBox("Podaj liczbe dziesiętną, zakres 0 do 255", "Zamiana liczby dzisiętnej na 8-bitową binarną")
  56.     i = 0
  57.     If dec >= 0 And dec <= 255 Then
  58.         Do
  59.             bin = dec Mod 2
  60.             dec = dec \ 2
  61.             Cells(2, 8 - i) = bin
  62.            
  63.             If Cells(2, i + 1) = "" Then
  64.                 Cells(2, i + 1) = 0
  65.             End If
  66.            
  67.             i = i + 1
  68.         Loop Until dec = 0
  69.     Else
  70.         MsgBox "Liczba nie mieści się w podanym zakresie"
  71.     End If
  72. End Sub
  73. ' StrReverse(string) odwaraca ciąg
  74. Sub do5b()
  75.     Dim dec As Integer, bin As Integer, i As Integer, wynik As String, x As Integer
  76.     dec = InputBox("Podaj liczbę dziesiętną do konwersji na postać binarną") ' liczba dziesiętna znajduje się w komórce A1
  77.    x = dec ' kopiuję wartość dec
  78.    Do Until dec = 0
  79.         bin = dec Mod 2
  80.         dec = dec \ 2
  81.         wynik = wynik & bin
  82.     Loop
  83.     MsgBox "Liczba " & x & " binarnie to:" & Chr(13) & StrReverse(wynik)
  84. End Sub
  85. Sub do6()
  86.     Dim x As Integer, tmp As Integer, i As Integer
  87.         x = InputBox("Podaj liczbę")
  88.         tmp = 1
  89.         i = 0
  90.         Do
  91.             tmp = tmp * 2
  92.             i = i + 1
  93.         Loop While tmp < x
  94.         If tmp = x Then
  95.             MsgBox "Liczbę " & x & " można przedstawić jako 2^" & i
  96.         Else
  97.             MsgBox "Liczby " & x & " nie można przedstawić jako 2^n"
  98.         End If
  99. End Sub
  100. Sub do7()
  101.     Dim a As Integer, b As Integer, r As Integer, NWD As Integer
  102.     a = InputBox("Podaj a:")
  103.     b = InputBox("Podaj b:")
  104.         Do
  105.             r = a Mod b
  106.             If r = 0 Then
  107.                 NWD = b
  108.             Else
  109.                 a = b
  110.                 b = r
  111.             End If
  112.         Loop Until r = 0
  113.     MsgBox NWD
  114. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement