过滤器中的最后一行偏移量

时间:2014-01-17 21:03:02

标签: excel vba excel-vba filter

因此我的宏的目的是为部件号添加版本号。首先,用户将输入所有数据,然后我的宏将在表格底部创建一个包含该信息的新条目。然后它将根据部件号过滤表格。仍然在过滤器中我需要它来查看前一个条目并为我当前的条目添加1。例如,如果前一个是01,则下一个需要为02,依此类推。我遇到麻烦让我的宏运行下面的添加1部分是我的代码。我想在过滤器中可能无法实现,或者我必须以不同的方式编写代码。任何见解都会非常棒!谢谢!

    'Update version number
 Sheets("New Version ").Select
    part = Range("B4").Value
    Sheets("PN_List").Select
    ActiveSheet.Range("$A$1:$K$3000").AutoFilter Field:=1, Criteria1:=part
    ActiveWorkbook.Worksheets("PN_List").AutoFilter.Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("PN_List").AutoFilter.Sort.SortFields.Add Key:= _
        Range("B1:B3000"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption _
        :=xlSortNormal
    With ActiveWorkbook.Worksheets("PN_List").AutoFilter.Sort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    Worksheets("PN_List").Activate
With Range("B" & Rows.Count).End(xlUp).Offset(1)
    .Value = "0" & .Offset(-1).Value + 1
    End With

3 个答案:

答案 0 :(得分:0)

我的猜测是你的格式格式为“常规”

右键单击该单元格,转到“设置单元格格式”。将类别更改为“文本”

或者,如果您希望在VBA中完成,请添加:

.Value = "0" & .Offset(-1).Value + 1
.Style = "Text"

答案 1 :(得分:0)

按要求:

'Update version number
Sheets("New Version ").Select
part = Range("B4").Value
Sheets("PN_List").Select
ActiveSheet.Range("$A$1:$K$3000").AutoFilter Field:=1, Criteria1:=part
ActiveWorkbook.Worksheets("PN_List").AutoFilter.Sort.SortFields.Clear
ActiveWorkbook.Worksheets("PN_List").AutoFilter.Sort.SortFields.Add Key:= _
    Range("B1:B3000"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption _
    :=xlSortNormal
With ActiveWorkbook.Worksheets("PN_List").AutoFilter.Sort
  .Header = xlYes
  .MatchCase = False
  .Orientation = xlTopToBottom
  .SortMethod = xlPinYin
  .Apply
End With
Worksheets("PN_List").Activate
With Range("B" & Rows.Count).End(xlUp).Offset(1)
  ' following line was added to set text format in the target cell
  .NumberFormat = "@"
  .Value = "0" & .Offset(-1).Value + 1
End With

答案 2 :(得分:0)

让我们重新编写你的代码。

Dim wsNewVersion = Thiwworkbook.Sheets("New Version")
Dim wsPNList = Thiwworkbook.Sheets("PN_List")

part = wsNewVersion.Range("B4").Value

wsPNList.Range("A1:K3000").AutoFilter Field:= 1, Criteria1:=part
wsPNList.AutoFilter.Sort.SortFields.Clear
wsPNList.AutoFilter.Sort.SortFields.Add Key:= _
wsPNList.Range("B1:B3000"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption _
:=xlSortNormal
With wsPNList.AutoFilter.Sort
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With

'~~> Below code is what you need i guess

Dim myrng As Range, lrow As Long
Dim myadd

'~~> Get the Range Address of the visible cells only
lrow = wsPNList.Range("A" & Rows.Count).End(xlUp).Row
Set myrng = wsPNList.Range("B1", Range("B" & lrow)).Offset(1, 0).Resize(lrow - 1).SpecialCells(xlCellTypeVisible)

'~~> pass addresses in array
myadd = Split(myrng.Address, ",")

'~~> Now you know the address, you can assign the value
With wsPNList
    .Range(myadd(UBound(myadd))).Value = Range(myadd(UBound(myadd) - 1)).Value + 1
End With    

希望这是你所需要的。
我相信它是:)但是谁知道。