读取单元格并创build工作表

如何在Visual Basic中从B列表单元格的Excel单元格中的“控件”直到空单元格读取?

之后,我想为每个单元格生成一个名为单元格的新工作表。 在这里:

图片

您会看到本专栏的内容,这些内容可能会不时地出现。 读完后,我想生成名称为空的表单:RW_BONDS,…。

Sub test() Dim i As Long i = 1 While Len(Sheets("Control").Cells(i, 2)) Worksheets.Add.Name = Sheets("Control").Cells(i, 2): i = i + 1 Wend End Sub 

编辑答案的评论:

 Sub test() Dim i As Long i = 1 With Sheets("Control") On Error Resume Next Application.DisplayAlerts = False While Len(.Cells(i, 2)) If Len(Sheets(.Cells(i, 2).Value).Name) = 0 Then Else Sheets(.Cells(i, 2).Value).Delete Worksheets.Add.Name = .Cells(i, 2): i = i + 1 Wend Application.DisplayAlerts = True On Error GoTo 0 End With End Sub 

你可以做这样的事情。

 Private Sub CommandButton1_Click() Dim ws As Excel.Worksheet Dim lRow As Long Dim lastRow As Long 'Set the sheet to read from Set ws = Application.Sheets("control") 'Set the row to start reading at lRow = 3 lastRow = wsOwners.Cells(wsOwners.Rows.Count, "B").End(xlUp).Row 'Loop through the rows Do While lRow <= lastRow If ws.Range("B" & lRow).Value <> "" then 'Add a new sheet ActiveWorkbook.Sheets.Add Before:=Worksheets(Worksheets.Count) 'Change the name to the value of column B in the current row ActiveWorkbook.ActiveSheet.Name = ws.Range("B" & lRow).Value End If 'Increment your row to the next one lRow = lRow + 1 Loop End Sub 
 set ws = worksheets("Source") row = 1 col = "B" Do row = row + 1 if ws.range(col & row).text = "" then exit do worksheets.add.name = ws.range(col & row).text Loop End Sub 
 Sub createSheets() With Worksheets("control") iRow = 1 'Start on the first row While Len(.Cells(iRow, 2)) > 0 'While there isn't a blank cell Worksheets.Add.Name = .Cells(iRow,2) 'Create/rename sheet iRow = iRow + 1 Wend End With End Sub