Board logo

標題: [發問] 如何將模組內的變數Set為變數? (例如controls的功能) [打印本頁]

作者: PKKO    時間: 2014-12-13 16:52     標題: 如何將模組內的變數Set為變數? (例如controls的功能)

假設我有s1~s100的變數
在userform上面可以使用
For j = 1 To 100
    Set ctrl1 = ("s" & j)
    If ctrl1 = " " Then ctrl1 = ""
Next

但是在模組內(非userform)卻無法這樣使用
該使用何種相關功能的關鍵字呢~?
作者: GBKEE    時間: 2014-12-14 06:45

回復 1# PKKO
將UserForm的變數名稱,在一般模組內(非物件模組(UserForm,ThisWorkbook,Sheet1... )
宣告為這專案的 Public 公用變數,如此UserForm的變數,可在一般模組內呼叫到
作者: PKKO    時間: 2014-12-14 16:47

回復 2# GBKEE


    超版大大您好,抱歉,我說明的不夠清楚

我想問的是:

在模組內的變數假設有100個
分別命名為S1~S100
我希望透過判斷是IF S1 ="HH" THEN S1 = "YY"
但這簡單的程式碼要寫一百行(S1~S100)也挺辛苦的
是否有辦法可以

FOR I = 1 TO 100
  SET Sxx="S" & I
  IF Sxx ="HH" THEN Sxx = "YY"
NEXT
作者: PKKO    時間: 2014-12-15 07:29

回復 2# GBKEE

超版大大您好,主要是檔案裡面的這段程式碼
  1. With Sheets("資料庫")
  2.     If Not .Cells.Find(sheetname, Lookat:=1) Is Nothing Then
  3.         c = .Cells.Find(sheetname, Lookat:=1).Column
  4.         
  5.         s1 = .Cells(2, c): s2 = .Cells(3, c): s3 = .Cells(4, c): s4 = .Cells(5, c)
  6.         s5 = .Cells(6, c): s6 = .Cells(7, c): s7 = .Cells(8, c): s8 = .Cells(9, c)
  7.         s9 = .Cells(10, c): s10 = .Cells(11, c): s11 = .Cells(12, c)
  8.         s12 = .Cells(13, c): s13 = .Cells(14, c): s14 = .Cells(15, c)
  9.         s15 = .Cells(16, c): s16 = .Cells(17, c)
  10.         
  11.         Sheets("DIR").Activate
  12.     Else
  13.         MsgBox "請重新確認!"
  14.     End If

  15. End With
複製代碼
但我想把S1~S16做點改變
所以想在程式碼最後面加上一段
  1. For I = 1 To 16
  2.     Set ctrl = ("s" & I)
  3.     If ctrl = " " Then ctrl = ""
  4. Next
複製代碼
但是會出現錯誤訊息" 型態不符合"
作者: GBKEE    時間: 2014-12-15 08:27

回復 4# PKKO
試試看

模組頂端宣告私用變數
Dim S As Range
  1.   With Sheets("資料庫")
  2.     If Not .Cells.Find(sheetname, Lookat:=1) Is Nothing Then
  3.         C = .Cells.Find(sheetname, Lookat:=1).Column
  4.         Set S = .Cells(2, C).Resize(16)
  5.         Sheets("DIR").Activate
  6.     Else
  7.         MsgBox "請重新確認!"
  8.     End If

  9. End With
複製代碼
  1. For Each E In S
  2.     Set ctrl = S
  3.     If ctrl = " " Then ctrl = ""
  4. Next
複製代碼

作者: PKKO    時間: 2014-12-15 10:38

本帖最後由 PKKO 於 2014-12-15 10:40 編輯

回復 5# GBKEE

感謝超版大大,小弟學習了
以下是小弟實際的程式碼(學習後改版)
改版原因是
原本S1~S16都是需要應用的,現在反而S1~S16沒有值了
因此將大大您的S改成Q(因為原本S已經有使用),用Q的值拆開成為陣列,就可以用陣列去讀值
目前實驗完成,已可完美使用,再次感謝超版大大
  1. Public q As Range, myArray()'模組頂端,因為別的模組有用到因此宣告public
  2. With Sheets("資料庫")
  3.     If Not .Cells.Find(sheetname, Lookat:=1) Is Nothing Then
  4.         c = .Cells.Find(sheetname, Lookat:=1).Column
  5.         Set q = .Cells(2, c).Resize(16)
  6.         Sheets("DIR").Activate
  7.     Else
  8.         MsgBox "請重新確認!"
  9.     End If

  10. End With
  11.     c = 0
  12.     ReDim myArray(0 To 15)
  13.     For Each E In q
  14.         Set ctrl = E'其實這邊可以不需要了
  15.         If ctrl = " " Then myArray(c) = "" Else myArray(c) = ctrl'把ctrl換成E就可以了
  16.         c = c + 1
  17.     Next
複製代碼

作者: GBKEE    時間: 2014-12-15 16:09

回復 6# PKKO
  1. Dim i As Integer
  2.     With Sheets(1)
  3.         If Not .Cells.Find(sheetname, Lookat:=1) Is Nothing Then
  4.             c = .Cells.Find(sheetname, Lookat:=1).Column
  5.             Set q = .Cells(2, c).Resize(16)
  6.             Sheets("DIR").Activate
  7.         Else
  8.             MsgBox "請重新確認!"
  9.         End If
  10.     End With
  11.     myArray = Application.WorksheetFunction.Transpose(q)
  12.     For i = 1 To q.Count
  13.         If ctrl = " " Then myArray(i) = "" Else myArray(i) = ctrl
  14.    
  15.     Next
  16. '***************************
  17.     'For i = 1 To UBound(myArray)
  18.     '   If ctrl = " " Then myArray(i) = "" Else myArray(i) = ctrl
  19.     'Next
複製代碼
'***************************
作者: PKKO    時間: 2014-12-15 23:42

本帖最後由 PKKO 於 2014-12-15 23:43 編輯

回復 7# GBKEE


    感謝超版大大,又再次學習了
但您的意思因該是改為這樣,因為ctrl並沒有使用的必要了
  1. myArray = Application.WorksheetFunction.Transpose(q)
  2.     For YY = 1 To q.Count
  3.         If myArray(YY) = " " Then myArray(YY) = ""
  4.     Next
複製代碼
實驗過了,此方法也完全可行,且撰寫速度更加快速




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