用于按行的顺序插入行和数据的vba代码

时间:2017-03-03 23:26:49

标签: excel vba excel-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

我需要它做的是一旦用户点击“添加”我需要代码对第一列进行排序以找到部件号需要去的位置并将所有相关单元保持在同一行中。我的前两行是标题,我的数据范围是A:BQ。 我尝试过几个不同的代码

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

这个将数据移动到前两个标题列中,实际上将部件号放在需要的位置,但不填写整行的信息。

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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