Excel VBA动态代码重复失败

时间:2017-05-24 22:59:30

标签: excel vba excel-vba

这段代码有点复杂,但问题是第二次和第三次运行它会开始丢失它从中提取信息的“Base434”工作表上的列。我尝试了一个快速修复添加“Range(”A1“)。选择以便以前突出显示的任何内容都不能将其抛弃但是它一直在放弃第20行,即”T“列。我已将所有代码保留在下面希望有人能找到我的错误。我无法对其进行排序。

本质上,此代码在名为“Base434”的导入工作表上对设置的数据字段进行排序,将特定字段复制到具有一些嵌入式公式的另一个页面,然后检查工作表“NoStdHC”是否存在。如果没有,它将创建所述工作表并添加标题。然后移动到名为“Base434”的过滤工作表并复制该工作表中的所有可见单元格。然后它将它们粘贴在“NoStdHC”A列的第一个可用单元格中。我的问题是在运行它之后,它拒绝复制已导入的下一个“Base434”表上的最后一列。任何人都可以在我的代码中找到错误吗?是的,我知道如果我在编码方面做得更好,可能会有很多这样的内容,但我更愿意理解代码在做什么,这就是为什么我用这种方式写的。

array(2) {
    ["1"]=> string(8) "iPhone 8"
    ["2"]=> string(8) "iPhone 7"
}

2 个答案:

答案 0 :(得分:2)

正如@ A.S.H评论的那样,尽量避免使用/* I have to send json object on Sever for which I have implemented following things but I am getting volley 400 error. */ package com.example.administrator.crmtestapp; import android.app.Activity; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.SearchView; import android.support.v7.widget.Toolbar; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import com.android.volley.AuthFailureError; import com.android.volley.NetworkResponse; import com.android.volley.Request; import com.android.volley.RequestQueue; import com.android.volley.Response; import com.android.volley.ServerError; import com.android.volley.VolleyError; import com.android.volley.VolleyLog; import com.android.volley.toolbox.HttpHeaderParser; import com.android.volley.toolbox.StringRequest; import com.android.volley.toolbox.Volley; import org.json.JSONException; import org.json.JSONObject; import java.io.UnsupportedEncodingException; import java.util.ArrayList; public class BP_Details extends AppCompatActivity implements View.OnClickListener { SearchView searchView; EditText bpCode, bpName, bpAddress1, bpAddress2, bpCity, bpState, bpCountry, payTerm, zipNum, etarea, emailId, tinNum, cstNum, panNum, mobileNum, customerLocation, salesPersonNum; String code,name,address1,address2,city,state,country,zip,area,email,tin,cst,pan,mobile,cLocation,salesPname,pTerm; Toolbar toolbar; String s="CBS"; String s1="NOIDA"; int currency=1; Button btn; int finalCurrency; String curency; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.bp__details); toolbar= (Toolbar) findViewById(R.id.my_toolbar); setSupportActionBar(toolbar); bpCode = (EditText) findViewById(R.id.bp_code); bpName = (EditText) findViewById(R.id.bp_name); bpAddress1 = (EditText) findViewById(R.id.address1); bpAddress2 = (EditText) findViewById(R.id.address2); bpCity = (EditText) findViewById(R.id.bp_city); bpState = (EditText) findViewById(R.id.bp_state); bpCountry = (EditText) findViewById(R.id.bp_country); payTerm = (EditText) findViewById(R.id.payterm); zipNum = (EditText) findViewById(R.id.zip); etarea = (EditText) findViewById(R.id.area); emailId = (EditText) findViewById(R.id.email_id); /*currency = (EditText) findViewById(R.id.currency);*/ tinNum = (EditText) findViewById(R.id.tin_no); cstNum = (EditText) findViewById(R.id.cst_no); panNum = (EditText) findViewById(R.id.pan_no); mobileNum = (EditText) findViewById(R.id.mobile_num); customerLocation = (EditText) findViewById(R.id.customer_location); salesPersonNum = (EditText) findViewById(R.id.salesperson_no); searchView=(SearchView) findViewById(R.id.sear); btn=(Button)findViewById(R.id.btnAdd); btn.setOnClickListener(this); searchView.requestFocus(); code = bpCode.getText().toString().trim(); name=bpName.getText().toString().trim(); address1=bpAddress1.getText().toString().trim(); address2=bpAddress2.getText().toString().trim(); city=bpCity.getText().toString().trim(); state=bpState.getText().toString().trim(); country=bpCountry.getText().toString().trim(); // pTerm=payTerm.getText().toString(); zip=zipNum.getText().toString().trim(); area=etarea.getText().toString().trim(); email=emailId.getText().toString().trim(); /*curency=currency.getText().toString().trim(); finalCurrency=Integer.parseInt(curency.trim());*/ tin=tinNum.getText().toString().trim(); cst=cstNum.getText().toString().trim(); pan=panNum.getText().toString().trim(); mobile=mobileNum.getText().toString().trim(); cLocation=customerLocation.getText().toString().trim(); salesPersonNum.getText().toString().trim(); } @Override public void onClick(View v) { if (v.getId()==R.id.btnAdd){ RequestQueue requestQueue = Volley.newRequestQueue(BP_Details.this); String URL="http://103.75.33.98/BPService/GetAllBPService.svc/SetAllBP"; JSONObject jsonBody = new JSONObject(); try { jsonBody.put("COMPANY_NO",s); jsonBody.put("LOCATION_NO",s1); jsonBody.put("BP_CODE",code); jsonBody.put("NAME",name); jsonBody.put("ADDRESS1",address1); jsonBody.put("ADDRESS2",address2); jsonBody.put("CITY",city); jsonBody.put("STATE",state); jsonBody.put("COUNTRY",country); jsonBody.put("ZIP",zip); jsonBody.put("AREA_CODE",area); jsonBody.put("EMAIL",email); jsonBody.put("CURRENCY",currency); jsonBody.put("TIN_GRN",tin); jsonBody.put("CST_NO",cst); jsonBody.put("PAN_NO",pan); jsonBody.put("SALES_PERSON_NO",salesPname); jsonBody.put("PHONE_NO",mobile); jsonBody.put("LOCATION",cLocation); }catch (JSONException e){ e.printStackTrace(); } final String mRequestBody = jsonBody.toString(); StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() { @Override public void onResponse(String response) { Log.i("response", response); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Log.e("LOG_VOLLEY", error.toString()); // As of f605da3 the following should work NetworkResponse response = error.networkResponse; if (error instanceof ServerError && response!=null){ try { String res=new String(response.data,HttpHeaderParser.parseCharset(response.headers)); }catch (Exception e){ e.printStackTrace(); } } } }){ @Override public String getBodyContentType() { return "application/json;/*charset=utf-8*/"; } @Override public byte[] getBody() throws AuthFailureError { try { return mRequestBody == null ? null : mRequestBody.getBytes("utf-8"); }catch (UnsupportedEncodingException uee) { VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s", mRequestBody, "utf-8"); return null; } } @Override protected Response<String> parseNetworkResponse(NetworkResponse response) { String responseString = ""; if (response != null) { responseString = String.valueOf(response.statusCode); } return Response.success(responseString, HttpHeaderParser.parseCacheHeaders(response)); } }; requestQueue.add(stringRequest); } } } 。范围应使用其工作表名称进行限定。 Select/Activate/ActiveCell构造实现了这两个目标。 With...End With语句允许您对指定对象执行一系列语句,而无需重新限定对象的名称。

缩进使代码更易于阅读和理解。

考虑到上述情况,我认为这段代码是可以理解的

With

End Sub

答案 1 :(得分:1)

如果你想编写代码,你很容易理解你不会写这样的代码: -

Sheets("Base434").Select
Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy

这是你的代码所说的,翻译成简单的语言: -

Look at sheet "Base434"
Look at cell A1 (implied: in that sheet)
Look at what you are looking at and extend your view to the last ??? right
    (This is where the mistake is)
Copy what you are looking at.

现在,当然,如果你想了解所有这些目标是什么,你可能会表达这样的想法: -

Copy the cells in Row 1 of Sheet "Base434" from A1 to the end of the row.

使用这种方法,你最终会得到这样的代码: -

Dim RangeToCopy As Range
Dim Cl As Long                              ' the last used column

With Worksheets("Base434")
    Cl = .Cells(1, .Columns.Count).End(xlToLeft).Column
    Set RangeToCopy = .Range(.Cells(1, 1), .Cells(1, Cl))
End With
MsgBox "Range to copy = " & RangeToCopy.Address
RangeToCopy.Copy

您是否认为此代码比您的版本更难阅读和理解?嗯,它有三个优点,即使它是。一,它没有你的错。第二,它从未接近想要犯下你的方法所犯的错误。三,它可能仍然包含的任何错误都很容易找到并且很快消除。

此外,它运行得更快。