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
我需要做的是一旦用户点击“添加”我需要代码sorting通过第一列find零件号码需要去的地方,并保持所有相关单元格在同一行。 我的前两行是标题,我的数据范围从A:BQ。 我已经尝试了一些不同的代码
'Sort the rows based on the data in column A Columns("A").Sort key1:=Range("A2"), _ order1:=xlAscending
这个将数据移动到前两个标题栏中,实际上将零件号码放在需要的位置,但不填写整行信息。
我了解到,您的数据最初是通过部件号进行sorting的,并且希望每个插入适合适当的行以保持sorting有效。 你可以试试这个:
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
如果您的数据从第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