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

时间:2015-09-30 21:45:03

标签: excel-vba vba excel

此代码正在首次运行,但当我尝试再次运行时,我收到以下错误:

  

运行时错误'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

  Selection.EntireRow.Insert ' Here is where appers the error when I try to do the second run
  ActiveSheet.Cells(3, 1).Select
  ActiveSheet.Cells(1 + 3, 1) = A
  ActiveSheet.Cells(1 + 3, 2) = B
  ActiveSheet.Cells(1 + 3, 3) = C
  ActiveSheet.Cells(1 + 3, 4) = D
  ActiveSheet.Cells(1 + 3, 5) = E
  ActiveSheet.Cells(1 + 3, 7) = F
  ActiveSheet.Cells(1 + 3, 8) = G
  ActiveSheet.Cells(1 + 3, 9) = H
  ActiveSheet.Cells(1 + 3, 10) = I
  ActiveSheet.Cells(1 + 3, 11) = J
  ActiveSheet.Cells(1 + 3, 12) = K
  ActiveSheet.Cells(1 + 3, 13) = L
  ActiveSheet.Cells(1 + 3, 14) = M
  ActiveSheet.Cells(1 + 3, 15) = N
  ActiveSheet.Cells(1 + 3, 16) = O
  excelWB.Saved = True
  Set excelWS = Nothing
  Set excelWB = Nothing
  excelApp.Quit
  Set excelApp = Nothing
  MsgBox "Listo"
End Sub

1 个答案:

答案 0 :(得分:0)

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

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