1

他のスプレッドシートで見たことから、これは可能であるに違いないと確信していることを除いて、私は実質的にVBAの経験がありません。私はあちこちを検索しましたが、私が使用できるヘルプやコードの説明が見つかりません。誰かが助けてくれることを願っています。

ウェブサイトのカートからダウンロードしましたが、データをフォーマットしていないため、新しい販売注文/請求書生成ソフトウェアにアップロードする必要があります。

例として、データが現在どのように見えるかを示す画像へのリンクがあります(ワークブックは「Orders.csv」と呼ばれますが、必要に応じてxlsxに変換できます)。

http://web225.extendcp.co.uk/fiercepc.co.uk/img1.jpg

顧客が複数の製品(製品の数量ではなく、完全に異なる製品)を購入したかどうかを確認できるように、行全体にリストされています。最初の製品は列Hから始まり、2番目は列Oから、3番目は列Vからというように続きます。

次のようにデータを表示する必要があります。

http://web225.extendcp.co.uk/fiercepc.co.uk/img2.jpg

したがって、各製品は互いに下にリストされ、その前に同じ顧客の詳細が表示されます。これは、請求ソフトウェアが各注文IDを確認し、それに応じてすべての異なる製品を示す請求書を作成できるようにするためです。

私はこれについてどうやって行くのか分かりません。行にセル内のデータがあるかどうかをチェックし、それに応じて範囲をコピーするループマクロである必要があると思います。また、マクロは別のブック(マクロと呼ばれることもあります)にある必要があるため、ダウンロードされるたびに新しいブックになるため、このダウンロードに基づいて動作します。これが理にかなっていることを願っています。

これは私だけでなく、誰かにとって非常に簡単なことだと確信しています。助けてください!これは単なるスプレッドシートの例であり、実際のシートははるかに大きく、より多くのデータが含まれているため、範囲などを操作できるように、説明付きのマクロが必要です。

4

2 に答える 2

2

私はelsewereから自分の質問に対する答えを得ることができましたが、答えが的確で詳細だったので、興味があるかもしれないすべての人と答えを共有したいと思いました。

'****This macro is to use on sheets within the same workbook
'****If you want to transfer your data to another workbook you
'****will have to alter the code somewhat, but the idea is the same

Sub copydata()
Dim x As Integer
Dim y As Integer
Dim ws1 As Worksheet
Dim ws2 As Worksheet

Set ws1 = Worksheets("Ouput sheet") 'whatever you worksheet is
Set ws2 = Worksheets("Orders") 'or whatever your worksheet is called


'Item 1 - I'm calling the separate sections where each item ordered is in your worksheet Item 1, Item 2
'this encompasses columns H-N for item 1, etc, etc
r = 3 'this is the first row where your data will output
x = 3 'this is the first row where you want to check for data
Do Until ws2.Range("A" & x) = "" 'This will loop until column A is empty, set the column to whatever you want
                            'but it cannot have blanks in it, or it will stop looping. Choose a column that is
                            'always going to have data in it.

If Not ws2.Range("H" & x).Value = "" Then 'This checks your column H to make sure it's not empty
                                            'If empty, it goes on to the next line, if not it copies the data.
                                            'This column should be something that will have something in it if
                                            'there is a product ordered for Item 1
                                            'i.e. don't choose column J if it will have blanks where there is
                                            'actually an item ordered

'this section copies the data, the worksheet left of the = sign is the one data will be written to
    ws1.Range("A" & r).Value = ws2.Range("A" & x).Value 'Order Date
    ws1.Range("B" & r).Value = ws2.Range("B" & x).Value 'Order ID
    ws1.Range("C" & r).Value = ws2.Range("C" & x).Value 'Customer
    ws1.Range("D" & r).Value = ws2.Range("D" & x).Value 'Billing Add
    ws1.Range("E" & r).Value = ws2.Range("E" & x).Value 'Subtotal
    ws1.Range("F" & r).Value = ws2.Range("F" & x).Value 'Tax Amount
    ws1.Range("G" & r).Value = ws2.Range("G" & x).Value 'Total Amount
    ws1.Range("H" & r).Value = ws2.Range("H" & x).Value 'Product ID
    ws1.Range("I" & r).Value = ws2.Range("I" & x).Value 'Column J - couldn't read your headings for a few of these
    ws1.Range("J" & r).Value = ws2.Range("J" & x).Value 'Column K
    ws1.Range("K" & r).Value = ws2.Range("K" & x).Value 'L
    ws1.Range("L" & r).Value = ws2.Range("L" & x).Value 'Price
    ws1.Range("M" & r).Value = ws2.Range("M" & x).Value 'Attributes

    r = r + 1 'Advances r and x when there is a matching case
    x = x + 1
Else
    x = x + 1 'Advances only x (to check the next line) when there is not a matching case,
                'i.e. your output line stays on the next line down from where it last wrote data
                'while x advances
End If
Loop 'End of Item 1


'Item 2

x = 3 'this time we only define x, we want r to stay where it's at so it can continue copying the data into one
    'seamless list
Do Until ws2.Range("A" & x) = "" 'still want this to stay the same

If Not ws2.Range("O" & x).Value = "" Then 'This one needs to change to match the column in your second Item

'the ranges on ws1 will stay the same, ws2 ranges pertaining to customer data stay the same, ws2 ranges pertaining
'to specific Item 2 info will change
    ws1.Range("A" & r).Value = ws2.Range("A" & x).Value 'Order Date       *SAME
    ws1.Range("B" & r).Value = ws2.Range("B" & x).Value 'Order ID       *SAME
    ws1.Range("C" & r).Value = ws2.Range("C" & x).Value 'Customer       *SAME
    ws1.Range("D" & r).Value = ws2.Range("D" & x).Value 'Billing Add       *SAME
    ws1.Range("E" & r).Value = ws2.Range("E" & x).Value 'Subtotal       *SAME
    ws1.Range("F" & r).Value = ws2.Range("F" & x).Value 'Tax Amount       *SAME
    ws1.Range("G" & r).Value = ws2.Range("G" & x).Value 'Total Amount       *SAME
    ws1.Range("H" & r).Value = ws2.Range("O" & x).Value 'Product ID       *CHANGED!!!!
    ws1.Range("I" & r).Value = ws2.Range("P" & x).Value 'Column J       *CHANGED!!!!
    ws1.Range("J" & r).Value = ws2.Range("Q" & x).Value 'Column K       *CHANGED!!!!
    ws1.Range("K" & r).Value = ws2.Range("R" & x).Value 'L       *CHANGED!!!!
    ws1.Range("L" & r).Value = ws2.Range("S" & x).Value 'Price       *CHANGED!!!!
    ws1.Range("M" & r).Value = ws2.Range("T" & x).Value 'Attributes       *CHANGED!!!!

    r = r + 1 'Advances r and x when there is a matching case
    x = x + 1
Else
    x = x + 1 'Advances only x (to check the next line) when there is not a matching case,
                'i.e. your output line stays on the next line down from where it last wrote data
                'while x advances
End If
Loop 'End of Item 2
'simply copy Item 2 code and change the appropriate values to match Items 3,4,5,6, etc, etc


'You will get a list of all the info for Item 1, follow by all info for Item 2, etc, etc
'i.e. if Paul orders 2 items, they won't end up right below each other, but his second
'item will end up farther down, but will still be on the list
'If this is not what you want you could sort afterwards or alter the code, but it is a significant alteration

End Sub
于 2012-07-03T17:43:09.790 に答える
0

解決策は次のとおりです。

  1. 行をループする
  2. 行ごとに、占有されている列の数を取得します。
  3. 簡単な計算で行の注文数を見つけます(各製品の注文が同じ列数を占めると仮定します)
  4. 注文をループする-新しいシートに製品データをコピーする
  5. これらのコピー操作のそれぞれについて、最も外側のループで処理中の行から顧客データをコピーします。
  6. 最後の要件は。マクロでワークブックorders.csvを開き(ファイル名と場所が同じであると想定)、上記のすべての操作を実行します。

私はあなたのためにそれを書くことができます。しかし、あなたがそれを自分で書くならば、それはあなたにとって良い学習経験になるでしょう。ここにstackoverflowで、ほとんどのクエリ(行の占有列数を取得する方法など)に対する回答を見つけることができます。

また、Excel VBAの使用を開始するには、次のページにアクセスしてください: http ://www.functionx.com/vbaexcel/

于 2012-06-12T03:55:30.627 に答える