IfError + vlookup VBA

时间:2017-11-05 16:38:04

标签: excel-vba vba excel

我正在使用以下VBA代码来测试整数变量。

primerRescateCP = WorksheetFunction.IfError(WorksheetFunction.Match(WorksheetFunction.VLookup("RescateCartera Propia", Range("B:B"), 1, 0), Columns(2), 0), 0)

如果测试结果为真,那么代码运行良好,但如果测试结果为假,我会得到一个"应用程序定义的或对象定义的错误"消息,不要将0赋予变量。

我认为iferror公式编写得很好,所以我无法找到解决方案。

任何建议都应该受到赞赏:-) 在此先感谢!!

3 个答案:

答案 0 :(得分:2)

您无法在VBA中使用IfError(),请考虑:

Sub poiuyt()
    Dim primerRescateCP As Long

    With Application.WorksheetFunction
        On Error Resume Next
            primerRescateCP = .Match(.VLookup("RescateCartera Propia", Range("B:B"), 1, 0), Columns(2), 0)
        On Error GoTo 0
        If Err.Number > 0 Then
            primerRescateCP = 0
            Err.Number = 0
        End If
        MsgBox primerRescateCP
    End With
End Sub

修改#1:

基于评论的更新:

Sub poiuyt_2()
    Dim primerRescateCP As Long

    With Application.WorksheetFunction
        primerRescateCP = 0
        On Error Resume Next
            primerRescateCP = .Match(.VLookup("RescateCartera Propia", Range("B:B"), 1, 0), Columns(2), 0)
        On Error GoTo 0
        MsgBox primerRescateCP
    End With
End Sub

答案 1 :(得分:1)

这是我对它的看法。我给了@Gary的学生整整20分钟的时间让他一起行动; - )

    package com.example.a963fa.ambigo;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Patterns;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Switch;
import android.widget.Toast;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.FirebaseException;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseAuthUserCollisionException;
import com.google.firebase.auth.PhoneAuthCredential;
import com.google.firebase.auth.PhoneAuthProvider;

public class SignUpActivity extends AppCompatActivity implements View.OnClickListener{

    ProgressBar progressBar;
    EditText editTextEmail, editTextPassword;

    private static Button button_signup;

    private FirebaseAuth mAuth;
    private FirebaseAuth firebaseAuth;
    private FirebaseAuth firebaseAuthListner;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign_up);

        editTextEmail = (EditText) findViewById(R.id.EditText_email);
        editTextPassword = (EditText) findViewById(R.id.EditText_password);
        progressBar = (ProgressBar) findViewById(R.id.progressbar);

        mAuth = FirebaseAuth.getInstance();

        firebaseAuth = FirebaseAuth.getInstance();

        firebaseAuthListner = FirebaseAuth.getInstance();

        firebaseAuthListner = (FirebaseAuth.AuthStateListener)(firebaseAuth){

        }

        findViewById(R.id.Button_signup).setOnClickListener(this);

        onClickButtonListener();

    }

    public void onClickButtonListener(){

        button_signup = (Button)findViewById(R.id.Button_signup);
        button_signup.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View view) {
                Intent intent = new Intent("com.example.a963fa.ambigo.MapsActivity");
                startActivity(intent);
            }
        });

    }

private void registerUser(){
    String email = editTextEmail.getText().toString().trim();
    String password = editTextPassword.getText().toString().trim();

    if (email.isEmpty()){
        editTextEmail.setError("E-mail is required");
        editTextEmail.requestFocus();
        return;
    }

    if (!Patterns.EMAIL_ADDRESS.matcher(email).matches()){

        editTextEmail.setError("Please enter a valid E-mail");
        editTextEmail.requestFocus();
        return;
    }

    if (password.isEmpty()){
        editTextPassword.setError("Password is required");
        editTextPassword.requestFocus();
        return;
    }

    if (password.length()<6){
        editTextPassword.setError("Minimum Length of password should be 6");
        editTextPassword.requestFocus();
        return;
    }

    progressBar.setVisibility(View.VISIBLE);

    mAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
        @Override
        public void onComplete(@NonNull Task<AuthResult> task) {

            progressBar.setVisibility(View.GONE);
            if (task.isSuccessful())
            {
                Toast.makeText(getApplicationContext(), "User Registered Successfully", Toast.LENGTH_SHORT).show();
            }
            else {
                if (task.getException() instanceof FirebaseAuthUserCollisionException){
                    Toast.makeText(getApplicationContext(), "You already registered with this E-mail and Password", Toast.LENGTH_SHORT).show();
                }
                else {
                        Toast.makeText(getApplicationContext(), task.getException().getMessage(), Toast.LENGTH_SHORT).show();
                }
            }
        }
    });
}
    @Override
    public void onClick(View view) {

                registerUser();
    }

}

答案 2 :(得分:1)

为避免使用On Error语句,您可以自行测试错误:

Dim resultVLookup As Variant
Dim resultMatch As Variant
Dim primerRescateCP As Variant
primerRescateCP = 0
resultVLookup = Application.VLookup("RescateCartera Propia", Range("B:B"), 1, 0)
If Not IsError(resultVLookup) Then
    resultMatch = Application.Match(resultVLookup, Columns(2), 0)
    If Not IsError(resultMatch) Then
        primerRescateCP = resultMatch
    End If
End If