简体   繁体   中英

Excel VBA application-defined or object-defined error

I am having trouble resolving this issue regarding

application-defined or object-defined error

My codes as below

Dim intX as integer
Dim Uname As String
Dim Pword As String
Dim Epin As String

intX = 9
Uname = Cells(intX, 5)

Do
If Cells(intX, 4).Value > 0 Then
    Uname = Cells(intX, 5)
    Pword = Cells(intX, 6)
    Epin = Cells(intX, 7)
    Else
    GoTo Skips
    End If

'Bunch of codes

Skips:
    intX = intX + 1
Err_Clear:
    If Err <> 0 Then
    Err.Clear
    Resume Next
    End If

Loop Until IsEmpty(Uname)

My table basically has a column of login infos and a column of yes no being informed via a 1 = yes and 0 or null = no system, so if the cells(intX,4) has a 1, it will perform the bunch of codes

Uname or Cells(intX,5) has value until the end (or empty cell)

The code runs perfectly until the final loop (it finishes the task but return a application -defined or object-defined error hence the code won't run after the Loop Until IsEmpty(Uname)

The error points to

If Cells(intX, 4).Value > 0 Then

Any help please?

While I haven't been able to test this code, I sure hope that it is able to run without problems.

Dim intX As Integer
Dim Uname As String
Dim Pword As String
Dim Epin As String

For intX = 9 To 1000000
    If Trim(Sheet1.Cells(intX, 5).Value2) = vbNullString Then Exit Sub
    If IsNumeric(Sheet1.Cells(intX, 4).Value2) Then
        If Sheet1.Cells(intX, 4).Value2 > 0 Then
            Uname = Sheet1.Cells(intX, 5).Value2
            Pword = Sheet1.Cells(intX, 6).Value2
            Epin = Sheet1.Cells(intX, 7).Value2
        End If
    Else
        GoTo Skips
    End If

    'Bunch of codes

Skips:
Err_Clear:
    If Err <> 0 Then
        Err.Clear
        Resume Next
    End If

Next lngrow

Here are the things I changed: - explicit coding vs implicit coding using Sheet1. (or what ever sheet you are referring to up front to ensure that Excel is using the sheet you want it to - explicit coding again using .value2 at the end - before checking .Cells(intX, 4).value2 if it is above zero I added another check if it is actually numeric - exchanging the endless loop for a definite one - replacing IsEmpty for a comparison against vbNullString as IsEmpty per help file should be only used to check if a variable has beeen inizialized. But a cell isn't really a variable.

Hope this helped.

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