Board logo

標題: 請問是否有化簡的方法? (再次感謝G大的幫忙~完成 [打印本頁]

作者: av8d    時間: 2012-7-24 15:48     標題: 請問是否有化簡的方法? (再次感謝G大的幫忙~完成

本帖最後由 av8d 於 2012-7-25 18:43 編輯
  1. Set d = CreateObject("Scripting.Dictionary")
  2. d("GAS1") = "A"
  3. d("GAS2") = "B"
  4. d("GAS3") = "C"
  5. d("GAS4") = "D"
  6. d("GAS5") = "E"
  7. d("GAS6") = "F"
  8. d("GAS7") = "G"
  9. d("GAS8") = "H"
  10. d("GAS9") = "I"
  11. d("GAS10") = "J"
  12. d("GAS11") = "K"
  13. d("GAS12") = "L"
  14. d("GAS13") = "M"
  15. d("GAS14") = "N"
  16. d("GAS15") = "O"
  17. d("GAS16") = "P"
  18. d("GAS17") = "Q"
  19. d("GAS18") = "R"
  20. L1C = Label1.Caption
  21. L2C = Label2.Caption
  22. L3C = Label3.Caption
  23. L4C = Label4.Caption
  24. TB1 = TextBox1.Value

  25. '資料轉換
  26. For i = 1 To 18
  27.     If Me.Controls("OptionButton" & i).Value = True Then
  28.         If i = 1 Then
  29.         Sheets("點餐").Cells(Rows.Count, "A").End(3)(2, 1).Resize(1, 7) = Array(L1C, L2C, L3C, L4C, 1, d("GAS" & i), TB1)
  30.         End If
  31.         
  32.         If i = 2 Then
  33.         Sheets("點餐").Cells(Rows.Count, "A").End(3)(2, 1).Resize(1, 7) = Array(L1C, L2C, L3C, L4C, 1, d("GAS" & i), TB1)
  34.         End If
  35.         
  36.         If i = 3 Then
  37.         Sheets("點餐").Cells(Rows.Count, "A").End(3)(2, 1).Resize(1, 7) = Array(L1C, L2C, L3C, L4C, 1, d("GAS" & i), TB1)
  38.         End If
  39.         
  40.         If i = 4 Then
  41.         Sheets("點餐").Cells(Rows.Count, "A").End(3)(2, 1).Resize(1, 7) = Array(L1C, L2C, L3C, L4C, 1, d("GAS" & i), TB1)
  42.         End If
  43.         
  44.         If i = 5 Then
  45.         Sheets("點餐").Cells(Rows.Count, "A").End(3)(2, 1).Resize(1, 7) = Array(L1C, L2C, L3C, L4C, 1, d("GAS" & i), TB1)
  46.         End If
  47.         
  48.         If i = 6 Then
  49.         Sheets("點餐").Cells(Rows.Count, "A").End(3)(2, 1).Resize(1, 7) = Array(L1C, L2C, L3C, L4C, 1, d("GAS" & i), TB1)
  50.         End If
  51.         
  52.         If i = 7 Then
  53.         Sheets("點餐").Cells(Rows.Count, "A").End(3)(2, 1).Resize(1, 7) = Array(L1C, L2C, L3C, L4C, 1, d("GAS" & i), TB1)
  54.         End If
  55.         
  56.         If i = 8 Then
  57.         Sheets("點餐").Cells(Rows.Count, "A").End(3)(2, 1).Resize(1, 7) = Array(L1C, L2C, L3C, L4C, 1, d("GAS" & i), TB1)
  58.         End If
  59.         
  60.         If i = 9 Then
  61.         Sheets("點餐").Cells(Rows.Count, "A").End(3)(2, 1).Resize(1, 7) = Array(L1C, L2C, L3C, L4C, 1, d("GAS" & i), TB1)
  62.         End If
  63.         
  64.         If i = 10 Then
  65.         Sheets("點餐").Cells(Rows.Count, "A").End(3)(2, 1).Resize(1, 7) = Array(L1C, L2C, L3C, L4C, 1, d("GAS" & i), TB1)
  66.         End If
  67.         
  68.         If i = 11 Then
  69.         Sheets("點餐").Cells(Rows.Count, "A").End(3)(2, 1).Resize(1, 7) = Array(L1C, L2C, L3C, L4C, 1, d("GAS" & i), TB1)
  70.         End If
  71.         
  72.         If i = 12 Then
  73.         Sheets("點餐").Cells(Rows.Count, "A").End(3)(2, 1).Resize(1, 7) = Array(L1C, L2C, L3C, L4C, 1, d("GAS" & i), TB1)
  74.         End If
  75.         
  76.         If i = 13 Then
  77.         Sheets("點餐").Cells(Rows.Count, "A").End(3)(2, 1).Resize(1, 7) = Array(L1C, L2C, L3C, L4C, 1, d("GAS" & i), TB1)
  78.         End If
  79.         
  80.         If i = 14 Then
  81.         Sheets("點餐").Cells(Rows.Count, "A").End(3)(2, 1).Resize(1, 7) = Array(L1C, L2C, L3C, L4C, 1, d("GAS" & i), TB1)
  82.         End If
  83.         
  84.         If i = 15 Then
  85.         Sheets("點餐").Cells(Rows.Count, "A").End(3)(2, 1).Resize(1, 7) = Array(L1C, L2C, L3C, L4C, 1, d("GAS" & i), TB1)
  86.         End If
  87.         
  88.         If i = 16 Then
  89.         Sheets("點餐").Cells(Rows.Count, "A").End(3)(2, 1).Resize(1, 7) = Array(L1C, L2C, L3C, L4C, 1, d("GAS" & i), TB1)
  90.         End If
  91.         
  92.         If i = 17 Then
  93.         Sheets("點餐").Cells(Rows.Count, "A").End(3)(2, 1).Resize(1, 7) = Array(L1C, L2C, L3C, L4C, 1, d("GAS" & i), TB1)
  94.         End If
  95.         
  96.         If i = 18 Then
  97.         Sheets("點餐").Cells(Rows.Count, "A").End(3)(2, 1).Resize(1, 7) = Array(L1C, L2C, L3C, L4C, 1, d("GAS" & i), TB1)
  98.         End If
  99.     End If
  100. Next i
複製代碼

作者: GBKEE    時間: 2012-7-24 16:06

回復 1# av8d
  1. Sub Ex()
  2.     Dim d As Object, i As Integer
  3.     Set d = CreateObject("Scripting.Dictionary")
  4.     L1C = Label1.Caption
  5.     L2C = Label2.Caption
  6.     L3C = Label3.Caption
  7.     L4C = Label4.Caption
  8.     TB1 = TextBox1.Value
  9.     For i = 1 To 18
  10.         d("GAS" & i) = Chr(64 + i)   '使用字元
  11.         If Me.Controls("OptionButton" & i).Value = True Then
  12.             Sheets("點餐").Cells(Rows.Count, "A").End(3)(2, 1).Resize(1, 7) = Array(L1C, L2C, L3C, L4C, 1, d("GAS" & i), TB1)
  13.         End If
  14.     Next
  15. End Sub
複製代碼

作者: av8d    時間: 2012-7-24 18:25

本帖最後由 av8d 於 2012-7-25 00:23 編輯

回復 2# GBKEE


    G大再次打擾~如果是這樣A~R是不規則中文~用英文暫代
d("GAS1") = "A"
d("GAS2") = "B"
d("GAS3") = "C"
d("GAS4") = "D"
d("GAS5") = "E"
d("GAS6") = "F"
d("GAS7") = "G"
d("GAS8") = "H"
d("GAS9") = "I"
d("GAS10") = "J"
d("GAS11") = "K"
d("GAS12") = "L"
d("GAS13") = "M"
d("GAS14") = "N"
d("GAS15") = "O"
d("GAS16") = "P"
d("GAS17") = "Q"
d("GAS18") = "R"
L1C = Label1.Caption
L2C = Label2.Caption
L3C = Label3.Caption
L4C = Label4.Caption
TB1 = TextBox1.Value

'資料轉換
For i = 1 To 18
    If Me.Controls("OptionButton" & i).Value = True Then
        Sheets("點餐").Cells(Rows.Count, "A").End(3)(2, 1).Resize(1, 7) = Array(L1C, L2C, L3C, L4C, 1, d("GAS" & i), TB1)
    End If
Next i

For i = 19 To 36
    If Me.Controls("OptionButton" & i).Value = True Then
        Sheets("點餐").Cells(Rows.Count, "A").End(3)(2, 1).Resize(1, 7) = Array(L1C, L2C, L3C, L4C, 1, d("GAS" & i), TB1)
    End If
Next i

d("GAS19")到d("GAS36")和d("GAS1")到d("GAS18")一樣
d("GAS1")等於d("GAS19")
d("GAS2")等於d("GAS20")
以此類推
我該如何寫才不用一直重複寫?
作者: GBKEE    時間: 2012-7-25 16:15

回復 3# av8d
  1. Option Explicit
  2. Sub Ex()
  3.     Dim d As Object, i As Integer
  4.     Set d = CreateObject("Scripting.Dictionary")
  5.     L1C = Label1.Caption
  6.     L2C = Label2.Caption
  7.     L3C = Label3.Caption
  8.     L4C = Label4.Caption
  9.     TB1 = TextBox1.Value
  10.     For i = 1 To 18
  11.         d("GAS" & i) = Sheet1.Cells(i, "A")  '不規則中文在 A1 - A18
  12.         If Me.Controls("OptionButton" & i).Value = True Then
  13.             Sheets("點餐").Cells(Rows.Count, "A").End(3)(2, 1).Resize(1, 7) = Array(L1C, L2C, L3C, L4C, 1, d("GAS" & i), TB1)
  14.         End If
  15.     Next
  16. End Sub
複製代碼





歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)