標題:
[發問]
執行活頁簿內所有工作表的迷思
[打印本頁]
作者:
alex520188
時間:
2011-8-22 12:56
標題:
執行活頁簿內所有工作表的迷思
各位先進
可否幫小弟看一下 以下語法 哪裡有錯
我要處理活頁簿理的所有工作表 可是他只能在同一個ws內跑"目前工作表的數量"的次數 沒辦法一個工作表跑完 跑下一個˙ 或是各位大大有更好的寫法?!
Sub A123()
Dim I%, J%, k%
Dim Current As Worksheet
For Each Current In Worksheets
J = Rows(1).End(xlToRight).Column
J = J - 1
Columns(J).Insert Shift:=xlToRight
k = Range("a:a").End(xlDown).Row
For I = 2 To k Step 1
Cells(I, (J + 1)).Formula = "=IF(ISBLANK(OFFSET(0,-1)),"",OFFSET(0,-1)-OFFSET(0,-2))"
Cells(I, (J + 2)).Formula = "=IF(ISBLANK(OFFSET(0,-2)),"",B3+(OFFSET(0,-2)*0.001)-ROUND(RAND()*0.00005,5))"
If Cells(I, (J - 1)) = "" Then
Cells(I, J) = ""
ElseIf Cells(I, (J - 1)) - Cells(I, (J - 2)) > 0 Then
Randomize
Cells(I, J) = Round(((-0.1 - -0.4) * Rnd + -0.4), 1) + Cells(I, (J - 1))
ElseIf Cells(I, (J - 1)) - Cells(I, (J - 2)) < 0 Then
Randomize
Cells(I, J) = Round(((0.3 - 0.1) * Rnd + 0.1), 1) + Cells(I, (J - 1))
End If
Next I
Cells(1, J) = Now()
MsgBox Current.Name
Next
End Sub
作者:
GBKEE
時間:
2011-8-22 13:26
回復
1#
alex520188
Sub A123()
Dim I%, J%, k%
Dim Current As Worksheet
For Each Current In Worksheets
With Current
J = .Rows(1).End(xlToRight).Column
J = J - 1
.Columns(J).Insert Shift:=xlToRight
k = .Range("a:a").End(xlDown).Row
For I = 2 To k Step 1
.Cells(I, (J + 1)).Formula = "=IF(ISBLANK(OFFSET(0,-1)),"",OFFSET(0,-1)-OFFSET(0,-2))"
.Cells(I, (J + 2)).Formula = "=IF(ISBLANK(OFFSET(0,-2)),"",B3+(OFFSET(0,-2)*0.001)-ROUND(RAND()*0.00005,5))"
If .Cells(I, (J - 1)) = "" Then
.Cells(I, J) = ""
ElseIf .Cells(I, (J - 1)) - .Cells(I, (J - 2)) > 0 Then
Randomize
.Cells(I, J) = Round(((-0.1 - -0.4) * Rnd + -0.4), 1) + .Cells(I, (J - 1))
ElseIf .Cells(I, (J - 1)) - .Cells(I, (J - 2)) < 0 Then
Randomize
.Cells(I, J) = Round(((0.3 - 0.1) * Rnd + 0.1), 1) + .Cells(I, (J - 1))
End If
Next I
.Cells(1, J) = Now()
MsgBox .Name
End With
Next
End Sub
複製代碼
作者:
alex520188
時間:
2011-8-22 14:17
版大 謝謝你的回覆
不過加了這行 With Current 之後
一樣也不能跑所有的活頁簿
另外我想問一下
Cells(I, (J + 1)).Formula = "=IF(ISBLANK(OFFSET(0,-1)),"",OFFSET(0,-1)-OFFSET(0,-2))"
Cells(I, (J + 2)).Formula = "=IF(ISBLANK(OFFSET(0,-2)),"",B3+(OFFSET(0,-2)*0.001)-ROUND(RAND()*0.00005,5))"
這兩行code 要怎改寫 因為我是從execl公式直接貼上的 執行上會卡住~"~
作者:
GBKEE
時間:
2011-8-22 14:42
回復
3#
alex520188
一樣也不能跑所有的活頁簿
要跑所有的活頁簿嗎?
For Each Wo In Workbooks
For Each Current In Wo.Worksheets
With .Cells(I, J + 1)
.Formula = "=IF(ISBLANK(" & .Offset(0, -1).Address & "),""""," & .Offset(0, -1).Address & " - " & .Offset(0, -2).Address & ")"
End With
With .Cells(I, J + 2)
.Formula = "=IF(ISBLANK(" & .Offset(0, -2).Address & "),"""",B3+(" & .Offset(0, -2).Address & "*0.001)-ROUND(RAND()*0.00005,5))"
End With
Next
Next
作者:
alex520188
時間:
2011-8-22 23:34
版大 謝謝你的回覆
這些code已經能正常使用
不過小弟還是有問題想請教
"=IF(ISBLANK(" & .Offset(0, -1).Address & "),""""," & .Offset(0, -1).Address & " - " & .Offset(0, -2).Address & ")"
這一串 & .Offset(0, -1).Address & 為何要這樣寫 我不懂他的語法
For Each Current In Wo.Worksheets
其中 Wo.Worksheets 是什麼意思?
作者:
alex520188
時間:
2011-8-26 18:05
回復
4#
GBKEE
版大 謝謝你的回覆
這些code已經能正常使用
不過小弟還是有問題想請教
"=IF(ISBLANK(" & .Offset(0, -1).Address & "),""""," & .Offset(0, -1).Address & " - " & .Offset(0, -2).Address & ")"
這一串 ("& .Offset(0, -1).Address & ") 為何要這樣寫 我不懂他的語法
For Each Current In Wo.Worksheets
其中 Wo.Worksheets 是什麼意思?
作者:
GBKEE
時間:
2011-8-27 15:32
回復
6#
alex520188
Offset 是
Range
物件的屬性 ,用以代表
某個指定區域
以外的範圍。唯讀。
With Range("a1")
MsgBox .Offset( 0, 0).Address
MsgBox .Offset(5).Address
MsgBox .Offset(5 , 2).Address
End With
你是否知了解 For Each Current In
Worksheets
工作表集合物件
同理
For Each Wo In
Workbooks
活頁簿集合物件
作者:
alex520188
時間:
2011-8-30 16:34
回復
7#
GBKEE
謝謝版大
小弟已了解
歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)