我正在尝试浏览A列中的列表,如果在B列中旁边有一个对应的是,请在A列中创建一个新标签页。
我知道非常基本的VBA。当某些消息为真时,出现消息框时,我没有正确引用该范围。
Private Sub CommandButton1_Click()
lastcell = ThisWorkbook.Worksheets("MasterSheet").Cells(Rows.Count, 1).End(xlUp).Row
lastcellsecond = ThisWorkbook.Worksheets("MasterSheet").Cells(Rows.Count, 2).End(xlUp).Row
For a = 2 To lastcellsecond
If Cells(a, 2) = "Yes" Then
For i = 2 To lastcell
With ThisWorkbook
newname = ThisWorkbook.Worksheets("MasterSheet").Cells(i, 1).Value
.Sheets.Add after:=Sheets(.Sheets.Count)
ActiveSheet.Name = newname
End With
Next i
Else
MsgBox "RETRY"
End If
Next a
ThisWorkbook.Worksheets("MasterSheet").Activate
ThisWorkbook.Worksheets("MasterSheet").Cells(1, 1).Select
End Sub
答案 0 :(得分:0)
这里有几种迭代范围的方法:
Private Sub CommandButton1_Click()
Dim values As Variant
With ThisWorkbook.Worksheets("MasterSheet")
values = .Range("A2", .Cells(.Rows.Count, 1)).Resize(, 2).Value
End With
Dim r As Long
Dim newname As String
For r = 1 To UBound(values, 2)
If values(r, 2) = "Yes" Then
newname = values(r, 1)
With ThisWorkbook
.Sheets.Add(After:=Sheets(.Sheets.Count)).Name = newname
End With
End If
Next
ThisWorkbook.Worksheets("MasterSheet").Activate
ThisWorkbook.Worksheets("MasterSheet").Cells(1, 1).Select
End Sub
Private Sub CommandButton1_Click()
Dim cell As Range
Dim newname As String
With ThisWorkbook.Worksheets("MasterSheet")
For Each cell In .Range("A2", .Cells(.Rows.Count, 1))
If cell.Offset(0, 1).Value = "Yes" Then
newname = cell.Value
With ThisWorkbook
.Sheets.Add(After:=Sheets(.Sheets.Count)).Name = newname
End With
End If
Next
End With
ThisWorkbook.Worksheets("MasterSheet").Activate
ThisWorkbook.Worksheets("MasterSheet").Cells(1, 1).Select
End Sub
Private Sub CommandButton1_Click()
Dim cell As Range
Dim newname As String
Dim r As Long
With ThisWorkbook.Worksheets("MasterSheet")
For r = 2 To .Range("A2", .Cells(.Rows.Count, 1))
If .Cells(r, 2).Value = "Yes" Then
newname = .Cells(r, 1).Value
With ThisWorkbook
.Sheets.Add(After:=Sheets(.Sheets.Count)).Name = newname
End With
End If
Next
End With
ThisWorkbook.Worksheets("MasterSheet").Activate
ThisWorkbook.Worksheets("MasterSheet").Cells(1, 1).Select
End Sub
我强烈建议您观看: