我在使用按钮清除列表框中的整行数据时遇到了问题,我的想法是我有一个文本框,其来源是工作表" x",我希望能够当我按下按钮时从列表框中选择一个项目时,清除此工作表中的文本。
这就是我一直在尝试的:
Dim ws4 As Worksheet
Set ws4 = Worksheets("ALCANCES")
With ws4
Dim r1 As Range
Set r1 = ws4.Range("A1:A200")
For Each c1 In r1
If c1.Value = listAlc.Value Then
c1.Font.Bold = True
With c1
c1.EntireRow.Clear
End With
End If
Next c1
End With
此代码工作正常,如果不是清除我输入删除整行,但我不想删除该行,我只想清除它上面的信息,这个代码有时会这样做,但有时它清除我选择的行及其上方的所有内容。
提前致谢。
答案 0 :(得分:0)
Private Sub ButAlbo_Click()
Dim ws4 As Worksheet
Set ws4 = Worksheets("ALCANCES")
Dim fnd As Variant
Dim rplc As Variant
iRow = ws4.Range("d1").Value
fnd = iRow
rplc = Clear
ws4.Cells.Replace what:=fnd, Replacement:=rplc, _
LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
SearchFormat:=False, ReplaceFormat:=False
End Sub
答案 1 :(得分:0)
列表框属性: 名字(lisAlcan) rowsource ALCANCES!A1:C300 boundcolum 3 columcount 2 controlsource ALCANCES!D1
然后我创建了两个按钮来添加信息,另一个按钮从列表和工作表中删除该信息:
Private Sub butAlgr_Click()
If txtAlcan.Value = "" Then
MsgBox "Ingrese el Alcance", vbOKOnly
Exit Sub
End If
Dim LastRow As Long, ws As Worksheet
Set ws = Sheets("ALCANCES")
LastRow = ws.Range("A" & Rows.Count).End(xlUp).Row + 1 'Finds the last blank row
LastRow = ws.Range("B" & Rows.Count).End(xlUp).Row + 1 'Finds the last blank row
LastRow = ws.Range("C" & Rows.Count).End(xlUp).Row + 1 'Finds the last blank row
ws.Range("A" & LastRow).Value = "*" 'Adds the TextBox3 into Col A & Last Blank Row
ws.Range("B" & LastRow).Value = txtAlcan.Text 'Adds the ComboBox1 into Col B & Last Blank Row
ws.Range("C" & LastRow).Value = LastRow
'clear the data
Me.txtAlcan.Value = ""
Me.txtAlcan.SetFocus
MsgBox "Datos registrados", vbOKOnly
End Sub
和删除按钮:
Private Sub ButAlbo_Click()
Dim ws4 As Worksheet
Set ws4 = Worksheets("ALCANCES")
Dim fnd As Variant
Dim rplc As Variant
iRow = ws4.Range("d1").Value
ws4.Cells(iRow, 1).Value = Clear
ws4.Cells(iRow, 2).Value = Clear
ws4.Cells(iRow, 3).Value = Clear
End Sub