VBA其他工作簿没有正确关闭

时间:2017-10-24 13:30:15

标签: excel vba excel-vba save

您好我的VBA代码不想正确关闭我的原始数据文件。 我有一个数据捕获表单然后,当我点击提交,打开另一个Excel文件,将所有数据复制到其中,并应该保存并关闭工作簿。 我的代码如下:

Private Sub submit_Click()

'Open Workbook
If MsgBox("You are about to Submit, Are you sure?" & vbCr & "Please make sure that the OUTCOME box is complete", vbYesNo) = vbNo Then Exit Sub


Dim wb As Workbook, sh As Worksheet
Set wkb = Workbooks.Open("\\ServerName\Reports Folder\Team Name\Manager Name\RAW\RAW QC data.xlsx")

'Make Daily_Tracking_Dataset active
'Determine emptyRow
'Transfer Information

Set wb = Workbooks("RAW QC data.xlsx")

Set sh = wb.Sheets(1)
cAry = Array(Me.QCBX, Me.CallBX, Me.INBX, Me.AgntBX, Me.VoxBX, Me.ClntBX, Me.PolBX, Me.DateBX1, Me.AuditBX1, Me.TextBox7, Me.TextBox8, Me.OUTBX1, Me.Cbx1_1, Me.Cbx1_2, Me.Cbx1_3, Me.Cbx1_4, Me.OUTBX2, Me.Cbx2_1, Me.Cbx2_2, Me.Cbx2_3, Me.OUTBX3, Me.Cbx3_1, Me.Cbx3_2, Me.OUTBX4, Me.Cbx4_1, Me.Cbx4_2, Me.Cbx4_3, Me.OUTBX5, Me.Cbx5_1, Me.Cbx5_2, Me.Cbx5_3, Me.Cbx5_4, Me.Cbx5_5, Me.Cbx5_6, Me.Cbx5_7, Me.Cbx5_8, Me.ACBX, Me.QTBX, Me.QFBX)
    With sh
        For i = 1 To 39
            .Cells(Rows.Count, i).End(xlUp)(2) = cAry(i - 1).Value
        Next
    End With

'Save the Raw data
wb.Close SaveChanges:=True

End Sub

发生的事情是它看起来好像有效但当我尝试提交下一个时,它给了我SAVE AS窗口

1 个答案:

答案 0 :(得分:2)

您的代码中同时包含wkbwb。可能这不是你想要的。只需尝试在代码中将wkb更改为wb即可。这是开始使用Option Explicit - What do Option Strict and Option Explicit do?

的一个很好的理由

一般来说,这样简单的事情应该有效:

Public Sub TestMe()

    'Dim wb      As Workbook
    Dim wkb     As Workbook
    Dim sh      As Worksheet

    Set wkb = Workbooks.Open(ThisWorkbook.Path & "\Testing.xlsx")
    'Set wb = Workbooks("Testing.xlsx")

    'Put your loop instead of the TEST later:
    wb.Worksheets(1).Cells(1, 1) = "TEST"

    Application.DisplayAlerts = False
    wkb.Save
    Application.DisplayAlerts = True
    wkb.Close

End Sub