简体   繁体   中英

vba- If else statement within a for loop

I'd like to add the following that deletes row that contains 0

    If Cells(intFile, "CU") = "0" Then           
        Sheet1.Rows(r).EntireRow.Delete    
    End If    

to my code below.

   If CDate(rst(3)) >= Date Then

       For intCount = 0 To rst.Fields.Count - 1
           strHold = strHold & rst(intCount).Value & "|"

Currently on 'Cells' ElseIf Cells(intFile, "CU") = "0" Then I'm getting compile error: sub or function not defined. what should i put instead of Cells ?

My reference: https://msdn.microsoft.com/en-us/library/752y8abs.aspx

The issue here is that there are many "flavours" of Visual Basic, and they aren't interchangeable.

Your application is running in MSAccess, and it will have certain objects, functions, etc, that are specifically applicable to that environment. The Cells object is something that is specifically applicable to the MS Excel environment and therefore Access VBA has no understanding of what a Cells object is.

(And the documentation you referred to is regarding the syntax for VB.Net - but the specific topic you were looking at is treated the same in all these different versions of "Visual Basic". But it does appear from that web page that the Then in an If statement is an optional keyword in VB.Net, while it is required in VBA, so there's one difference on even such an elementary piece of coding.)

If you change your code from

If CDate(rst(3)) >= Date Then
ElseIf Cells(intFile, "CU") = "0" Then
    Sheet1.Rows(r).EntireRow.Delete

to be

If CDate(rst(3)) >= Date And rst(98) <> 0 Then

I believe you will be doing what you set out to do, ie exclude all records where the 4th field ( 3 due to zero-base) is before today and also exclude all records where the 99th field is 0.

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