返回列表 上一主題 發帖

變動紀錄的程式

寫法就和大大的這篇 http://forum.twbts.com/thread-16677-1-1.html  一樣

只是你裡面有一段寫錯

(Range("B" & WR - 1) <> Range("B2")) Then '總量有異動時才記錄

(Range("B" & (WR - 1)) <> Range("B2")) Then

TOP

呃,  我搞烏龍了
這樣才對

Sub RecordPrice()
Dim WR As Long
Dim I As Byte


If Range("D2") < 1 Then Exit Sub

Excel.Application.EnableEvents = 0


WR = Range("A1").End(xlDown).Row + 1
'ActiveWindow.ScrollRow = WR - 5 '只顯示最新幾筆資料
If (WR = 3) Or _
   (Range("B" & WR - 1) <> Range("B2")) Then '總量有異動時才記錄
    For I = 1 To 3
    Cells(WR, I) = Cells(2, I)
    Next
End If


Excel.Application.EnableEvents = 1


'For I = 1 To 10
'   Cells(WR, I) = Cells(2, I)
'Next 'I
End Sub

TOP

不不, 我不厲害, 這個其他大大都知道, 他們忙而已


Sub RecordPrice()
Dim WR As Long
Dim I As Byte

Excel.Application.EnableEvents = 0

Dim DDE_總量 As Range
Set DDE_總量 = Range("D2")
   
If IsError(DDE_總量.Value) Then Exit Sub
If DDE_總量.Value <= 0 Then Exit Sub

WR = DDE_總量.CurrentRegion.Row + DDE_總量.CurrentRegion.Rows.Count
'ActiveWindow.ScrollRow = WR - 5 '只顯示最新幾筆資料
If (WR = 3) Or _
   (Cells(WR - 1, DDE_總量.Column) <> DDE_總量.Value) Then       '總量有異動時才記錄
    Cells(WR, DDE_總量.Column).Offset(, -1).Resize(, 3).Value = _
                      DDE_總量.Offset(, -1).Resize(, 3).Value
End If

Excel.Application.EnableEvents = 1
End Sub

TOP

本帖最後由 jackyq 於 2016-3-22 08:41 編輯

因為你說你要換位置ㄚ
原本總量的 DDE 在  B2 那就寫成 Set DDE_總量 = Range("B2")
如果總量的 DDE 搬到  D2 那就寫成 Set DDE_總量 = Range("D2")
其他的都不用改
如果堅持用原先那個
每次換位置就要修改不少地方


Sub RecordPrice()
Dim WR As Long
Dim I As Byte


If Range("F2") < 1 Then Exit Sub

Excel.Application.EnableEvents = 0

WR = Range("C1").End(xlDown).Row + 1
'ActiveWindow.ScrollRow = WR - 5 '只顯示最新幾筆資料
If (WR = 3) Or _
   (Range("D" & WR - 1) <> Range("D2")) Then '總量有異動時才記錄
    For I = 3 To 5
    Cells(WR, I) = Cells(2, I)
    Next
End If


Excel.Application.EnableEvents = 1

'For I = 1 To 10
'   Cells(WR, I) = Cells(2, I)
'Next 'I
End Sub

TOP

哪個版本? ..............

TOP

有可能執行到 Excel.Application.EnableEvents = 0 時
還沒執行到 Excel.Application.EnableEvents = 1 就貝你中途 Stop 掉
那你的事件就不會在觸發
OR 其他原因.... 沒檔案沒真相

TOP

改成這樣

    Sub RecordPrice()
Dim WR As Long
Dim I As Byte

Dim DDE_總量 As Range
Set DDE_總量 = Range("D2")
   
If IsError(DDE_總量.Value) Then Exit Sub
If DDE_總量.Value <= 0 Then Exit Sub

WR = DDE_總量.CurrentRegion.Row + DDE_總量.CurrentRegion.Rows.Count
'ActiveWindow.ScrollRow = WR - 5 '只顯示最新幾筆資料
If (WR = 3) Or _
   (Cells(WR - 1, DDE_總量.Column) <> DDE_總量.Value) Then       '總量有異動時才記錄

    Excel.Application.EnableEvents = 0

    Cells(WR, DDE_總量.Column).Offset(, -1).Resize(, 3).Value = _
                      DDE_總量.Offset(, -1).Resize(, 3).Value

    Excel.Application.EnableEvents = 1
End If

End Sub

TOP

存檔後 檔案記得關閉在重開

TOP

回復 15# 藍天麗池


    你的檔案裡面的    ThisWorkBook

Private Sub Workbook_Open()
Application.RTD.ThrottleInterval = 0
''   Application.Calculation = xlCalculationManual     -> 把這個遮罩掉後, 存檔再重開
End Sub

TOP

123_b.zip (215.37 KB)

TOP

        靜思自在 : 知識要用心體會,才能變成自己的智慧。
返回列表 上一主題