我有下面的代码工作正常,但是当我关闭文件时出现错误代码。 问题在于我想要在C24之后搜索列“C”中的下一个空单元的逻辑。关闭文件时出错
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
至于现在关闭文件时错误是: “对象变量或With块变量未设置”
在行:
For Each cell In ws.Range("C24:C100")
正如你所看到的,我已经试过也使用该选项之前:
'NextFree = Range("C24:C" & Rows.Count).Cells.SpecialCells(xlCellTypeBlanks).Row
这也行得通,但有误差当关闭文件时: “无法获得Range类的SpecialCells属性”
任何想法如何做到这一点?
问候,
布赖恩
你收什么文件?每个循环的 – user3598756
工作正常。您需要用Worksheets(“Sheet1”)和Worksheets(“Sheet2”)替换'sheet1'和'sheet2',或者在开头定义它们。 –
错误是当我关闭具有此代码的excel文件时。现在我已经将sheet1中的代码更改为Worksheet(“Sheet1”),但当文档关闭时仍然有相同的错误。初始代码已更新。错误:“对象变量或块变量未设置” – Brian