根据行条目评估范围并写入另一列

时间:2016-02-06 17:11:53

标签: excel vba excel-vba

我想查看范围并查找标记某些值,然后根据这些值填充另一列中的同一行。

Column 1       Column 2
Code1
Code10

如果我在宏中指定Code1,我想填充第2列。

我有以下代码。我收到运行时错误。如何避免运行时错误。

Sub Test()

With Sheets("Sheet1")

    Set r = .Range("A:A").Find(what:="Old", LookIn:=xlValues)
    Dim rownum As Long
    rownum = r.Row
    Dim rownum2 As Long
    rownum2 = rownum - 1

    With Sheets("Sheet1")

        Set r2 = .Range("C6:C" & rownum2)
        Dim cell As Variant

        For Each cell In r2
           If Cells.Value = ("Code1" Or "Code2" Or "Code3" Or "Code4") Then
                .Select
                .Offset(0, 7).Value = "Special"
            End If
        Next cell

    End With

End With

End Sub

1 个答案:

答案 0 :(得分:2)

我把它清理了一下:

public class BackgroundTask extends AsyncTask<String, Void, String> {
    Context ctx;
    AlertDialog alertDialog;

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        alertDialog=new AlertDialog.Builder(ctx).create();
    }

    public BackgroundTask(Context ctx) {
        // TODO Auto-generated constructor stub
        this.ctx=ctx;
    }

    @Override
    protected String doInBackground(String... params) {
        // TODO Auto-generated method stub
        String reg_url="http://10.0.2.2/findDoctor/register.php";
        String method=params[0];
        if(method.equals("register")) {
            String type=params[1];
            String YourName=params[2];
            String regNum=params[3];
            String fatherName=params[4];
            String gender=params[5];
            String mobileNumber=params[6];
            String password=params[7];

            try {
                URL url=new URL(reg_url);
                HttpURLConnection connection=(HttpURLConnection) url.openConnection();
                connection.setRequestMethod("POST");
                connection.setDoOutput(true);
                OutputStream OS=connection.getOutputStream();
                BufferedWriter bufferedWriter=new BufferedWriter(new OutputStreamWriter(OS));
                String Data=URLEncoder.encode("type","UTF-8")+"="+URLEncoder.encode(type,"UTF-8")+"&"+
                    URLEncoder.encode("YourName","UTF-8")+"="+URLEncoder.encode(YourName,"UTF-8")+"&"+
                    URLEncoder.encode("regNum","UTF-8")+"="+URLEncoder.encode(regNum,"UTF-8")+"&"+
                    URLEncoder.encode("fatherName","UTF-8")+"="+URLEncoder.encode(fatherName,"UTF-8")+"&"+
                    URLEncoder.encode("gender","UTF-8")+"="+URLEncoder.encode(gender,"UTF-8")+"&"+
                    URLEncoder.encode("mobileNumber","UTF-8")+"="+URLEncoder.encode(mobileNumber,"UTF-8")+"&"+
                    URLEncoder.encode("password","UTF-8")+"="+URLEncoder.encode(password,"UTF-8");

                bufferedWriter.write(Data);
                bufferedWriter.flush();
                bufferedWriter.close();
                OS.close();
                InputStream IS=connection.getInputStream();
                IS.close();
                return "registration successful";
            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        alertDialog.setMessage(result);
        alertDialog.show();
        if (result.endsWith("Registration seccussfull")) {
            Toast.makeText(ctx, result,Toast.LENGTH_LONG).show();
        } else if(result.endsWith("already found")){
            alertDialog.setMessage("its works");
            alertDialog.show();
        }
    }
}

主要问题是If声明。请参阅上文,了解使用Sub Test() Dim r2 As Range Dim rownum2 As Long Dim cell As Range With Sheets("Sheet1") rownum2 = .Range("A:A").Find(what:="Old", LookIn:=xlValues).Row - 1 Set r2 = .Range("C6:C" & rownum2) End With For Each cell In r2 If cell.Value = "Code1" Or cell.Value = "Code2" Or cell.Value = "Code3" Or cell.Value = "Code4" Then cell.Offset(0, 7).Value = "Special" End If Next cell End Sub 语句的正确方法。