多范围选择的VBA Sort Union

时间:2017-01-19 05:05:45

标签: excel vba excel-vba sorting

在多范围选择上运行排序宏时,我得到运行时错误“1004”。我是VBA的新手并使用Application.Union没有帮助。

public static Credential authorize() throws IOException {
        // Load client secrets.
        InputStream in = Quickstart.class
            .getResourceAsStream("/client_secret.json");
        GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(
            JSON_FACTORY, new InputStreamReader( in ));

        // Build flow and trigger user authorization request.
        GoogleAuthorizationCodeFlow flow =new GoogleAuthorizationCodeFlow.Builder(
                HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
            .setDataStoreFactory(DATA_STORE_FACTORY)
            .setAccessType("offline").setApprovalPrompt("auto").build();
        Credential credential = new AuthorizationCodeInstalledApp(flow,
            new LocalServerReceiver.Builder()
                       .setHost("localhost").build()).authorize("user");

        GoogleCredential gCredential = new GoogleCredential.Builder()
            .setTransport(HTTP_TRANSPORT)
            .setJsonFactory(JSON_FACTORY)
            .setServiceAccountId(serviceaccountemail)
            .setServiceAccountPrivateKeyFromP12File(new File("privatekeyfile.p12"))
            .setServiceAccountScopes(SCOPES)
            .setServiceAccountUser(useremail)
            .setClientSecrets(clientSecrets)
            .build().setAccessToken(credential.getAccessToken())
            .setRefreshToken(credential.getRefreshToken())
            .setExpirationTimeMilliseconds(
                     credential.getExpirationTimeMilliseconds());

        System.out.println("Credentials saved to " +
            DATA_STORE_DIR.getAbsolutePath());
        return credential;
    }

我需要在多个打印页面上对与数字(B列)相关联的名称(C列)进行排序。我已经看到其他人通过在一个连续页面上列出名称和数字来解决单一范围排序问题。

编辑:

隐藏行,排序和取消隐藏行适用于尝试对Application.Union进行排序的位置。

Sub Sort()

    Dim R1 As Range
    Dim R2 As Range
    Dim RR As Range
    Set R1 = Range("B12:C31") 'Page One
    Set R2 = Range("B47:C66") 'Page Two, etc.
    Set RR = Union2(R1, R2)
    RR.Sort Key1:=Range("C1"), Order1:=xlAscending, Header:=xlGuess, _
    OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
    DataOption1:=xlSortNormal

End Sub

我希望有一种方法可以对多个范围进行排序,因此不需要宏。

0 个答案:

没有答案