2017-03-03 24 views
1

私はまだVBAには新しく、私は基本的に独学です。私は仕事のためにスプレッドシートを開発しました。データを昇順で移動する必要がある新しい行を挿入するようにコードを変更する必要があります。 これは私がユーザーフォームが各列に関係する情報を適切な細胞にデータを挿入持つように、現在使用していますコードです:私は、ユーザーがヒットしたら、「追加され行うには、それを必要とする何行とデータを昇順に挿入するためのVBAコード

Private Sub cmdAdd_Click() 
    'Copy input values to sheet. 
    Dim lRow As Long 
    Dim ws As Worksheet 
    Set ws = Worksheets("Part Number Database") 
    lRow = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row 
    With ws 
     .Cells(lRow, 1).Value = Me.txtpartnumber.Value 
     .Cells(lRow, 2).Value = Me.txtdescrip.Value 
     .Cells(lRow, 3).Value = Me.cbodept.Value 
     .Cells(lRow, 4).Value = Me.cbomedia.Value 
     .Cells(lRow, 5).Value = Me.cboeff.Value 
     .Cells(lRow, 6).Value = Me.txtcfm.Value 
     .Cells(lRow, 7).Value = Me.cbofm.Value 
     .Cells(lRow, 8).Value = Me.cbofs.Value 
     .Cells(lRow, 9).Value = Me.cbogasketsize.Value 
     .Cells(lRow, 10).Value = Me.cbogasketpn.Value 
     .Cells(lRow, 11).Value = Me.cbogasketloc.Value 
     .Cells(lRow, 12).Value = Me.cboseal.Value 
     .Cells(lRow, 13).Value = Me.cboadhesive.Value 
     .Cells(lRow, 14).Value = Me.cboresin.Value 
     .Cells(lRow, 15).Value = Me.cbohard.Value 
     .Cells(lRow, 16).Value = Me.cbogelres.Value 
     .Cells(lRow, 17).Value = Me.cbogelhard.Value 
     .Cells(lRow, 18).Value = Me.cbohotmelt.Value 
     .Cells(lRow, 19).Value = Me.cbofglocation.Value 
     .Cells(lRow, 20).Value = Me.cbofg1.Value 
     .Cells(lRow, 21).Value = Me.txtfg1.Value 
     .Cells(lRow, 22).Value = Me.cbofg2.Value 
     .Cells(lRow, 23).Value = Me.txtfg2.Value 
     .Cells(lRow, 24).Value = Me.cbofg3.Value 
     .Cells(lRow, 25).Value = Me.txtfg3.Value 
     .Cells(lRow, 27).Value = Me.cbomisc1.Value 
     .Cells(lRow, 28).Value = Me.txtmisc1.Value 
     .Cells(lRow, 29).Value = Me.cbomisc2.Value 
     .Cells(lRow, 30).Value = Me.txtmisc2.Value 
     .Cells(lRow, 31).Value = Me.cbomisc3.Value 
     .Cells(lRow, 32).Value = Me.txtmisc3.Value 
     .Cells(lRow, 33).Value = Me.cbomisc4.Value 
     .Cells(lRow, 34).Value = Me.txtmisc4.Value 
     .Cells(lRow, 35).Value = Me.cbomisc5.Value 
     .Cells(lRow, 36).Value = Me.txtmisc5.Value 
     .Cells(lRow, 37).Value = Me.cbomisc6.Value 
     .Cells(lRow, 38).Value = Me.txtmisc6.Value 
     .Cells(lRow, 39).Value = Me.cbomisc7.Value 
     .Cells(lRow, 40).Value = Me.txtmisc7.Value 
     .Cells(lRow, 41).Value = Me.cbomisc8.Value 
     .Cells(lRow, 42).Value = Me.txtmisc8.Value 
     .Cells(lRow, 43).Value = Me.txtminiheight.Value 
     .Cells(lRow, 44).Value = Me.txtppi.Value 
     .Cells(lRow, 45).Value = Me.cboalum.Value 
     .Cells(lRow, 46).Value = Me.cbowood.Value 
     .Cells(lRow, 48).Value = Me.cbometal1.Value 
     .Cells(lRow, 49).Value = Me.txtmetal1.Value 
     .Cells(lRow, 50).Value = Me.cbometal2.Value 
     .Cells(lRow, 51).Value = Me.txtmetal2.Value 
     .Cells(lRow, 52).Value = Me.cbometal3.Value 
     .Cells(lRow, 53).Value = Me.txtmetal3.Value 
     .Cells(lRow, 54).Value = Me.cbometal4.Value 
     .Cells(lRow, 55).Value = Me.txtmetal4.Value 
     .Cells(lRow, 56).Value = Me.cbometal5.Value 
     .Cells(lRow, 57).Value = Me.txtmetal5.Value 
     .Cells(lRow, 58).Value = Me.cbometal6.Value 
     .Cells(lRow, 59).Value = Me.txtmetal6.Value 
     .Cells(lRow, 60).Value = Me.cbometal7.Value 
     .Cells(lRow, 61).Value = Me.txtmetal7.Value 
     .Cells(lRow, 62).Value = Me.cbometal8.Value 
     .Cells(lRow, 63).Value = Me.txtmetal8.Value 
     .Cells(lRow, 64).Value = Me.cbometal9.Value 
     .Cells(lRow, 65).Value = Me.txtmetal9.Value 
     .Cells(lRow, 66).Value = Me.txtheight.Value 
     .Cells(lRow, 67).Value = Me.txtlength.Value 
     .Cells(lRow, 68).Value = Me.txtwidth.Value 
     .Cells(lRow, 69).Value = Me.txtbuild.Value 

「最初の列をソートして、部品番号の場所を調べ、関連するすべてのセルを同じ行に保持するコードが必要です。最初の2行は見出しで、データの範囲はA:BQです。 私はこの1つは、最初の二つの見出しの列にデータを移動し、実際にそれをする必要があります部品番号を置きますが、全体の行全体で情報を記入していない、いくつかの異なるコード

'Sort the rows based on the data in column A 
    Columns("A").Sort key1:=Range("A2"), _ 
     order1:=xlAscending 

を試してみました。

+0

これはかなり大きいスプレッドシートで、5000を超える部品番号が付いています。本書の他のワークシートは、情報を引き出すために昇順になるようにこれらの部品番号に依存しているため、人為的ミスを減らすことを検討しています。 –

+0

似たような質問がたくさんありますが、それらの回答を読んで回答を得ることができます。例えば:http://stackoverflow.com/a/14859682 –

答えて

0

データが最初に部品番号でソートされていて、各挿入が適切な行に収まるようにしてソートを有効にしておきたいと思います。あなたはこれを試してみてください:

With ws 
    lRow = 1 + Application.Match(txtpartnumber, .Columns(1), True) 
    .Rows(lRow).Insert Shift:=xlDown 
    .Cells(lRow, 1).value = txtpartnumber 
    ' etc... Fill other data at the row 
    ' ... 
End With 
+0

私は、行の挿入を避ける傾向がありますし、スピードのために(これが問題になるはずです)、おそらく変更することはありません_surrounding_ range – user3598756

+0

ASHありがとうございますそのコードは完璧に機能しました!我々は週に20以上の新しい部品番号を入力し、これは間違いなく時間を節約するのに役立ちます。 –

+0

@AshleyGoodwinあなたは歓迎されて、それが助けてくれたことを嬉しく思っています:) –

0

あなたのデータは2行目から始まり、あなたは1行のヘッダーを持って、その後、使用している場合:

Columns("A:N").Sort key1:=Range("A1"), order1:=xlAscending, Header:=xlYes 

あなたのデータは2行目から始まり、そうでない場合は一方で1行目にヘッダーがある場合は、

Range("A2:N" & Cells(Rows.count, 1).End(xlUp).row).Sort key1:=Range("A2"), order1:=xlAscending, Header:=xlNo 
+0

@AshleyGoodwin、このコードを試しましたか? – user3598756

関連する問題