2014-09-02 28 views
1

我已经找遍了,但找不到它的一个例子。我有一个代码,用于搜索列并删除包含该单元格中的确切值的所有行。有没有可能有一个InputBox决定我的代码在哪一列看

我要让这一点,可以在任何表中使用两个输入框的第一个要求用户看在列(这是我有一个问题)

和第二码要求标准。

现有的代码是这样的。

Sub OCR() 
    Dim Firstrow As Long 
    Dim Lastrow As Long 
    Dim Lrow As Long 
    Dim CalcMode As Long 
    Dim ViewMode As Long 

    With Application 
     CalcMode = .Calculation 
     .Calculation = xlCalculationManual 
     .ScreenUpdating = False 
    End With 

    With ActiveSheet 
     .Select 
     ViewMode = ActiveWindow.View 
     ActiveWindow.View = xlNormalView 
     .DisplayPageBreaks = False 
     Firstrow = .UsedRange.Cells(1).Row 
     Lastrow = .UsedRange.Rows(.UsedRange.Rows.Count).Row 
     For Lrow = Lastrow To Firstrow Step -1 

    'Amend test 
    With .Cells(Lrow, "B") 
    If Not IsError(.Value) Then 
    If .Value = "Test" Then .EntireRow.Delete 
    End If 
    End With 


    'END of loop 
     Next Lrow 

    End With 

那么这将删除所有行测试是在相关列(B),但我怎么可以使用的InputBox决定列,并寻找价值。

+0

我建议让用户在Excel表单输入列名和绕圈成2个细胞和读出的来自您的VBA代码的值。否则,你需要两个输入框,一个用于列名,一个用于标准,在你的代码开始处(在Dim之后) – MikeD 2014-09-02 10:25:26

+0

但是这不会仅仅适用于一个电子表格。所以我不能确定那些细胞总是可用 – Flaunting 2014-09-02 10:26:29

+0

这只是不正确的方式来实现你想要的东西:)使用'AUTOFILTER'。参见[THIS](http://stackoverflow.com/questions/11631363/how-to-copy-a-line-in-excel-using-a-specific-word-and-pasting-to-another-excel-s )和过滤器后简单地删除行。 – 2014-09-02 10:42:09

回答

2

替换:

With .Cells(Lrow, "B") 

与:

s = Application.InputBox("Pick a cell", Type:=8).Address(1, 0) 
lettre = Split(s, "$")(0) 
With .Cells(Lrow, lettre) 

EDIT#1

当的InputBox出现后,使用 鼠标挑选任何细胞中所需的列。

EDIT#2

当的InputBox时,键入单元地址来接的任何细胞中所需的列。您的ScreenUpdating设置禁止使用鼠标。

EDIT#3

最终通过打着使用的方法是:

s = Application.InputBox("Enter Column Data", Type:=2) 
With .Cells(Lrow, s) 
+0

嘿谢谢你的回应,但不幸的是,这不起作用,当试图输入它的错误列的值。如果我使用“C”例如它说:“您输入的公式包含错误”如果我通过数字引用该列它不起作用 – Flaunting 2014-09-02 12:18:50

+0

当您看到输入框时,使用**鼠标**选择任何单元格在期望的列中。 – 2014-09-02 12:20:34

+0

这不会发生在我身上,当我看到输入框时它只会让我输入,而我无法在后台单击电子表格 – Flaunting 2014-09-02 12:22:37

相关问题