Excel Visual Basic复制表

时间:2017-06-20 09:15:03

标签: excel vba excel-vba

我的目标是创建一段代码,为代码中的每个字符串创建模板工作表的副本 - 重命名复制的工作表以匹配字符串。

这是我第一次使用Microsoft Visual Basic for Applications。我在" ThisWorkbook"下添加了我的代码。但是,当我运行代码时,我得到了一个

  

下标超出范围

错误。

    Sub generateStationTabs()
        Dim stringNames() As Variant
        Dim currentString As String
        Dim previousSheet As String

        previousSheet = "Template                 "

        stringNames() = Array("String 1", "String 2", "String 3", "String 4", "String 5", "String 6", "String 7", "String 8", "String 9", "String 10", "String 11", "String 12", "String 13", "String 14", "String 15", "String 16", "String 17", "String 18", "String 19", "String 20", "String 21", "String 22", "String 23", "String 24", "String 25", "String 26", "String 27", "String 28", "String 29", "String 30")
        For indexVariable = 0 To UBound(stringNames)
            ' Pad String Name to 20 Characters Length       currentString = Left(stringNames(indexVariable) & Space(8), 20)
            ' Copy the Template worksheet to after the Previous Sheet
            ActiveWorkbook.Sheets("Template                 ").Copy after:=ActiveWorkbook.Sheets(previousSheet)         
            ' Rename the copied worksheet
            ActiveWorkbook.ActiveSheet.Name = currentString         
            ' Update the previous sheet
            previousSheet = currentString
        Next

    End Sub

2 个答案:

答案 0 :(得分:1)

您尚未定义ActiveWorkbook.Sheets("Template ").Copy after:=ActiveWorkbook.Sheets(ActiveWorkbook.Sheets.Count),因此无法找到它。基本上,您要求Excel查找尚未创建的工作表,因此无法找到。

相反,你可以试试......

private void dataGridView1_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Up || e.KeyCode == Keys.Down) { var index = e.KeyCode == Keys.Up ? -1 : e.KeyCode == Keys.Down ? 1 : 0; var rowIndex = dataGridView1.CurrentCell.RowIndex + index; if (rowIndex > -1) { bool pingable = false; Ping pinger = new Ping(); var row = dataGridView1.Rows[rowIndex]; if (row != null) { PingReply reply = pinger.Send(row.Cells[0].Value.ToString()); txtIP.Text = row.Cells[0].Value.ToString(); txtUser.Text = row.Cells[1].Value.ToString(); txtComputer.Text = row.Cells[2].Value.ToString(); txtUnity.Text = row.Cells[3].Value.ToString(); txtSession.Text = row.Cells[4].Value.ToString(); if (pingable = reply.Status == IPStatus.Success) { pictureBoxGreen.Show(); pictureBoxRed.Hide(); } else if (pingable = reply.Status == IPStatus.TimedOut) { pcGreen.Hide(); pcRed.Show(); } } } } }

...然后根据需要重命名工作表。

答案 1 :(得分:1)

代码就是这样。

Sub generateStationTabs()
    Dim stringNames() As Variant
    Dim currentString As String
    Dim previousSheet As String

    previousSheet = "Template"

    stringNames() = Array("String 1", "String 2", "String 3", "String 4", "String 5", "String 6", "String 7", "String 8", "String 9", "String 10", "String 11", "String 12", "String 13", "String 14", "String 15", "String 16", "String 17", "String 18", "String 19", "String 20", "String 21", "String 22", "String 23", "String 24", "String 25", "String 26", "String 27", "String 28", "String 29", "String 30")
    For i = 0 To UBound(stringNames)
        ' Pad String Name to 20 Characters Length       currentString = Left(stringNames(indexVariable) & Space(8), 20)
        ' Copy the Template worksheet to after the Previous Sheet
        ActiveWorkbook.Sheets("Template").Copy after:=ActiveWorkbook.Sheets(Sheets.Count)
        ' Rename the copied worksheet
        ActiveWorkbook.ActiveSheet.Name = stringNames(i)
        ' Update the previous sheet
        'previousSheet = currentString
    Next

End Sub