从一个工作簿到另一个工作簿的Excel宏复制列

时间:2017-08-25 12:45:40

标签: excel excel-vba vba

我有一本工作簿:“检查日志记录”。从我想要复制列“A”并将其粘贴到工作簿中:“单元测试模板”。在“A”栏中。

我一直在尝试使用以下代码:

Set targetColumn = Workbooks("Unit test template.xlsm").Worksheets("Sheet1").Columns("A")

虽然每次我尝试运行宏时,都会收到一条消息:“运行时错误'9':下标超出范围”然后突出显示该行:

public class MainActivity extends AppCompatActivity {
    private static final String LOGTAG = "info";
    private static final String QUOTES_API = "https://andruxnet-random-famous-quotes.p.mashape.com/?cat=movies&count=1";
    private static final String MASHAPE_KEY = "myapikey";

    TextView quotesTextView, quotesSourceTextView;

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

        Button newQuoteBtn = (Button) findViewById(R.id.quotesBtn);
        quotesTextView = (TextView) findViewById(R.id.quotesText);
        quotesSourceTextView = (TextView) findViewById(R.id.quotesSourceText);

        newQuoteBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                new GetQuote().execute();
            }
        });
    }

    private class GetQuote extends AsyncTask<Void, Void, Void> {
        private String quote, quoteSource;

        @Override
        protected Void doInBackground(Void... voids) {
            try {
                URL quotesURL = new URL(QUOTES_API);
                HttpsURLConnection conn = (HttpsURLConnection) quotesURL.openConnection();
                conn.setRequestProperty("X-Mashape-Key", MASHAPE_KEY);
                conn.setRequestProperty("Accept", "application/json");

                if(conn.getResponseCode() == 200) {
                    InputStream inputStream = conn.getInputStream();
                    InputStreamReader isReader = new InputStreamReader(inputStream, "UTF-8");

                    BufferedReader buffReader = new BufferedReader(isReader);
                    StringBuffer json = new StringBuffer(1024);
                    String tmp="";
                    while((tmp=buffReader.readLine())!=null) {
                        json.append(tmp).append("\n");
                    }
                    buffReader.close();

                    JSONObject data = new JSONObject(json.toString());
                    Log.i(LOGTAG, data.getString("quote"));
                    quote = data.getString("quote");
                    quoteSource = data.getString("author");
                } else {
                    quote = "Response code: " + conn.getResponseCode();
                    quoteSource = "Mashape";
                }
            } catch (Exception e) {
                e.printStackTrace();
                quote = e.getMessage();
                quoteSource = "Exception Class";
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            quotesTextView.setText(quote);
            quotesSourceTextView.setText(quoteSource);
            super.onPostExecute(aVoid);
        }
    }

}

当我尝试运行宏时,两个工作簿都处于打开状态。

如果有人能提供帮助,我将非常感谢

1 个答案:

答案 0 :(得分:1)

  

“运行时错误'9':下标超出范围”。

它几乎无法在工作簿中找到两个“单元测试template.xlsm”或“Sheet1”中的一个。

工作簿问题..
1.检查文件名是否为“单元测试template.xls x ”(不是.xls m
2.检查文件“Unit test template.xlsm”是否在与“Check for logging.xlsm”相同的会话中打开(如果Ctrl + Tab没有带您到下一个工作簿,这意味着它们以两种不同的方式打开Excel应用程序会议)
3.文件名略有偏离。

工作表问题..
1.检查“Sheet1”是否已删除。所以它实际上可能是“Sheet2”等......