简体   繁体   中英

Excel-VBA : Skip subroutine if cell is empty

Ok, so I have 10 columns, labelled "A"-"J".

Each Row will have some combination of these columns filled in with string values.

I need to run some conditional statements and I'm wondering if there is a more efficient method of doing them without simply looping through them all.

What I have now:

If isempty("A1) then
Else
    if isempty("B1") then
    else
        Sheet2!"B1" = "A1  and B1"
    end if
    if isempty("C1") then
    else
        Sheet2!"A1" = "A1 and C1"
    end if
    [...etc]
end if

If isempty("B1) then
Else
    if isempty("C1") then
    else
        Sheet2!"B1" = "B1 and C1"
    end if
    if isempty("D1") then
    else
        Sheet2!"C1" = "C1 and D1"
    end if
    [...etc]
end if

It's long, cumbersome, and not very pretty. Moreover, it takes a long time because we have a few hundred records (rows) to go through. Is there a faster way to look at X Row, say A,B,C,E,&J have things, and do stuff based on that.

If A,C,&J are filled Do this.. 
If B is empty do this...
If C Or D is full, do this other thing.

I'm not entirely sure of the order in which cells should be checked but perhaps this will get you started.

Dim rw As Long, lr As Long
With Cells(1, 1).CurrentRegion
    lr = .Rows.Count
    For rw = 1 To lr
        If Application.CountA(Range("A" & rw & ",C" & rw & ",J" & rw)) = 3 Then
            'If A,C,&J are filled Do this..
        ElseIf IsEmpty(Range("B" & rw)) Then
            'If B is empty do this...
        ElseIf CBool(Application.CountA(Range("C" & rw & ",D" & rw))) Then
            'If C Or D is full, do this other thing.
        End If
    Next rw
End With

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM