如何根据excel vba中的单元格值逐步重命名工作表

时间:2017-07-25 08:29:51

标签: excel vba excel-vba excel-formula

我有一个包含多个值的电子表格,但结构相同。我想根据单元格值" E1"重命名工作表。

但是,E1中有多个具有相同值的工作表。如何重命名工作表abc01(如果单元格E1 = abc)和相应的工作表单元格E1 = abc as abc02等等。

我试过了

public class FileReceiver {

    private final boolean mDebug = true;

//    private final int MIN_PORT_NUMBER = 1024;
  //  private final int MAX_PORT_NUMBER = 65536;
    private final String PORT = "PORT";
    private final String MESSENGER = "MESSENGER";

    // Constants start from 2001
    public static final int CODE = 2001;
    public static final int LISTENING = 2002;
    public static final int CONNECTED = 2003;
    public static final int RECEIVING_FILE = 2004;
    public static final int FILE_RECEIVED = 2005;
    public static final int RECEIVE_ERROR = 2006;

    private Context context;
    private Handler mHandler;

    private Intent i;

    public FileReceiver(Context context, Handler mHandler) {
        this.context = context;
        this.mHandler = mHandler;
    }

/*    private boolean isPortAvailable(int port) {

        boolean available;

        if (port < MIN_PORT_NUMBER || port > MAX_PORT_NUMBER) {
            throw new IllegalArgumentException("Invalid port: " + port);
        }

        ServerSocket ss = null;

        try {
            ss = new ServerSocket(port);
            ss.setReuseAddress(true);
            available = true;
        } catch (IOException e) {
            available = false;
        } finally {

            if (ss != null) {
                try {
                    ss.close();
                } catch (IOException e) {

                }
            }
        }

        return available;
    }
*/
    private int getRandomPort() {
 //int port = 1001;
        int port = 40500;

        //do{
          //  port = (int) (MIN_PORT_NUMBER + 1 + Math.floor(Math.random()*(MAX_PORT_NUMBER - MIN_PORT_NUMBER-1)));

            if(mDebug)
                Log.i("FileReceiver","Trying port : " + port);

//        }while(!isPortAvailable(port));

        return port;


 //   return port;
    }

    public void getFile(){

        int port = getRandomPort();

        if(mDebug)
            Log.i("FileReceiver","Port : " + port);

        i = new Intent(context,ReceiverService.class);

        i.putExtra(PORT,port);
        i.putExtra(MESSENGER,new Messenger(mHandler));

        context.startService(i);

    }

    public void close() {
        context.stopService(i);
    }
}

如果我在单元格E1中有多个具有相同值的工作表

,则此代码将失败

感谢任何帮助...

3 个答案:

答案 0 :(得分:1)

试一试......

Sub ReNameSheets()
Dim ws As Worksheet
Dim i As Long, cnt As Long
Dim shIdx As Long
For Each ws In Worksheets
    If InStr(LCase(ws.Name), LCase(ws.Range("E1").Value)) = 0 Then
        For i = 1 To Worksheets.Count
            If InStr(LCase(Sheets(i).Name), LCase(ws.Range("E1").Value)) > 0 Then
                cnt = cnt + 1
                If Right(Sheets(i).Name, 2) > shIdx Then shIdx = Right(Sheets(i).Name, 2)
            End If
        Next i
        If cnt > 0 Then
            cnt = shIdx + 1
            ws.Name = ws.Range("E1").Value & Format(cnt, "00")
        Else
            ws.Name = ws.Range("E1").Value & "01"
        End If

        cnt = 0
        shIdx = 0
    End If
Next ws
End Sub

答案 1 :(得分:0)

创建一个计数器变量并在For Each循环中递增该变量。之后,您可以将数字附加到E1的值。

答案 2 :(得分:0)

如您所见,工作表名称必须是唯一的。如果该名称已存在,您可以将索引添加到名称的末尾。

Sub RenameWorksheet()
    Dim WS As Worksheet
    With ThisWorkbook
        For Each WS In .Worksheets
            WS.Name = WS.Range("E1").Value & IIf(Exists(WS.Range("E1").Value), WS.Index, vbNullString)
        Next WS
    End With
End Sub

Function Exists(ByVal Name As String) As Boolean
    Dim WS As Worksheet
    With ThisWorkbook
        For Each WS In .Worksheets
            If WS.Name = Name Then
                Exists = True
                Exit Function
            End If
        Next WS
    End With
End Function