以下代码摘录旨在将单元格的样式更改为" Bad"如果单元格有公式错误" #REF!"剩下的单元格3列没有" Gesamtergebnis"其中的文字。 但是为什么下面的代码颜色也是HAS" Gesamtergebnis"写下它的三个单元格,就像" AND"的第二部分一样。条款被忽略了?
For Each cell In Final.Worksheets("PIVOT").UsedRange.Cells.SpecialCells(xlFormulas)
If cell.Value = "Error 2023" And cell.Offset(0, -3).Value = "Gesamtergebnis" Then cell.Style = "Bad"
Next cell
谢谢, 鲍尔泰克
答案 0 :(得分:3)
处理案例的更好方法是使用 public class BackgroundWorker extends AsyncTask<String,Void,String> {
Context context;
AlertDialog alertDialog;
public BackgroundWorker(Context ctx) {
context = ctx;
}
@Override
protected String doInBackground(String... params) {
String type = params[0];
String login_url = "http://eateathere.com/login.php";
String register_url = "http://eateathere.com/signup.php";
String navigate = "http://eateathere.com/res_retrieve.php";
if (type.equals("login")) {
try {
String user_name = params[1];
String password = params[2];
URL url = new URL(login_url);
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8"));
String post_data = URLEncoder.encode("user_name","UTF-8")+"="+URLEncoder.encode(user_name,"UTF-8")+"&"
+URLEncoder.encode("password","UTF-8")+"="+URLEncoder.encode(password,"UTF-8");
bufferedWriter.write(post_data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
String result ="";
String line;
while((line = bufferedReader.readLine())!= null){
result += line;
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return result;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
protected void onPreExecute () {
alertDialog = new AlertDialog.Builder(context).create();
alertDialog.setTitle("Status");
}
@Override
protected void onPostExecute (String result){
alertDialog.setMessage(result);
alertDialog.show();
// Intent in = new Intent(context, Homepage.class);
//context.startActivity(in);
if(result == null)
{
// do what you want to do
}
else if (result.contains("Login Success")) // msg you get from success like "Login Success"
{
//I have problem here
String name = params[1];
String pass = params[2];
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
SharedPreferences.Editor edit = sp.edit();
edit.putString("user_name", name);
edit.commit();
SharedPreferences sp2 = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
SharedPreferences.Editor edit2 = sp2.edit();
edit2.putString("password", pass);
edit2.commit();
Intent in = new Intent(context, Homepage.class);
context.startActivity(in);
alertDialog.setMessage(result);
alertDialog.show();
}
参数SpecialCells
。 16
使用这些参数可以选择范围内公式中的错误。
这是你在尝试的吗?
SpecialCells
<强>截图强>
我正在使用Sub Sample()
Dim ErrRange As Range
Dim rng As Range
Dim aCell As Range
Set rng = Final.Worksheets("PIVOT").UsedRange.Cells
On Error Resume Next
Set ErrRange = rng.SpecialCells(xlCellTypeFormulas, 16)
On Error GoTo 0
If Not ErrRange Is Nothing Then
For Each aCell In ErrRange
If aCell.Offset(, -3).Value = "Gesamtergebnis" Then aCell.Style = "Bad"
Next aCell
End If
End Sub
代替aCell.Interior.ColorIndex = 3
进行演示。它将细胞染成红色。
答案 1 :(得分:1)
尝试以下方法
Sub ErrTest()
Dim cell As Range
Dim errval As String
Set cell = Range("A1")
If IsError(cell) Then
errval = cell.Value
Select Case errval
Case CVErr(xlErrDiv0)
MsgBox "#DIV/0! error"
Case CVErr(xlErrNA)
MsgBox "#N/A error"
Case CVErr(xlErrName)
MsgBox "#NAME? error"
Case CVErr(xlErrNull)
MsgBox "#NULL! error"
Case CVErr(xlErrNum)
MsgBox "#NUM! error"
Case CVErr(xlErrRef)
MsgBox "#REF! error"
Case CVErr(xlErrValue)
MsgBox "#VALUE! error"
Case Else
MsgBox "This should never happen!!"
End Select
End If
End Sub
以下是文件https://msdn.microsoft.com/en-us/vba/excel-vba/articles/cell-error-values
根据您的情况,您可以使用以下功能
Function refError(cell As Range) As Boolean
Dim errval As Variant
If IsError(cell) Then
errval = cell.Value
If errval = CVErr(xlErrRef) Then refError = True
End If
End Function
然后你的代码就像
For Each cell In Final.Worksheets("PIVOT").UsedRange.Cells.SpecialCells(xlFormulas)
If refError(cell) And cell.Offset(0, -3).Value = "Gesamtergebnis" Then cell.Style = "Bad"
Next cell