【问题标题】:code for communicating between a table workbook and a function workbook in excel -the aftermathexcel表格工作簿和函数工作簿之间通信的代码-后果
【发布时间】:2014-10-04 08:10:21
【问题描述】:

几天前,我发布了一个问题,以了解如何编写代码让我在函数工作簿和输入/表格工作簿之间进行通信。 @DarthSpeedious 慷慨地帮助我想出了在另一个工作簿中计算后返回正确值的代码。

但是,它只返回一个计算。为了尝试循环上次帮助我完成的代码,我尝试为其中一个输入变量创建一个数组,以用作循环过程的计数器。运行代码后,VBA 一直冻结在我身上,因此我无法调试代码。我尝试了几种不同的方法来构造数组和循环参数,但没有一个成功。

谁能帮帮我?

Public Sub Decompact()

    ' Gets input from another workbook
    Dim wb1 As Workbook                                                                             ' Declaring wb1 and wb2 as variable of type Workbook
    Dim wb2 As Workbook

    Set wb1 = Workbooks.Open("decompaction along exmpleline.xlsx")                                  'Note: In order to access data from another workbook, it should be open.
    Set wb2 = ThisWorkbook                                                                          'ThisWorkbook: refrence to the workbook having this code


    'Setting up array for y1
    last_row = Range("U3").End(xlDown).Row

    Dim y1()
    ReDim y1(last_row - 2, 1)

    'Storing values in array
    For i = 3 To last_row - 2
        y1(i, 0) = Range("U" & i + 2)
    Next


    'Looping decompaction
    For z = LBound(y1) To UBound(y1)

        wb1.Sheets("Shaly sst").Range("B3") = wb2.Sheets("Sheet1").Range("U" & i + 1)                   'Accept input of variable y1 in Ui and store it in cell B3 of book1
        wb1.Sheets("Shaly sst").Range("B2") = wb2.Sheets("Sheet1").Range("V" & i + 1)                   'Accept input of variable y2 in Vi and store it in cell B3 of book1

        wb2.Sheets("Sheet1").Range("AC" & i + 1) = wb1.Sheets("Shaly sst").Range("H3")                  'Output from book1 in H3 to a cell ACi in book2

     Next z

End Sub

图资源:https://db.tt/AxFG9snn

【问题讨论】:

  • 作为替代方案,我尝试使用 Cells(i, n) to 和更短的语法: Sub Decompact3() Set wb1 = Workbooks.Open("book1.xlsx") Set wb2 = ThisWorkbook last_row = Range("U3").End(xlDown).Row For i = 3 To last_row wb1.Sheets("Shaly sst").Range("B3") = wb2.Sheets("Sheet1").Cells(i, 21) wb1.Sheets("Shaly sst").Range("B2") = wb2.Sheets("Sheet1").Cells(i, 22) wb2.Sheets("Sheet1").Cells(i, 29) = wb1.Sheets("Shaly sst").Range("H3") Next i End Sub

标签: vba excel


【解决方案1】:

非虚拟机, 想通了:

Sub Decompact3()
' Gets input from another workbook
Dim wb1 As Workbook                                                         ' Declaring wb1 and wb2 as variable of type Workbook
Dim wb2 As Workbook
Dim LastRow As Long

Set wb1 = Workbooks.Open("decompaction along exmpleline.xlsx")                                      'Note: In order to access data from another workbook, it should be open.
Set wb2 = ThisWorkbook                                                      'ThisWorkbook: refrence to the workbook having this code

    With wb2.Sheets("Sheet1")
        LastRow = .Range("U" & .Rows.Count).End(xlUp).Row
    End With

    For i = 3 To LastRow
        wb1.Sheets("Shaly sst").Range("B3") = wb2.Sheets("Sheet1").Cells(i, 21)               'Access value stored in cell A1 of sheet1 in book1 and stre it in cell A1 of book2
        wb1.Sheets("Shaly sst").Range("B2") = wb2.Sheets("Sheet1").Cells(i, 22)

        wb2.Sheets("Sheet1").Cells(i, 29) = wb1.Sheets("Shaly sst").Range("H3")        'Store the output (cell C1 of book2) in cell C1 of book1
    Next i

End Sub

再次感谢 DarthSpeedious 的帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-04
    • 2018-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-23
    • 1970-01-01
    相关资源
    最近更新 更多