Excel VBA更改复制的活动工作表的名称

时间:2015-12-10 19:21:15

标签: excel vba excel-vba excel-2013

以下是我的代码。我想将此应用于任何excel文件并创建所选表单的副本,并将其副本重命名为Updated。

    ActiveWorkbook.ActiveSheet.Select
    ActiveWorkbook.Activesheet.copy After:=Sheets(1)
    *Sheets("page (2)").Select
    Sheets("page (2)").Name = "Updated"*

如何选择新复制的工作表。 *周围的部分是问题,我只是不知道要改变什么。

1 个答案:

答案 0 :(得分:2)

您应该检查更新名称是否已存在。

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import javax.xml.bind.DatatypeConverter;

public class SendEmailWithTLSConnectionTest {

private static DataOutputStream dos;
private static BufferedReader out = null;

public static void main(String[] args) throws Exception
{   
    try
    {
       String username = DatatypeConverter.printBase64Binary("leo@tls.calcium.co.nz".getBytes());
       String password = DatatypeConverter.printBase64Binary("2wsxZAQ!".getBytes());

       Socket sock = new Socket("aspmx.l.google.com", 587);

       out = new BufferedReader(new InputStreamReader(sock.getInputStream()));    
       dos = new DataOutputStream(sock.getOutputStream());

       if (sendCmd("EHLO aspmx.l.google.com") == 250)
       {
           // TODO: parse response
           if (true/*response contains STARTTLS capability*/)
           {
               sendCmd("STARTTLS", 220);

               SSLSocket sslSocket = (SSLSocket) ((SSLSocketFactory) SSLSocketFactory.getDefault()).createSocket(
                 sock, 
                 sock.getInetAddress().getHostAddress(), 
                 sock.getPort(), 
                 true);                 

               sslSocket.setUseClientMode(true);
               sslSocket.setEnableSessionCreation(true);        

               System.out.println("CLIENT: securing connection");
               sslSocket.startHandshake();
               // on an initial handshake, startHandshake() blocks the calling
               // thread until the handshake is finished...
               System.out.println("CLIENT: secured");

               sock = sslSocket;
               out = new BufferedReader(new InputStreamReader(sock.getInputStream()));    
               dos = new DataOutputStream(sock.getOutputStream());

               sendCmd("EHLO aspmx.l.google.com", 250);
           }
       }
       else
           sendCmd("HELO aspmx.l.google.com", 250);

       sendCmd("AUTH LOGIN", 334);
       if (sendCmd(username, new int[]{235, 334}) == 334)
           sendCmd(password, 235);

       sendCmd("MAIL FROM: <leo@tls.calcium.co.nz>", 250);
       sendCmd("RCPT TO: <leo@tls.calcium.co.nz>", new int[]{250, 251});
       sendCmd("DATA", 354);

       sendLine("From: <leo@tls.calcium.co.nz>");
       sendLine("To: <leo@tls.calcium.co.nz>");
       sendLine("Subject: test");
       sendLine("");
       sendLine("Test 1 2 3");
       sendCmd(".", 250);

       sendCmd("QUIT", 221);
     }
     catch(Exception ex)
     {
        System.out.println("Exception when sending out test. Error: " + ex.getMessage());
     }
  }

  private static void sendLine(String s) throws Exception
  {
      dos.writeBytes(s + "\r\n");
      System.out.println("CLIENT: " + s);
  }

  private static int sendCmd(String s) throws Exception
  {
      sendLine(s);

      String line = out.readLine();
      System.out.println("SERVER: " + line);                                    

      int respCode = Integer.parseInt(line.substring(0, 3));
      while ((line.length() > 3) && (line.charAt(3) == '-'))
      {
          line = out.readLine();
          System.out.println("SERVER: " + line);                                    
      }

      return respCode;
  }

  private static int sendCmd(String s, int expectedRespCode) throws Exception
  {
      int respCode = sendCmd(s);
      checkResponse(respCode, expectedRespCode);
      return respCode;
  }

  private static int sendCmd(String s, int[] expectedRespCodes) throws Exception
  {
      int respCode = sendCmd(s);
      checkResponse(respCode, expectedRespCodes);
      return respCode;
  }

  private static void checkResponse(int actualRespCode, int expectedRespCode)
  {
      if (actualRespCode != expectedRespCode)
          throw new Exception("command failed");
  }

  private static void checkResponse(int actualRespCode, int[] expectedRespCodes)
  {
      for (int i = 0; i < expectedRespCodes.length; ++i)
      {
          if (actualRespCode == expectedRespCode)
              return;
      }
      throw new Exception("command failed");
  }
}