-2

Is there a way I can make a list. I am trying to make a comment log which will include Name---Date---Comment. I will have 3 work books. Two workbooks will have two different lists of Name,Date and Comment then I want the third one to collect these lists into one master list based on the date. How would I go about programming the macro for that? I understand the VBA Recorder. My problem is the size of the lists is undetermined. For example, there could be 5 comments in one book, and 3 comments in the second book. I'd like the the master list to have 8 comment logs in order based on date entered.

4

2 に答える 2

1

なんてこった、とにかく退屈だ。最新のコメントを投稿する前にこれを書きましたが、動的範囲がある場合は、ソースに CurrentRegion プロパティを使用した場所であれば、それらの範囲の名前を置き換えることができる場合があります。これに従うことができるかどうかを確認してください。少なくとも正しい方向に向ける必要があります。

Sub CombineLists2()

    Dim rng_Source As Excel.Range
    Dim rng_Target As Excel.Range

    Dim wbk_1 As Excel.Workbook
    Dim wbk_2 As Excel.Workbook
    Dim wbk_3 As Excel.Workbook

    On Error GoTo ErrorHandler

    'I'd recommend doing a test here to ensure that
    'all three workbooks are open, which is one reason
    'that we're using object variables.
    'If one of the workbooks isn't open then it'll throw an error.
    'Obviously change the names to your own files.
    Set wbk_1 = Application.Workbooks("ATeam.xlsx")
    Set wbk_2 = Application.Workbooks("BTeam.xlsx")
    Set wbk_3 = Application.Workbooks("CTeam.xlsm")

    'You may want to delete any contents that are in the combined book first.
    wbk_3.Worksheets(1).Range("A1").CurrentRegion.Clear

    'We'll now get a reference to the range containing the list.
    'You'll obviously have to change the references to suit where
    'your list is.
    'In this case we'll take the headings as well.
    'Note that CurrentRegion range will only work if there
    'is nothing directly adjacent to the list.
    wbk_1.Worksheets(1).Range("A1").CurrentRegion.Copy _
     Destination:=wbk_3.Worksheets(1).Range("A1")

    'That's the first workbook done.
    'The second one we may need to tweak a little because we
    'don't want the headings this time.
    'I'm going to use a range reference for this so that
    'it'll be a little more transparent than doing it in one go.

    'First we grab the whole list range including headings, just
    'as we did for the first list.
    Set rng_Source = wbk_2.Worksheets(1).Range("A1").CurrentRegion

    'Next, we offset that by 1 row and shrink the size of it by
    '1 row to get rid of the headings.
    Set rng_Source = rng_Source.Offset(1, 0) _
     .Resize(rng_Source.Rows.Count - 1, rng_Source.Columns.Count)

    'I'll use another range to get the location of the existing list
    'in the combined workbook.
    Set rng_Target = wbk_3.Worksheets(1).Range("A1").CurrentRegion

    'Now we copy the second list and paste it to one cell below that range
    rng_Source.Copy Destination:=rng_Target.Offset(rng_Target.Rows.Count, 0)

ExitPoint:

On Error Resume Next

Set wbk_3 = Nothing
Set wbk_2 = Nothing
Set wbk_1 = Nothing
Set rng_Source = Nothing
Set rng_Target = Nothing

On Error GoTo 0

Exit Sub

ErrorHandler:

MsgBox "Error " & Err.Number & vbCrLf & Err.Description

Resume ExitPoint

End Sub
于 2013-03-29T07:29:15.827 に答える
0

問題の試みを奨励する StackOverflow の精神で、私は次の提案をします。

マクロレコーダーをご存知ですか?(これは [開発] タブにあります (Excel リボン オプションで公開する必要がある場合があります)。)

これを手動で行うアクションを記録すると、マクロ レコーダは必要なコードの 90% を生成します。

あとは、ハードコードされた範囲参照を CurrentRegion などの動的参照に置き換えて、リストの内容を取得するだけです。それで行き詰まったら、ぜひもう一度投稿してください。

3 番目の (ターゲット) ワークブックにマクロを記録して、ソース ワークブックからコンテンツを簡単に取り込めるようにすることをお勧めします。

于 2013-03-29T01:13:18.023 に答える