简体   繁体   English

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

[英]vba code to insert row and data in ascending order

I'm still quite new to VBA and I'm basically self taught. 我对VBA还很新,我基本上都是自学成才。 I've developed a spreadsheet for work and I need to alter my code to insert a new row where the data needs to go in ascending order. 我已经开发了一个工作电子表格,我需要更改我的代码以插入一个新行,其中数据需要按升序排列。 This is the code I am using currently to have the user form insert the data into the appropriate cells with the information that pertains to each column: 这是我目前使用的代码,用户表单将数据插入到具有与每列相关的信息的相应单元格中:

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

What I need it to do is once the user hits "Add" I need the code to sort through the first column to find where the part number needs to go and keep all the relevant cells in the same row. 我需要它做的是一旦用户点击“添加”我需要代码来排序第一列,以找到部件号需要去的地方,并将所有相关的单元保持在同一行。 My first two rows are headings and my data ranges from A:BQ. 我的前两行是标题,我的数据范围是A:BQ。 I've tried a few different codes 我尝试过几个不同的代码

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

This one moves data into the first two heading columns and actually puts the part number where it needs to be, but does not fill in the info across the whole row. 这个将数据移动到前两个标题列中,并实际将部件号放在需要的位置,但不填写整行的信息。

I understand that you data was initially sorted by the part number, and you want each insertion to fit at the appropriate row to keep the sorting valid. 我知道您的数据最初是按部件号排序的,并且您希望每个插入都适合相应的行以保持排序有效。 You might try this: 你可以试试这个:

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

if your data starts from row 2 and you have headers in row 1, then use: 如果您的数据从第2行开始并且您在第1行中有标题,则使用:

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

while if your data starts from row 2 and you don't have headers in row 1, then use: 如果您的数据从第2行开始并且第1行中没有标题,则使用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM