从一行写入多行

时间:2015-09-11 11:20:11

标签: excel vba excel-vba

Excel + VBA。

我有一个包含4列ID,无线轴,包装/交货长度的文件。 我正在做的是将基于No Spools的行数写入另一个工作表(您可以在“CreateOrders”表中查看数据以更好地理解)。 这工作正常,但我有一个问题。我之前在“截止日期”表单中:“CreateOrders”放置了“今天日期”,但现在想要根据前一页“PROGRAMA COBRE”中的ID设置特定日期,例如:

来自:

From

致:

To

我的档案:http://speedy.sh/s4peR/Livro3.xlsm

我手动完成,“手动”设置所有日期,但我该如何自动执行此操作?

感谢您的帮助。

编辑(我的代码):

Sub CreateOrdersFile()
    Dim c As Range, a As Long, No_Order As String, sh2 As Worksheet

    Set sh2 = Sheets("CreateOrders")
    No_Order = Application.InputBox(Prompt:="Please enter the first Order Number (Today Date + 001)", Title:="Order Number required!")

    With sh2
        .UsedRange.ClearContents
        .Range("A1:J1").Value = Array("No Order", "Due Date", "ID", "No Spools", "Packing Unit", "Quantity Unit", "Type", "Remark", "Storage Location", "Item No")
        .Range("A2").Value = No_Order
    End With

    For Each c In Range("B2:B" & Cells(Rows.Count, 1).End(xlUp).Row)
        a = c.Value

        With sh2.Cells(Rows.Count, 3).End(xlUp).Offset(1).Resize(a, 3)
            .Value = Array(c.Offset(, -1).Value, 1, c.Offset(, 1).Value)
        End With

    Next c
        With sh2
            .Range("A2").AutoFill Destination:=.Range("A2:A" & .Cells(Rows.Count, 3).End(xlUp).Row), Type:=xlFillSeries
                With .Range("B2:B" & .Cells(.Rows.Count, 3).End(xlUp).Row)
                    '.Value = Format(Date, "dd/mm/yyyy")
                    .Offset(, 4).Value = "M"
                    .Offset(, 5).Value = "Order"
                    .Offset(, 7).Value = "POGU01"
                    .Offset(, 8).Value = 1
                End With
        End With
    MsgBox "Orders successfully created!", , "Orders Created"
End Sub

1 个答案:

答案 0 :(得分:0)

这是你正在尝试的(未经测试)?将您的For loop更改为此

For Each c In Range("B2:B" & Cells(Rows.Count, 1).End(xlUp).Row)
    a = c.Value

    With sh2.Cells(Rows.Count, 3).End(xlUp).Offset(1).Resize(a, 3)
        .Value = Array(c.Offset(, -1).Value, 1, c.Offset(, 1).Value)
    End With

    '~~> The below "should" fill the Column B with Dates.
    With sh2.Cells(Rows.Count, 2).End(xlUp).Offset(1).Resize(a, 1)
        .Value = c.Offset(, 2).Value
    End With
Next c