-1
我想將數據從一個工作簿複製到另一個工作簿。但是當我通過一個宏快捷鍵(Ctrl + Shift + O)運行它時,它不起作用。程序在調試模式下工作,但不通過快捷方式運行時
但是,當我在調試模式下運行我的程序時,它正常工作。我不明白爲什麼。
下面是代碼:
Option Explicit
Sub CONSUMED()
Dim wp As Workbook, wc As Worksheet
Dim UVAL As Variant, erow As Integer, lastrow As Integer, E As Integer
UVAL = InputBox("Enter a code:") `user will enter the value, which user wants to shift.`
Set wc = ThisWorkbook.Sheets("consumption")
erow = wc.Cells(wc.Rows.Count, 4).End(xlUp).Row
Set wp = Workbooks.Open("F:\report\Book1.xlsm") ' where dtat will move'
For E = erow To 7 Step -1
If Cells(E, 11) = UVAL Then ' when uservalue(uval) meets data from sheet'
Application.CutCopyMode = True
wc.Range(wc.Cells(E, 2), wc.Cells(E, 12)).Copy
With wp.Sheets("Sheet1")
lastrow = .Range("B" & .Rows.Count).End(xlUp).Row + 1
.Range(.Cells(lastrow, 1), .Cells(lastrow, 12)).PasteSpecial xlPasteValues
wc.Range(wc.Cells(E, 2), wc.Cells(E, "XFD")).Delete
End With
E = E - 1
End If
Next
wp.Save
wp.Close True
Application.CutCopyMode = False
End Sub
那不是VB.NET代碼。刪除標籤並閱讀[問]並採取[旅遊]'不完全運行'是一個可怕的問題描述。 – Plutonix
當您打開工作簿時,ActiveSheet會更改。您的For/Next循環頂部的If語句需要被限定在適當的工作表中。 –