将其他工作簿中的内容复制并粘贴到当前工作簿中

时间:2016-03-13 15:08:15

标签: excel vba excel-vba

我有一些代码可以从网上下载excel文件。我需要它将其内容复制并粘贴到我当前的工作簿中,但我得到“下标超出范围”错误。代码如下,并提前感谢!

    @SuppressWarnings("unchecked")
    @Override
    public List<Users> listUsersSort(int weight, String gender, String place, int ageTo, String currentUser) {
        System.out.println(weight+gender+place+ageTo+currentUser);
        Connection dbConnection = null;
        PreparedStatement preparedStatement = null;


        int iterator=0;
        List<Users> usersList =null;
        String selectSQL="select users.username, users.checkusr, users.password, users.name, users.enabled, users.surname, users.email, users.gender, users.age, users.weight, users.height, users.sport, users.place, users.photo from users where users.enabled = true";
        System.out.println(selectSQL);
        try {
            dbConnection.prepareStatement(selectSQL);
        } catch (SQLException e) {
            System.out.println("first catch block");
            e.printStackTrace();
        }
        if (weight<40 == false) {

            String weightParam = " AND users.weight <= ?";
            selectSQL=selectSQL.concat(weightParam);
            System.out.println(selectSQL);
            try {
                preparedStatement.setInt(iterator++, weight);
            } catch (SQLException e) {
                System.out.println("second catch block");
                e.printStackTrace();
            }
            System.out.println(selectSQL);
        }
        if (gender.isEmpty() == false) {
            String genderParam = " AND  users.gender LIKE '?'";
            selectSQL=selectSQL.concat(genderParam);
            try {
                preparedStatement.setString(iterator++, gender);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if (place.isEmpty() == false) {
            String placeParam = " AND users.place LIKE '?'";
            selectSQL=selectSQL.concat(placeParam);
            try {
                preparedStatement.setString(iterator++, place);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (ageTo<40 == false) {
            String age = " AND users.age <= ?";
            selectSQL=selectSQL.concat(age);
            try {
                preparedStatement.setInt(iterator++, ageTo);
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        String withoutUser=" AND users.username NOT LIKE '?'";
        selectSQL=selectSQL.concat(withoutUser);
        try {
            preparedStatement.setString(iterator++, currentUser);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        System.out.println("FINAL QUERY IS: " + selectSQL);
        ResultSet rs=null;
        try {
            rs = preparedStatement.executeQuery();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            while (rs.next()) {

                String username = rs.getString("username");

                System.out.println("username : " + username);

            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return usersList;
    }

复制范围代码:

Sub dwnld()
  Set IE = New SHDocVw.InternetExplorer
  IE.Visible = True
  IE.navigate "http://broomfield.flatironslibrary.org/"

  While IE.Busy
    DoEvents
  Wend
  Do Until Not IE.Busy And IE.readyState = 4
    DoEvents
  Loop

 IE.document.getElementsByClassName("hidden-xs header-button header-primary")(0).Click
 Application.Wait (Now + #12:00:02 AM#)
 IE.document.getElementById("username").Value = "user"
 IE.document.getElementById("password").Value = "pass"
 IE.document.getElementsByClassName("btn btn-primary extraModalButton")(0).Click
 Application.Wait (Now + #12:00:02 AM#)
 IE.navigate ("http://broomfield.flatironslibrary.org/MyAccount/CheckedOut?exportToExcel")
 Application.Wait (Now + #12:00:02 AM#)
 Application.SendKeys ("%o")
 Application.Wait (Now + #12:00:05 AM#)
 Call CopyingRange

End Sub

1 个答案:

答案 0 :(得分:0)

请尝试使用以下代码进行CopyingRange

Sub CopyingRange()
      Workbooks("CheckedOutItems").Sheets("Checked Out").Range("A3:E62").Copy ThisWorkBook.ActiveSheet.Range("B2")
End Sub
祝你好运!