-2

I want to print a rows of information onto another sheet. the problem is that the number of rows will always be dynamic never static.

For example:

*A1 = 4
*B1 = Thomas 
*C1 = Apples

*A2 = 2
*B2 = Jerry
*C2 = Oranges

*A3 = 1
*B3 = Tiffany
*C3 = Strawberries

What i want is to print these rows along with some string...

so on Sheet 2 startign on A1

I want it to read A1 "A quantity of " & A1 & " must be conumed by " & B1 & " product will be " & C1 & chr(10) "A quantity of 4 must be consumed by Thomas product will be Apples" (a new line after each row)

I don't know how to create a look which will do this so I dont' have to worry about number of rows.

4

1 回答 1

0

不完全确定打印每一行的意思。

您可以遍历列“A”中的所有值并连接下一列“D”中的值,如下所示:

Sub Macro1()
    With ActiveSheet
        LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
    End With

    Dim rRng As Range
    Set rRng = Range("A1:A" & LastRow)

    For Each cell In rRng.Cells
        cell.Offset(0, 3).Value = "A quantity of " & cell.Value & " must be consumed by " & cell.Offset(0, 1).Value & " product will be " & cell.Offset(0, 2).Value
    Next
End Sub

结果如下所示:

在此处输入图像描述


编辑

由于您提到了一个换行符,我假设您出于某种原因想要一个单元格中的所有文本。尝试这个:

Sub Macro1()
    With ActiveSheet
        LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
    End With

    Dim rRng As Range
    Set rRng = Range("A1:A" & LastRow)

    Dim result As Range
    Set result = Range("D1")

    result.Value = ""

    For Each cell In rRng.Cells
        result.Value = Range("D1").Value & "A quantity of " & cell.Value & " must be consumed by " & cell.Offset(0, 1).Value & " product will be " & cell.Offset(0, 2).Value & Chr(10)
    Next
End Sub

结果现在看起来像这样:

在此处输入图像描述

于 2013-10-23T19:27:44.767 回答