繁体   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