Advertisement
maciejms

VBA 4

May 22nd, 2019
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Option Explicit
  2. Option Base 1
  3. Sub tab1vba4()
  4.     Dim i As Integer, tbl(10) As Integer, tmp As Integer
  5.         ' tworzy tablice z losowymi liczbami i wpisuje do arkusza
  6.        For i = 1 To 10
  7.             tbl(i) = 1 + Int(Rnd * (10 - 1 + 1))
  8.             Cells(i, 1) = tbl(i)
  9.         Next i
  10.         ' przesuwa elementy w tablicy wg następującego wzoru: T(1) -> T(2) -> ... -> T(10) -> T(1)
  11.        tmp = tbl(10)
  12.         For i = 10 To 2 Step -1
  13.             tbl(i) = tbl(i - 1)
  14.             Cells(i, 2) = tbl(i)
  15.         Next i
  16.         Cells(1, 2) = tmp
  17.        
  18. End Sub
  19. Sub tab2vba4()
  20.     Dim i As Integer, pom As Integer, ileLiczb As Integer
  21.     ileLiczb = InputBox("Podaj ile liczb ma zawierać tablica")
  22.     ReDim t(ileLiczb) As Integer ' ReDim pozwala deklarować tablice dynamiczne
  23.        For i = 1 To ileLiczb
  24.             t(i) = 1 + Int(Rnd * (9))
  25.             Cells(i, 1) = t(i)
  26.         Next i
  27.         For i = 1 To ileLiczb / 2
  28.             pom = t(i)
  29.             t(i) = t(ileLiczb - i + 1)
  30.             t(ileLiczb - i + 1) = pom
  31.         Next i
  32.         For i = 1 To ileLiczb
  33.             Cells(i, 2) = t(i)
  34.         Next i
  35. End Sub
  36. Sub tab3vba4()
  37.     Dim i As Integer, j As Integer, t(20) As Integer
  38.     j = 1
  39.     For i = 1 To 20 Step 2
  40.         t(i) = j
  41.         t(i + 1) = j
  42.         j = j + 1
  43.     Next i
  44.     For i = 1 To 20
  45.         Cells(1, i) = t(i)
  46.     Next i
  47. End Sub
  48. Sub tab4vba4()
  49.     Dim i As Integer, j As Integer, t(10, 10) As Integer, n As Integer
  50.     'umieszcznie na obydwu przekątnych liczbę 1
  51.    ' dla drugiej przekątnej wycznaczyć zależność j od i (kolumny od wiersza)
  52.    n = 10 ' rozmiar macierzy (tablicy)
  53.    For i = 1 To n
  54.         For j = 1 To n
  55.             If (i = j) Or (j = n - i + 1) Then
  56.                 t(i, j) = 1
  57.             Else
  58.                 t(i, j) = -1
  59.             End If
  60.         Next j
  61.     Next i
  62.     ' wypisanie wyniku do arkusza
  63.    For i = 1 To n
  64.         For j = 1 To n
  65.             Cells(i, j) = t(i, j)
  66.         Next j
  67.     Next i
  68. End Sub
  69. Sub tab5avba4()
  70. 'Szyfr Cezara - szyfrowanie
  71.    Dim i As Byte, tekst As String, szyfr As String, v As Integer, lenght As Byte, a As Byte, code As Byte, pom As Byte
  72. et: tekst = InputBox("Podaj słowo")
  73.     v = InputBox("Podaj przesunięcie")
  74.     lenght = Len(tekst)
  75.     For i = 1 To lenght
  76.         a = Asc(Right(Left(tekst, i), 1))
  77.         pom = a + v
  78.         If (pom < 65) And (a <> 32) Then
  79.             pom = pom + 26
  80.         ElseIf pom > 90 And pom < 97 And (v > 0) Then
  81.             pom = pom - 26
  82.         ElseIf pom > 90 And pom < 97 And (v < 0) Then
  83.             pom = pom + 26
  84.         ElseIf pom > 122 Then
  85.             pom = pom - 26
  86.         ElseIf a = 32 Then
  87.             pom = 32
  88.         End If
  89.         szyfr = szyfr & Chr(pom)
  90.     Next i
  91.     MsgBox szyfr
  92.     code = MsgBox("Jeszcze raz?", vbYesNo + vbQuestion)
  93.     If code = vbYes Then
  94.         szyfr = ""
  95.         GoTo et
  96.     End If
  97. End Sub
  98. Sub tab5bvba4()
  99. 'Szyfr Cezara - deszyfrowanie
  100.    Dim szyfr As String, slowo As String, dl As Integer, v As Integer, pom As String, a As String, i As Integer, kod As Byte
  101. et: szyfr = InputBox("Podaj zaszyfrowaną wiadomość")
  102.     v = InputBox("Podaj przesunięcie")
  103.     dl = Len(szyfr)
  104.     For i = 1 To dl
  105.         a = Asc(Right(Left(szyfr, i), 1))
  106.         pom = a - v
  107.         If pom > 90 And pom < 97 And v > 0 Then
  108.             pom = pom + 26
  109.         ElseIf pom > 90 And pom < 97 And v < 0 Then
  110.             pom = pom - 26
  111.         ElseIf (pom < 65) And (a <> 32) Then
  112.             pom = pom + 26
  113.         ElseIf pom > 122 Then
  114.             pom = pom - 26
  115.         ElseIf (a = 32) Then
  116.             pom = 32
  117.         End If
  118.         slowo = slowo & Chr(pom)
  119.     Next i
  120.     MsgBox slowo
  121.     kod = MsgBox("Jeszcze raz?", vbYesNo + vbQuestion)
  122.     If kod = vbYes Then
  123.         slowo = ""
  124.         GoTo et
  125.     End If
  126. End Sub
  127. Sub tab6vba4()
  128. ' Bubble Sort
  129.    Dim t() As Variant, i As Integer, j As Integer, n As Integer, tmp As Integer
  130.     'n = InputBox("")
  131.    n = 10
  132.     t = Array(1, 3, 4, 3, 2, 5, 6, 8, 2, 7)
  133.     For i = 1 To 10
  134.         Cells(1, i) = t(i)
  135.     Next i
  136.     For j = 1 To n - 1
  137.         For i = 1 To n - 1
  138.             If (t(i) > t(i + 1)) Then
  139.                 tmp = t(i + 1)
  140.                 t(i + 1) = t(i)
  141.                 t(i) = tmp
  142.             End If
  143.         Next i
  144.     Next j
  145.     For i = 1 To 10
  146.         Cells(2, i) = t(i)
  147.     Next i
  148. End Sub
  149. Sub tab7vba4()
  150.     Dim i As Integer, j As Integer, k As Integer, t() As Variant, n As Integer, min As Integer
  151.     t = Array(2, 3, 4, 3, 1, 5, 6, 8, 1, 7)
  152.     n = 10
  153.     For i = 1 To n
  154.         Cells(1, i) = t(i)
  155.     Next i
  156. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  157.        For j = 1 To 10 - 1
  158.             min = t(j)
  159.             k = j
  160.             For i = j + 1 To 10
  161.                 If (t(i) < min) Then
  162.                     min = t(i)
  163.                     k = i
  164.                 End If
  165.             Next i
  166.             t(k) = t(j)
  167.             t(j) = min
  168.         Next j
  169. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  170.    For i = 1 To 10
  171.         Cells(3, i) = t(i)
  172.     Next i
  173. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement