Excel 为什么代码在第二次运行时失败,而不是第一次?

Excel 为什么代码在第二次运行时失败,而不是第一次?,excel,vba,Excel,Vba,此代码在第一次运行时运行,但当我尝试再次运行时,会出现以下错误: 运行时错误“91”:未设置对象变量或带块变量 您有很多不合格的对象引用,这可能会导致您的问题。我想你想要: Private Sub cmdingresar_Click() Dim excelApp As Excel.Application Dim excelWB As Excel.Workbook Dim excelWS As

此代码在第一次运行时运行,但当我尝试再次运行时,会出现以下错误:

运行时错误“91”:未设置对象变量或带块变量


您有很多不合格的对象引用,这可能会导致您的问题。我想你想要:

Private Sub cmdingresar_Click()
    Dim excelApp              As Excel.Application
    Dim excelWB               As Excel.Workbook
    Dim excelWS               As Excel.Worksheet

    Set excelApp = CreateObject("Excel.Application")
    Set excelWB = excelApp.Workbooks.Open("C:\Users\vere\Desktop\carrera desarrollo\visual 6\sistema mascara mahe\Cheques.xlsx")

    excelApp.Visible = False

    A = txtcheque.Text
    B = txtfechadeelaboracion.Text
    C = txtfechadecobro.Text
    E = txtfactura.Text
    F = txtproveedor.Text
    H = txtnumerodecuenta.Text
    I = txtrfc.Text
    L = txtdescripcion.Text
    N = txtneto.Text
    O = txtprecio.Text

    Set excelWS = excelWB.ActiveSheet
    With excelWS

        .Cells(3, 1).EntireRow.Insert
        .Cells(1 + 3, 1).Value = A
        .Cells(1 + 3, 2).Value = B
        .Cells(1 + 3, 3).Value = C
        .Cells(1 + 3, 4).Value = D
        .Cells(1 + 3, 5).Value = E
        .Cells(1 + 3, 7).Value = F
        .Cells(1 + 3, 8).Value = G
        .Cells(1 + 3, 9).Value = H
        .Cells(1 + 3, 10).Value = I
        .Cells(1 + 3, 11).Value = J
        .Cells(1 + 3, 12).Value = K
        .Cells(1 + 3, 13).Value = L
        .Cells(1 + 3, 14).Value = M
        .Cells(1 + 3, 15).Value = N
        .Cells(1 + 3, 16).Value = O
    End With
    ' if you want to save the file, this won't work!
    excelWB.Saved = True
    ' you need to use this
    excelWB.Close savechanges:=True
    Set excelWS = Nothing
    Set excelWB = Nothing
    excelApp.Quit
    Set excelApp = Nothing
    MsgBox "Listo"
End Sub

您好,似乎无法复制错误,请不要使用
selection
尝试选择一个显式范围。这可能是因为你花费了工作表,但没有更新你的选择范围。很好,你就是那个。。。很好的工作,但是我开始在这个。。。我把这个excelWB.Saved=True改为这个excelWB.Save,现在我可以保护文件了,我完成了我的系统来保护信息非常有帮助。。。
Private Sub cmdingresar_Click()
    Dim excelApp              As Excel.Application
    Dim excelWB               As Excel.Workbook
    Dim excelWS               As Excel.Worksheet

    Set excelApp = CreateObject("Excel.Application")
    Set excelWB = excelApp.Workbooks.Open("C:\Users\vere\Desktop\carrera desarrollo\visual 6\sistema mascara mahe\Cheques.xlsx")

    excelApp.Visible = False

    A = txtcheque.Text
    B = txtfechadeelaboracion.Text
    C = txtfechadecobro.Text
    E = txtfactura.Text
    F = txtproveedor.Text
    H = txtnumerodecuenta.Text
    I = txtrfc.Text
    L = txtdescripcion.Text
    N = txtneto.Text
    O = txtprecio.Text

    Set excelWS = excelWB.ActiveSheet
    With excelWS

        .Cells(3, 1).EntireRow.Insert
        .Cells(1 + 3, 1).Value = A
        .Cells(1 + 3, 2).Value = B
        .Cells(1 + 3, 3).Value = C
        .Cells(1 + 3, 4).Value = D
        .Cells(1 + 3, 5).Value = E
        .Cells(1 + 3, 7).Value = F
        .Cells(1 + 3, 8).Value = G
        .Cells(1 + 3, 9).Value = H
        .Cells(1 + 3, 10).Value = I
        .Cells(1 + 3, 11).Value = J
        .Cells(1 + 3, 12).Value = K
        .Cells(1 + 3, 13).Value = L
        .Cells(1 + 3, 14).Value = M
        .Cells(1 + 3, 15).Value = N
        .Cells(1 + 3, 16).Value = O
    End With
    ' if you want to save the file, this won't work!
    excelWB.Saved = True
    ' you need to use this
    excelWB.Close savechanges:=True
    Set excelWS = Nothing
    Set excelWB = Nothing
    excelApp.Quit
    Set excelApp = Nothing
    MsgBox "Listo"
End Sub