读取单元格并创建工作表

时间:2015-11-27 14:12:47

标签: excel vba excel-vba

如何在B列中读取Visual Basic,在Excel单元格中将单元格“控制”为单元格直到空单元格?

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

picture

您会看到此列的内容,这些内容可能会不时发生变化。阅读之后,我想生成一个名为RW_BONDS,...的空表。

4 个答案:

答案 0 :(得分:2)

你可以这样做。

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

答案 1 :(得分:1)

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

答案 2 :(得分:1)

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

答案 3 :(得分:1)

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