遍历列并替换缺少的值

时间:2018-07-06 11:02:10

标签: vba loops replace

我试图遍历各个列,并用一个值替换一个空白单元格,但是出现了两个错误:

1。)我知道计数应为8,但返回值为9

2。)代码的最后一行“ .value =”返回应用程序定义或对象定义的错误

我的测试数据从“ D4”开始看起来像这样:

1   1   1       
    2   2       
3       3       
4   4           
5   5   5       
6   6   6       
7   7   7       
8   8   8       
9   9   9       



Sub test()
Dim c As Integer, nextfree As Integer, rangesum As Integer


For c = 4 To 6
  If Range(Cells(4, c), Cells(12, c)).Count = 8 Then

    nextfree = Range(Cells(4, c), Cells(12, c)).Cells.SpecialCells(xlCellTypeBlanks).Row
    rangesum = Excel.WorksheetFunction.Sum(Worksheets("Sheet1").Range(Cells(4, c), Cells(12, c)))
    Worksheets("Sheet1").Range(Cells(nextfree, c)).Value = rangesum * 2

    End If
Next c
End Sub

1 个答案:

答案 0 :(得分:0)

尝试:

cmap = plt.cm.get_cmap('viridis_r')
cmap.set_under(cmap(1.0))

surf = ax.plot_surface(..., cmap=cmap, ...)

1)首先使用Sub test() Dim c As Integer, nextfree As Integer, rangesum As Integer For c = 4 To 6 If WorksheetFunction.CountA(Range(Cells(4, c), Cells(12, c))) = 8 Then nextfree = Range(Cells(4, c), Cells(12, c)).Cells.SpecialCells(xlCellTypeBlanks).Row rangesum = Excel.WorksheetFunction.Sum(Worksheets("Sheet1").Range(Cells(4, c), Cells(12, c))) Worksheets("Sheet1").Cells(nextfree, c).Value = rangesum * 2 End If Next c End Sub 返回8。它仅计算非空单元格。 WorksheetFunction.CountA(Range(Cells(4, c), Cells(12, c)))正在计算范围内的每个单元格-即使它为空,这也是为什么它返回9

2)您不需要在那里使用Range。直接从Cells方法中指向单元格地址即可。 Range(Cells(4, c), Cells(12, c)).Count代替Cells(nextfree, c).Value