重命名工作表基于INPUT BOX

时间:2016-11-28 11:15:31

标签: excel vba excel-vba

请帮我改变下面的代码。需要通过func (a ByTimestamp) Less(i, j int) bool { return a[j].Timestamp.Before(a[i].Timestamp) }

选择重命名单元格

由于

InputBox

1 个答案:

答案 0 :(得分:1)

有几种方法可以使用InputBox访问Cell C4。

一种方法是在String中选择InputBox,请参阅以下代码:

Dim RngStr  As String

RngStr = Application.InputBox(prompt:="Select the Cell for the new Sheet's name", Type:=2)
If Trim(.Range(RngStr)) <> "" Then

另一种方法是选择Range中的InputBox,请参阅以下代码:

Dim rng As Range

Set rng = Application.InputBox(prompt:="Select the Cell for the new Sheet's name", Type:=8)
If Trim(rng) <> "" Then

完整代码

Option Explicit

Sub RenWSs()

Dim WS As Worksheet
Dim shtName
Dim newName As String
Dim i As Integer
Dim RngStr  As String

RngStr = Application.InputBox(prompt:="Select the Range for the new Sheet's name", Type:=2)
For Each WS In Worksheets
    With WS        
        If Trim(.Range(RngStr)) <> "" Then
            shtName = Split(Trim(.Range(RngStr)), " ")
            newName = shtName(0)
            On Error GoTo ws_name_error
            .Name = newName
            GoTo done
repeat:
            .Name = newName & i
            GoTo done
ws_name_error:
            i = i + 1
            Resume repeat
        End If
    End With
    On Error GoTo 0
done:

Next

End Sub

要详细了解InputBox功能:https://msdn.microsoft.com/en-us/library/office/ff839468.aspx