VBA:IsEmpty中的Function Cells

时间:2017-04-26 14:26:00

标签: excel vba excel-vba

你可以帮我解决这个VBA代码吗?我认为错误的是IsEmpty函数中的Cells函数,即Excel指责错误的行。由于评论我做了一些更改,但它仍然没有运行。问题在于IsEmpty函数的行。

    Sub preenche_lacunas()

Dim linha_um As Integer
Dim linha_inicio As Integer
Dim linha_final As Integer
Dim coluna_um As Integer
Dim coluna_inicio As Integer
Dim coluna_final As Integer
Dim preenche_com As Integer    


    linha_inicio = 5
    linha_final = 101

    coluna_inicio = 2
    coluna_final = 16

    preenche_com = 0

    linha_um = linha_inicio
    Do While (linha_um <= linha_final)

        coluna_um = coluna_inicio
        Do While (coluna_um <= coluna_final)

            If IsEmpty(Cells(linha_um, coluna_um)) Then

                Cells(linha_um, coluna_um) = preenche_com
            End If

            coluna_um = coluna_um + 1
        Loop

        linha_um = linha_um + 1
    Loop

    End Sub

2 个答案:

答案 0 :(得分:1)

看起来你正试图从5到101和 coluna (即列)遍历 linha (即)从B到P(2到16)并在任何空白单元格中放置零。

with worksheets("Sheet1")
    with .range(.cells(5, 2), .cells(101, 16))
        'make sure that .UsedRange extends at least as far as the extents
        if isempty(.cells(.rows.count, .columns.count)) then _
            .cells(.rows.count, .columns.count) = 0
        'skip over if there are no blank cells
        on error resume next
        .specialcells(xlcelltypeblanks) = 0
        'reset standard error protocol
        on error goto 0
    end with
end with

请注意,包含零长度字符串的单元格(例如公式中的""TEXT(,))并非真正空白。

答案 1 :(得分:0)

更改为以下以获取列号:

coluna_inicio = Columns("B").Column
coluna_final = Columns("P").Column