VBA Excel:Workbooks.open方法有时只能工作

时间:2017-10-12 22:38:01

标签: excel vba excel-vba ms-office

我正在开发一个非常复杂的系统。我使用了很多工作簿,但是解决我问题的工具基本上是:所有公寓的日历,合同和预订清单。从预订清单我创建合同,在保存合同之前,我填写日历中的期间。当我尝试打开日历时没有任何反应,它什么也没有返回,但是这个问题只会发生,当合同被保留列表打开时!当我手动填写并保存合同时它正常工作,它打开日历很好,返回它的对象。

FillContractFromReservations()

Public Sub CriarContratoR(data_i As Date, data_f As Date, diaria As Long, inquilino As String, txlimp As Long, pgto As Long, dtpgto As Date, edificio As String, apto As String, id_r As Integer)
    path = "\\MICRO01\Meus documentos\CENTRAL DE ARQUIVOS\2. ALUGUEL\2.1 CONTRATOS ALUGUEL\ALUGUEL DE TEMPORADA\TEMPORADA 2017-2018\Contratos 2017-2018\" & edificio & " " & apto & "\" & edificio & " " & apto & ".xlsm"
    Set contrato = Workbooks.Open(path)
    contrato.Worksheets.Add
    i = contrato.Worksheets.Count()
    With contrato.Worksheets(i)
        .Range("C7").Value = inquilino
        .Range("F24").Value = data_i
        .Range("K24").Value = data_f
        .Range("E27").Value = diaria
        .Range("E28").Value = txlimp
        If pgto <> 0 Then
            .Range("E30").Value = pgto
            .Range("G30").Value = dtpgto
        End If
        .Range("K3").Value = id_r
    End With

    contrato.Worksheets(i).Name = inquilino
    contrato.Save
    contrato.Close
end sub

Contract_beforeSave()

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    ThisWorkbook.Worksheets(1).Unprotect Password:="ronryb"
    Dim calendario As Workbook
    Dim WB As Workbook

    Set calendario = Workbooks.Open("\\MICRO01\Meus documentos\CENTRAL DE ARQUIVOS\2. ALUGUEL\2.1 CONTRATOS ALUGUEL\ALUGUEL DE TEMPORADA\TEMPORADA 2017-2018\Sistema\02. Calendário 2017-2018.xlsm")'This is the method that works only sometimes
    Set WB = ThisWorkbook

    Dim i As Integer
    Dim j As Integer

    Dim edificio, apto As String
    edificio = WB.Worksheets("Liquidação").Range("C3").Value
    apto = WB.Worksheets("Liquidação").Range("H3").Value

    Dim c_row As Integer
    i = 8
    While edificio <> calendario.Worksheets("Imoveis Aluguéis").Cells(i, 2).Value Or apto <> calendario.Worksheets("Imoveis Aluguéis").Cells(i, 3).Value
        i = i + 1
    Wend
    c_row = i
    ... THE CODE GOES ON

我很失落,经常搜索,没有发现任何结论。我认为这可能是一个excel bug,但也许我错了。抱歉我的英语不好,有任何疑问我可以在评论中更好地解释,谢谢!

0 个答案:

没有答案