簡體   English   中英

將單元格從一個工作簿復制到另一個工作簿可以逐步執行,但不能在運行時進行

[英]Copying cells from one workbook to another works on step through but not when run

我正在編寫一個從活動工作簿(訂單窗體)中抓取特定單元格,打開指定的工作簿(報表)並按特定順序放入值的子程序。 奇怪的是,當逐步執行時,這很好用,我可以看到報表工作簿已打開並且值已填充,但是運行時它會打開工作簿,但似乎沒有更改。

代碼如下(有點粗糙,已經准備好,我還在學習...)

Sub TransferTheData()

Dim mySheet As Worksheet, myOtherSheet As Worksheet
Dim myOtherBook As Workbook
Dim i As Integer, j As Integer
Dim vToday As String

Set myOtherBook = Workbooks.Open("c:\\Test\Test Report.xlsx")
Set mySheet = ThisWorkbook.Sheets("Order Form")
Set myOtherSheet = myOtherBook.Sheets("Sheet1")

vToday = Format(Date, "dd/mmmm/yyyy")

j = myOtherSheet.Range("A" & Rows.Count).End(xlUp).Row + 1

    For i = 18 To 85
        If mySheet.Cells(i, 1).Value <> "" Then
            'Loop through 68 rows and copy values
            myOtherSheet.Cells(j, 3).Value = mySheet.Cells(i, 1).Value
            myOtherSheet.Cells(j, 4).Value = mySheet.Cells(i, 2).Value
            myOtherSheet.Cells(j, 5).Value = mySheet.Cells(i, 3).Value
            'constant values copied to each row in report sheet
            myOtherSheet.Cells(j, 1).Value = mySheet.Cells(5, 2).Value
            myOtherSheet.Cells(j, 2).Value = mySheet.Cells(9, 5).Value
            myOtherSheet.Cells(j, 8).Value = vToday
            myOtherSheet.Cells(j, 6).Value = mySheet.Cells(9, 2).Value
            myOtherSheet.Cells(j, 7).Value = mySheet.Cells(6, 2).Value
            j = j + 1
        End If
    Next i

End Sub

誰能看到我哪里出問題了? 幾個小時都在盯着它看...

通常,在看了幾個小時然后發布問題后,我就在這里找到了答案!

該主題討論並回答了該問題。 使用工作簿對象的Open方法的鍵盤快捷鍵中沒有Shift鍵(我認為):

Shift鍵使vb腳本在任何Open語句處停止。

將.open切換為.add,我們正在按預期推出完整功能! :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM