根据EXcel中另一个工作表上的输入填充行

时间:2017-05-24 07:14:26

标签: excel

我经历过各种各样的例子,但不符合我的需要。 我需要根据多个单元格填充表单。 例如,在Sheet 1 C5单元格中,我的值为2,在c6中我是5,在c7中我是8。 然后在表2中,我将在列a中创建行“e001,e002”,然后在单元格a3“E101,...... E105”中创建行,然后在“E401 ...... E408”中创建行 如果不使用vb​​a会更好,因为我还没有和它交谈过。

Sample sheet enter image description here

Sample sheet 2 enter image description here

以下是我编写的但不符合我期望的代码。

Public Sub InsertTagNumbers()  
    Dim ehv_breaker As Long  
    Dim row_no As Long  
    Dim breaker11kv As Long  
    Dim breaker33kv As Long  
    Dim breaker415V As Long  
    Dim breaker415E As Long  
    Dim carry11 As Long  
    Dim carry33 As Long  
    Dim carry415 As Long  
    Dim counter1 As Integer  
    Dim counter2 As Integer  

    ehv_breaker = 0
    row_no = 5
    Sheets("Sheet2").Range("b5").Select

    For ehv_breaker = 1 To Sheets("Sheet1").Range("c6").Value
        Sheets("sheet2").Cells(row_no, 2).Value = Sheets("sheet1").Cells(6, 4) & 
        Format$(ehv_breaker, "00")
        ActiveCell.Offset(1, 0).Select
        row_no = row_no + 1
    Next ehv_breaker

    For breaker11kv = 1 To Sheets("Sheet1").Range("c7").Value
        Sheets("Sheet2").Cells(row_no, 2).Value = Sheets("sheet1").Cells(7, 4) & 
        Format$(breaker11kv, "00")
        ActiveCell.Offset(1, 0).Select
        row_no = row_no + 1
        Next breaker11kv
    carry11 = breaker11kv

    For breaker33kv = 1 To Sheets("Sheet1").Range("c8").Value
        Sheets("sheet2").Cells(row_no, 2).Value = Sheets("sheet1").Cells(8, 4) & 
        Format$(breaker33kv, "00")
        ActiveCell.Offset(1, 0).Select
        row_no = row_no + 1
        Next breaker33kv
    carry33 = breaker33kv

    For breaker415V = 1 To Sheets("Sheet1").Range("c9").Value
        Sheets("sheet2").Cells(row_no, 2).Value = Sheets("sheet1").Cells(9, 4) & 
        Format$(breaker415V, "00")
        ActiveCell.Offset(1, 0).Select
        row_no = row_no + 1
        Next breaker415V
    carry415 = breaker415V

    For breaker415E = 1 To Sheets("Sheet1").Range("c10").Value
        Sheets("sheet2").Cells(row_no, 2).Value = Sheets("sheet1").Cells(10, 4) & 
        Format$(breaker415E, "00")
        ActiveCell.Offset(1, 0).Select
        row_no = row_no + 1
    Next breaker415E

    'For breaker11kv > breaker11kv To Sheets("Sheet1").Range("c11").Value + carry11
    Do
        Sheets("sheet2").Cells(row_no, 2).Value = Sheets("sheet1").Cells(11, 4) & 
        Format$(breaker11kv, "00")
        ActiveCell.Offset(1, 0).Select
        row_no = row_no + 1
        breaker11kv = breaker11kv + 1
    Loop Until breaker11kv > Sheets("Sheet1").Cells("c11").Value + carry11
    'Loop Until breaker11kv > Sheets("Sheet1").cells(c11").Value + carry11
    'Next breaker11kv

    For breaker33kv = breaker33kv To Sheets("Sheet1").Range("c12").Value + carry33
        Sheets("sheet2").Cells(row_no, 2).Value = Sheets("sheet1").Cells(12, 4) & 
        Format$(breaker33kv, "00")
        ActiveCell.Offset(1, 0).Select
        row_no = row_no + 1
    Next breaker33kv

    For breaker415V = breaker415V To Sheets("Sheet1").Range("c13").Value + carry415
        Sheets("sheet2").Cells(row_no, 2).Value = Sheets("sheet1").Cells(13, 4) & 
        Format$(breaker415V, "00")
        ActiveCell.Offset(1, 0).Select
        row_no = row_no + 1
    Next breaker415V

    MsgBox "ehv_breaker=" & ehv_breaker
    Application.ScreenUpdating = False
End Sub

1 个答案:

答案 0 :(得分:0)

你的第一个循环看起来很好,然后你有Do .. Loop Until,这有点不寻常。

之后的循环似乎是一团糟:例如:

For breaker33kv = breaker33kv To Sheets("Sheet1").Range("c12").Value + carry33

如何启动未初始化的变量?它应该从1开始吗?

For breaker33kv = 1 To Sheets("Sheet1").Range("c12").Value

我也不太确定carry变量的用途是什么?如果计划将同步和非同步断路器组合在一起,那么您的代码应该在Sheet1!7:7之后直接处理Sheet1!11:11,然后再继续使用Sheet1!8:8