如果我使用这种代码:
Sub CreateSheet()
Dim ws As Worksheet
With ThisWorkbook
Set ws = .Sheets.Add(After:=.Sheets(.Sheets.Count))
ws.Name = "Period"
End With End Sub
它创建一个名为" Period"的工作表。我想创建一个宏,它创建名为"工作表的时间段1"我第一次跑的时候。第二次它会创建"期间2"等等。所以只有一张/次。
我该怎么做?感谢您的帮助。
答案 0 :(得分:1)
试试这个
Sub Create()
Const LIMIT = 9
Dim ws As Worksheet
Dim i As Long
With ThisWorkbook
For i = 1 To LIMIT
Set ws = .Sheets.Add(After:=.Sheets(.Sheets.Count))
ws.Name = "Period " & CStr(i)
Next i
End With
End Sub
答案 1 :(得分:1)
根据附加信息,第一次拍摄可能是
Option Explicit
Sub Create()
Dim ws As Worksheet
Dim i As Long
i = GetNr(ThisWorkbook, "Period*")
With ThisWorkbook
Set ws = .Sheets.Add(After:=.Sheets(.Sheets.Count))
ws.Name = "Period " & CStr(i + 1)
End With
End Sub
Function GetNr(wb As Workbook, shtPattern As String) As Long
Dim maxNr As Long
Dim tempNr As Long
Dim ws As Worksheet
For Each ws In wb.Worksheets
If ws.Name Like shtPattern Then
tempNr = onlyDigits(ws.Name)
If tempNr > maxNr Then
maxNr = tempNr
End If
End If
Next ws
GetNr = maxNr
End Function
Function onlyDigits(s As String) As String
' Variables needed (remember to use "option explicit"). '
Dim retval As String ' This is the return string. '
Dim i As Integer ' Counter for character position. '
' Initialise return string to empty '
retval = ""
' For every character in input string, copy digits to '
' return string. '
For i = Len(s) To 1 Step -1
If Mid(s, i, 1) >= "0" And Mid(s, i, 1) <= "9" Then
retval = Mid(s, i, 1) + retval
Else
Exit For
End If
Next
' Then return the return string. '
onlyDigits = retval
End Function
答案 2 :(得分:0)
这将完全按照你的要求行事。将创建工作表周期,如果它已经存在,它将循环,直到找到下一个可用编号并创建下一个工作表。作为一个例子,我已经添加了它将从运行宏时激活的Sheet复制Range A2:H20并将其粘贴到新创建的工作表上。
driver = MobileDriverUtil.getAndroidDriverForDeviceFarm();
if (!driver.getCapabilities().getCapability("platformVersion").toString().startsWith("4")) {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, AutomationName.ANDROID_UIAUTOMATOR2);
driver.getCapabilities().merge(capabilities);
} else {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, AutomationName.APPIUM);
driver.getCapabilities().merge(capabilities);
}
SheetExists从此处获取的函数:Excel VBA If WorkSheet("wsName") Exists