关闭文件时出错

时间:2017-02-11 10:53:42

标签: excel vba

我有下面的代码工作正常,但当我关闭文件时,我有错误代码。 问题在于我想要在C24之后搜索“C”列中的下一个空单元格的逻辑。

dropna()

关闭文件时的错误现在是: “对象变量或未设置块变量”

排队:

def fill_with_nearest(df):
    s = df.set_index('date').b
    s = s.dropna().reindex(s.index, method='nearest')
    s.index = df.index
    return s

df.loc[df.b.isnull(), 'b'] = df.groupby('a').apply(fill_with_nearest).reset_index(0, drop=True)

print(df)

   a    b       date
0  1  4.0 2017-01-10
1  1  4.0 2017-02-09
2  1  6.0 2016-02-10
3  2  5.0 2017-01-10
4  2  5.0 2017-01-11
5  2  7.0 2016-02-10

正如您所看到的,我在使用选项之前也尝试过:

Private Sub ComboBox21_Change()
Dim NextFree As String
Dim Comboindex As Integer
Dim Combovalue As String

Comboindex = Sheet1.ComboBox21.ListIndex + 1
Combovalue = Sheet1.ComboBox21.Value

'NextFree = Range("C24:C" & Rows.Count).Cells.SpecialCells(xlCellTypeBlanks).Row

Dim ws As Worksheet
Set ws = ActiveSheet
For Each cell In ws.Range("C24:C100")
    If IsEmpty(cell) = True Then
        NextFree = cell.Row
              Exit For
    End If
Next cell

If NextFree > 25 Then
Set ws1 = ThisWorkbook.Sheets("Feuil1")
With ws1
    Set Rng = .Rows(NextFree - 1)
    Rng.Copy
    Rng.Offset(1).Insert Shift:=xlDown
    Application.CutCopyMode = False
    .Range("C" & NextFree & ":H" & NextFree).ClearContents
End With
End If

Worksheets("Feuil1").Cells(NextFree, 3).Value = Worksheets("Feuil2").Cells(Comboindex, 1).Value
Worksheets("Feuil1").Cells(NextFree, 5).Value = Worksheets("Feuil2").Cells(Comboindex, 2).Value
Worksheets("Feuil1").Cells(NextFree, 6).Value = Worksheets("Feuil2").Cells(Comboindex, 3).Value
Worksheets("Feuil1").Cells(NextFree, 8).Value = "=+F" & NextFree & "-(G" & NextFree & "*F" & NextFree & ")"

TotalHTF = "=SUM(H25:H" & NextFree & ")"
Worksheets("Feuil1").Cells(NextFree + 1, 8).Value = TotalHTF
TotalHT = Worksheets("Feuil1").Cells(NextFree + 1, 8).Value

TVAF = "=H" & NextFree + 1 & "*0.2"
Worksheets("Feuil1").Cells(NextFree + 2, 8).Value = TVAF
TVA = Worksheets("Feuil1").Cells(NextFree + 2, 8).Value

NetF = "=H" & NextFree + 1 & "+H" & NextFree + 2
Worksheets("Feuil1").Cells(NextFree + 3, 8).Value = NetF
Net = Worksheets("Feuil1").Cells(NextFree + 3, 8).Value

End Sub

这也有效,但在关闭文件时出现以下错误: “无法获得Range类的SpecialCells属性”

任何想法怎么做?

此致

布赖恩

1 个答案:

答案 0 :(得分:0)

  • 将sheet1更改为Worksheet(“Sheet1”) - 顶部的组合框项目仍显示sheet1
  • Dim ws1 as worksheet因为您目前没有定义它。
  • Dim nextfree as long因为它是一个数字
  • 另一件事。将底部的代码更改为:

    Dim TotalHT As Variant,TVA As Variant,Net As Variant

    工作表(“Feuil1”)。单元格(NextFree + 1,8).Formula =“= SUM(H25:H”& NextFree&“)” TotalHT =工作表(“Feuil1”)。单元格(NextFree + 1,8).Value

    工作表(“Feuil1”)。单元格(NextFree + 2,8).Formula =“= H”& NextFree + 1& “* 0.2” “” TVA =工作表(“Feuil1”)。单元格(NextFree + 2,8).Value

    工作表(“Feuil1”)。单元格(NextFree + 3,8).Formula =“= H”& NextFree + 1& “+ H”& NextFree + 2 Net =工作表(“Feuil1”)。单元格(NextFree + 3,8).Value

请尝试以下完整代码:

Private Sub ComboBox21_Change()
Dim Comboindex As long, NextFree As long
Dim Combovalue As String
Dim TotalHT As Variant, TVA As Variant, Net As Variant
Dim ws As Worksheet, ws1 As Worksheet

Comboindex = Worksheets("Feuil1").ComboBox21.ListIndex + 1
Combovalue = Worksheets("Feuil1").ComboBox21.Value

'NextFree = Range("C24:C" & Rows.Count).Cells.SpecialCells(xlCellTypeBlanks).Row

Set ws = ActiveSheet
For Each cell In ws.Range("C24:C100")
    If IsEmpty(cell) = True Then
        NextFree = cell.Row
              Exit For
    End If
Next cell

If NextFree > 25 Then
Set ws1 = ThisWorkbook.Sheets("Feuil1")
    With ws1
        Set Rng = .Rows(NextFree - 1)
        Rng.Copy
        Rng.Offset(1).Insert Shift:=xlDown
        Application.CutCopyMode = False
        .Range("C" & NextFree & ":H" & NextFree).ClearContents
    End With
End If

Worksheets("Feuil1").Cells(NextFree, 3).Value = Worksheets("Feuil2").Cells(Comboindex, 1).Value
Worksheets("Feuil1").Cells(NextFree, 5).Value = Worksheets("Feuil2").Cells(Comboindex, 2).Value
Worksheets("Feuil1").Cells(NextFree, 6).Value = Worksheets("Feuil2").Cells(Comboindex, 3).Value
Worksheets("Feuil1").Cells(NextFree, 8).Formula = "=+F" & NextFree & "-(G" & NextFree & "*F" & NextFree & ")"

Worksheets("Feuil1").Cells(NextFree + 1, 8).Formula = "=SUM(H25:H" & NextFree & ")"
TotalHT = Worksheets("Feuil1").Cells(NextFree + 1, 8).Value

Worksheets("Feuil1").Cells(NextFree + 2, 8).Formula = "=H" & NextFree + 1 & "*0.2"""
TVA = Worksheets("Feuil1").Cells(NextFree + 2, 8).Value

Worksheets("Feuil1").Cells(NextFree + 3, 8).Formula = "=H" & NextFree + 1 & "+H" & NextFree + 2
Net = Worksheets("Feuil1").Cells(NextFree + 3, 8).Value

End Sub