在附加檔案裡,excel檔,裡面是所寫程式的某部份
我在【Sheet台選】中增加了一個CommandButton1,名為【計算】,也寫好程式碼,按下就會計算【Sheet1台選】裡的程式。
程式碼如下:
Private Sub CommandButton1_Click()
' MsgBox Range("n5").Value
Dim i As Integer
For i = 5 To 14
Call Macro1(i)
Next
End Sub
現在我重新想了方式 附件如旁: [attach]14828[/attach]
若我在【台選】,寫好了以下的程式碼:
Private Sub CommandButton1_Click()
' MsgBox Range("n5").Value
Dim i As Integer
For i = 5 To 14
Call Macro1(i)
Next
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
' MsgBox Range("n5").Value
Dim i As Integer
For i = 5 To 14
Call Macro1(i)
所以我可以直接複製【台選】的程式碼貼在【台選2】裡嗎?如下:
Private Sub Worksheet_Change(ByVal Target As Range)
' MsgBox Range("n5").Value
Dim i As Integer
For i = 5 To 14
Call Macro1(i)
Next
End Sub
Private Sub Worksheet_Calculate() '重算事件程式碼
Dim i As Integer
Application.EnableEvents = False '停止 觸發事件
For i = 5 To 14
Call Macro1(i)
Next
Application.EnableEvents = True '恢復 觸發事件
End Sub
而在模組裡的module2的程式碼可以這樣寫嗎?
Sub Macro1(r As Integer) 'r為參數代表列號.integer為整數型態
With Sheets("台選")
.Range("M" & r).GoalSeek Goal:=.Range("N" & r).Value, ChangingCell:=.Range("D" & r)
End With
With Sheets("台選2")
.Range("M" & r).GoalSeek Goal:=.Range("N" & r).Value, ChangingCell:=.Range("D" & r)
End With