標題:
[發問]
如何將模組內的變數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
超版大大您好,主要是檔案裡面的這段程式碼
With Sheets("資料庫")
If Not .Cells.Find(sheetname, Lookat:=1) Is Nothing Then
c = .Cells.Find(sheetname, Lookat:=1).Column
s1 = .Cells(2, c): s2 = .Cells(3, c): s3 = .Cells(4, c): s4 = .Cells(5, c)
s5 = .Cells(6, c): s6 = .Cells(7, c): s7 = .Cells(8, c): s8 = .Cells(9, c)
s9 = .Cells(10, c): s10 = .Cells(11, c): s11 = .Cells(12, c)
s12 = .Cells(13, c): s13 = .Cells(14, c): s14 = .Cells(15, c)
s15 = .Cells(16, c): s16 = .Cells(17, c)
Sheets("DIR").Activate
Else
MsgBox "請重新確認!"
End If
End With
複製代碼
但我想把S1~S16做點改變
所以想在程式碼最後面加上一段
For I = 1 To 16
Set ctrl = ("s" & I)
If ctrl = " " Then ctrl = ""
Next
複製代碼
但是會出現錯誤訊息" 型態不符合"
作者:
GBKEE
時間:
2014-12-15 08:27
回復
4#
PKKO
試試看
模組頂端宣告私用變數
Dim S As Range
With Sheets("資料庫")
If Not .Cells.Find(sheetname, Lookat:=1) Is Nothing Then
C = .Cells.Find(sheetname, Lookat:=1).Column
Set S = .Cells(2, C).Resize(16)
Sheets("DIR").Activate
Else
MsgBox "請重新確認!"
End If
End With
複製代碼
For Each E In S
Set ctrl = S
If ctrl = " " Then ctrl = ""
Next
複製代碼
作者:
PKKO
時間:
2014-12-15 10:38
本帖最後由 PKKO 於 2014-12-15 10:40 編輯
回復
5#
GBKEE
感謝超版大大,小弟學習了
以下是小弟實際的程式碼(學習後改版)
改版原因是
原本S1~S16都是需要應用的,現在反而S1~S16沒有值了
因此將大大您的S改成Q(因為原本S已經有使用),用Q的值拆開成為陣列,就可以用陣列去讀值
目前實驗完成,已可完美使用,再次感謝超版大大
Public q As Range, myArray()'模組頂端,因為別的模組有用到因此宣告public
With Sheets("資料庫")
If Not .Cells.Find(sheetname, Lookat:=1) Is Nothing Then
c = .Cells.Find(sheetname, Lookat:=1).Column
Set q = .Cells(2, c).Resize(16)
Sheets("DIR").Activate
Else
MsgBox "請重新確認!"
End If
End With
c = 0
ReDim myArray(0 To 15)
For Each E In q
Set ctrl = E'其實這邊可以不需要了
If ctrl = " " Then myArray(c) = "" Else myArray(c) = ctrl'把ctrl換成E就可以了
c = c + 1
Next
複製代碼
作者:
GBKEE
時間:
2014-12-15 16:09
回復
6#
PKKO
Dim i As Integer
With Sheets(1)
If Not .Cells.Find(sheetname, Lookat:=1) Is Nothing Then
c = .Cells.Find(sheetname, Lookat:=1).Column
Set q = .Cells(2, c).Resize(16)
Sheets("DIR").Activate
Else
MsgBox "請重新確認!"
End If
End With
myArray = Application.WorksheetFunction.Transpose(q)
For i = 1 To q.Count
If ctrl = " " Then myArray(i) = "" Else myArray(i) = ctrl
Next
'***************************
'For i = 1 To UBound(myArray)
' If ctrl = " " Then myArray(i) = "" Else myArray(i) = ctrl
'Next
複製代碼
'***************************
作者:
PKKO
時間:
2014-12-15 23:42
本帖最後由 PKKO 於 2014-12-15 23:43 編輯
回復
7#
GBKEE
感謝超版大大,又再次學習了
但您的意思因該是改為這樣,因為ctrl並沒有使用的必要了
myArray = Application.WorksheetFunction.Transpose(q)
For YY = 1 To q.Count
If myArray(YY) = " " Then myArray(YY) = ""
Next
複製代碼
實驗過了,此方法也完全可行,且撰寫速度更加快速
歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)