首页 > 解决方案 > 用循环功能替换输入框

问题描述

所以我想创建一个循环函数 -
如果列 O 中的响应为“是”,则输入列 C 中的值作为复制工作表的“新名称”循环,直到列 O 为空白

我知道我必须删除输入框(“输入复制的工作表的名称”)
但是我不确定在 newname 之后放置什么作为循环函数

Sub Button112_Click()

Dim newName As String

On Error Resume Next
newName = InputBox("Enter the name for the copied worksheet")
If newName <> "" Then


    ThisWorkbook.Sheets("Template").Copy After:=Worksheets(Sheets.Count)
    On Error Resume Next
    ActiveSheet.Name = newName
    Range("$D$3").Value = newName
End If

Dim n As Name
For Each n In ActiveWorkbook.Names
n.Visible = True
Next n

Dim numrow
numrow = Range("F16").Value

If IsNumeric(numrow) Then

For i = 1 To numrow

Call INRW

Next i
End If

End Sub

我尝试过类似的方法,但没有成功

   lastRow = .Cells(Rows.Count, 15).End(xlUp).Row
        For i = 1 To lastRow
        If .Cells(i, 15).Value2 = "Yes" Then CopySheetAndRename (.Cells(i, 3).Value2)
        Next i
    End With

    End Sub

标签: excelvba

解决方案


我猜你喜欢这样的事情:

Sub Button112_Click()

    Dim lastRow As Long, i As Long

    With Worksheets("mySheetName")'change mySheetName to suit your needs

        lastRow = .Cells(Rows.Count, 15).End(xlUp).Row
        For i = 1 To lastRow
            If .Cells(i, 15).Value2 = "Yes" Then
                ThisWorkbook.Sheets("Template").Copy After:=Worksheets(Sheets.Count)
                ActiveSheet.Name = .Cells(i, 3).Value2
                Range("$D$3").Value = .Cells(i, 3).Value2
            End If
        Next

    End With

    .... 
    rest of your code

推荐阅读