要按顺序复制和粘贴的宏

时间:2016-08-29 17:23:50

标签: macros copy paste

我认为这可能有助于回答我的问题,我只是不够了解它。 - 抱歉,如果我的问题多余。 Macro to copy and paste in next blank cell

我有一个动态过滤器,允许我在数据表中查找数据并在单元格A1,B1和C1中显示数据 然后我有一个宏,它将复制单元格A1,B1和C1中的数据,并在单击分配给宏的按钮时将它们粘贴到D1,E1和F1。

Range("A1:C1").Select
Selection.Copy
Range("D1").Select
ActiveSheet.Paste

当我更改过滤器并运行宏/第二次单击按钮时,我无法找到或弄清楚如何修改宏以将数据粘贴到D2,E2和F2。 基本上,我试图通过单击基于我的动态过滤器找到的按钮来创建数据列表。

我希望这是有道理的,有人可以帮助我。

谢谢!

1 个答案:

答案 0 :(得分:0)

使用ElseIf找到解决方案。我相信它可以做得更干净,但它做了我需要它做的事情。我现在可以将宏分配给一个按钮并多次单击它以形成我的列表。

    package com.something.online.ice.ui.authentication;

    import java.util.Hashtable;
    import java.util.Properties;

    import javax.annotation.Resource;
    import javax.naming.Context;
    import javax.naming.NamingEnumeration;
    import javax.naming.NamingException;
    import javax.naming.directory.Attributes;
    import javax.naming.directory.DirContext;
    import javax.naming.directory.InitialDirContext;
    import javax.naming.directory.SearchControls;
    import javax.naming.directory.SearchResult;
    import javax.naming.ldap.InitialLdapContext;
    import javax.naming.ldap.LdapContext;


    /**
     * 

     * This is a solution that can be used to authenticate a user with something else than the DN, for example with a uid or sAMAccountName.

        The steps to do are:

        -Connect to the LDAP server
        -Authenticate with a service user of whom we know the DN and credentials
        -Search for the user you want to authenticate, search him with some attribute (for example sAMAccountName)
        -Get the DN of the user we found
        -Open another connection to the LDAP server with the found DN and the password
        -If the user is found and authentication works, you are fine

     *
     */

    public class LdapAuthManagerJNDI 
    {
        public static void main(String[] args)
        {
            LdapAuthManagerJNDI mgr = new LdapAuthManagerJNDI();
            System.out.println(mgr.authenticateUsr("svc_oapusr", "pswd"));

        }

        public boolean authenticateUsr(String usrName, String pswd)
        {


            Hashtable<String, String> serviceEnv = new Hashtable<String, String>();
            boolean authenticationresullt = false;


            serviceEnv.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
            serviceEnv.put(Context.PROVIDER_URL, "ldap://pcocpwdom01.corp.something.com:389");

            serviceEnv.put(Context.SECURITY_AUTHENTICATION, "simple");
            serviceEnv.put(Context.SECURITY_PRINCIPAL, "CN=SVCLdapQry,OU=ServiceAccounts_Admins,OU=Data Services,DC=corp,DC=something,DC=com"); 
            serviceEnv.put(Context.SECURITY_CREDENTIALS, "ADR0cks!~");

            // Create the initial context

            DirContext serviceCtx;
            try 
            {
                serviceCtx = new InitialDirContext(serviceEnv);
            } 
            catch (NamingException e) 
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return false;
            }

            boolean serviceConnectionResult = serviceCtx != null;

            if(serviceConnectionResult)
            {
                System.out.println("LDAP basic authorization is successful");
            }

            // user to authenticate
            String identifyingAttribute = "samaccountname";
            String ldapUrl = "ldap://pcocpwdom01.corp.something.com:389";
            String base = "DC=corp,DC=something,DC=com";

            // we don't need all attributes, just let it get the identifying one
            String[] attributeFilter = { identifyingAttribute };
            SearchControls sc = new SearchControls();
            sc.setReturningAttributes(attributeFilter);
            sc.setSearchScope(SearchControls.SUBTREE_SCOPE);

         // use a search filter to find only the user we want to authenticate
            String searchFilter = "(" + identifyingAttribute + "=" + usrName + ")";

            NamingEnumeration<SearchResult> results = null;

            try 
            {
                results = serviceCtx.search(base, searchFilter, sc);
            } 
            catch (NamingException e1) 
            {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

            DirContext usrCtx = null;
            try {
                if (results.hasMore()) {
                    // get the users DN (distinguishedName) from the result
                    SearchResult result = results.next();
                    String distinguishedName = result.getNameInNamespace();

                    // attempt another authentication, now with the user
                    Properties authEnv = new Properties();
                    authEnv.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
                    authEnv.put(Context.PROVIDER_URL, ldapUrl);
                    authEnv.put(Context.SECURITY_PRINCIPAL, distinguishedName);
                    authEnv.put(Context.SECURITY_CREDENTIALS, pswd);
                    usrCtx = new InitialDirContext(authEnv);

                    System.out.println("Authentication successful");
                    authenticationresullt =  true;
                }
            } catch (NamingException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }


          //close the service context
            if(usrCtx != null)
                try 
                {
                    usrCtx.close();
                } 
                catch (NamingException e) 
                {
                    e.printStackTrace();
                    return false;
                }


            //close the service context
            if(serviceCtx != null)
                try 
                {
                    serviceCtx.close();
                } 
                catch (NamingException e) 
                {
                    e.printStackTrace();
                    return false;
                }

            return authenticationresullt;


        }




    }