简体   繁体   中英

VBA Tab creation

I need to create multiple worksheets in a workbook as per values in coloumn A. The names of the worksheets should be as per the data in coumn A.

I have created a vba code for the same but I am not able to loop it after first cell to seond cell and so on. Below is the my code.

Sub inputboxandsheetname()

    Dim myValue As Variant

    Dim irow As Long

    irow = 1

    Do While Cells(irow, 1) <> Empty

        myValue = Cells(irow, 1)

        If myValue <> "" Then

            Sheets.Add After:=Sheets(ActiveWorkbook.Worksheets.Count)

            ActiveSheet.Name = myValue

        End If

        irow = irow + 1

    Loop

End Sub

Cells(irow, 1) <> Empty
If you don't qualify the range with a sheet object, it will always refer to the ActiveSheet . When you add a new sheet, this becomes the active sheet which will contain blank cells.

Also, Empty is used in VBA to check if a variant variable has a value assigned to it, not to check if a range contains a value.

Try the following instead:

Sub SO()

Dim indexSheet As Excel.Worksheet
Dim i As Integer
Set indexSheet = ActiveSheet
i = 1

While indexSheet.Cells(i, 1).Value <> vbNullString
    Sheets.Add After:=Sheets(Sheets.Count)
    ActiveSheet.Name = indexSheet.Cells(i, 1).Value
    i = i + 1
Wend

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM