从找到的值中删除整列

时间:2019-07-10 18:54:14

标签: excel vba

因此,我希望从文本框(即表单)中查找值,然后我希望删除该值的列以及左右两边的列。但是由于某种原因,Rango.Column无法正确获取列。 代码成功找到了值。

Private Sub CommandButton1_Click()
Dim Rango As Range

Dim Prueba_N As String

    Prueba_N = Partida.Txt.Value
    If Prueba_N <> 0 Then
        With Sheets("Prueba").Rows("6:6")
            Set Rango = .Find(What:=Partida_N, After:=.Cells(.Cells.Count), LookIn:=xlValues, Lookat:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False)

        End With

            RangoC_1 = Rango.Column - 1
            RangoC_2 = Rango.Column + 1
            Sheets("Prueba").Range(Cells(1, RangoC_1), Cells(1, RangoC_2)).EntireColumn.Delete
            Finalizar = MsgBox("Se ha eliminado la partida exitosamente")
            Partida.Hide
            Sheets("Materiales").Activate

    Else
        Partida_Err = MsgBox("Agregar Partida", vbCritical)
    End If

End Sub

我也不知道为什么有时如果我从按钮启动partida的表格时,它会在Sheets("Prueba").Range...行上向我返回1004错误,但是如果我直接启动该表格,却没有出现错误。

1 个答案:

答案 0 :(得分:1)

您将txt.value放入名为Prueba_N的变量中,然后搜索Partida_N

**Prueba_N** = Partida.Txt.Value
If Prueba_N <> 0 Then
    With Sheets("Prueba").Rows("6:6")
        Set Rango = .Find(What:=**Partida_N**, After:=.Cells(.Cells.Count), LookIn:=xlValues, Lookat:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False)
    End With

应该是

**Partida_N**, = Partida.Txt.Value
If Prueba_N <> 0 Then
    With Sheets("Prueba").Rows("6:6")
        Set Rango = .Find(What:=**Partida_N**, After:=.Cells(.Cells.Count), LookIn:=xlValues, Lookat:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False)
    End With

Rows("6,6")

搜索第66行

您想要

Rows("6")