如何使用VBA基于标头值选择列

时间:2016-04-19 07:24:52

标签: excel vba excel-vba

如果在该列中找不到特定值,我目前正在删除行。

excel数据示例

Sl.No     Content
1         Apple
2         Ball
3         Cat
4         Ball
5         Cat

我正在使用的代码是

Sub Delete()

Dim startrow As Long
'starting row number here
startrow = 2
' Assuming data to check is in A Column
Do Until startrow > Cells(Cells.Rows.Count, "A").End(xlUp).Row
    If Cells(startrow, 2).Value = "Ball" Then
        Rows(startrow).Delete
    Else
        startrow = startrow + 1
    End If
Loop

End Sub

此代码的输出是

Sl.No     Content
1         Apple
3         Cat
5         Cat

此代码工作正常。现在我需要一个小小的改变。我正在尝试使用列标题“Content”选择B列,然后删除包含“Ball”的行,而不是提供单元格编号。

请帮我解决这个问题。 TIA

1 个答案:

答案 0 :(得分:2)

roadtrainCol.setCellValueFactory(
            new Callback<CellDataFeatures<Spot,Boolean>,ObservableValue<Boolean>>()
            {
                public ObservableValue<Boolean> call(CellDataFeatures<Spot, Boolean> param) {
                    return param.getValue().getRoadtrain();
                }   
            });
    roadtrainCol.setCellFactory(col -> {
        CheckBoxTableCell<Spot, Boolean> cell = new CheckBoxTableCell<>(index -> {
            BooleanProperty active = new SimpleBooleanProperty();
            active.addListener((obs, wasActive, isNowActive) -> {
                System.out.println("del");
                rettedeSpots(tableViewId.getItems().get(index).getId());
            });
            return active ;
        });
        return cell ;
    });