ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 Excel Home精品图文教程库
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
300集Office 2010微视频教程 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 926|回复: 8

[求助] 表二的窗体通过删除按钮为什么删不了表一的内容

[复制链接]

TA的精华主题

TA的得分主题

发表于 2020-3-11 19:21 | 显示全部楼层 |阅读模式
窗体在表二,通过删除记录按钮为什么删不了表一的数据,但是如果学员查询停在表一就可以把数据删掉,这段代码哪里的问题,求老师指正,谢谢

Private Sub CommandButton8_Click() '删除记录
Dim xrow As Long   这个指定删除表一的代码似乎没有用
    With Sheet1
If Len(店名) = 0 Then
MsgBox "内容为空,删除学员信息失败!!!", 16, "温馨提示……": Exit Sub
End If
If MsgBox("是否真的要删除该学员信息?", vbQuestion + vbYesNo, "删除学员") = vbYes Then
    Dim j
   Application.ScreenUpdating = False
   xrow = Range("A2").CurrentRegion.Rows.Count + 1
   For j = Cells(Rows.Count, 2).End(3).Row To 3 Step -1
       If Cells(j, 1) = 学员电话.Text Then
           If Val(姓名.Text) = Val(Cells(j, 2)) And (性别.Text) = (Cells(j, 3)) And (区域.Text) = (Cells(j, 4)) And Val(店名.Text) = Val(Cells(j, 5)) And (卡项名称.Text) = (Cells(j, 6)) And Val(卡号.Text) = Val(Cells(j, 7)) And Val(老板电话.Text) = Val(Cells(j, 8)) And Val(激活日期.Text) = Val(Cells(j, 9)) And Val(到期日期.Text) = Val(Cells(j, 10)) Then
               Rows(j).Delete
           End If
       End If
   Next j
   Application.ScreenUpdating = True
学员电话.Value = ""
姓名.Value = ""
性别.Value = ""
区域.Value = ""
店名.Value = ""
卡项名称.Value = ""
卡号.Value = ""
老板电话.Value = ""
激活日期.Value = ""
到期日期.Value = ""
UserForm7.学员电话.Enabled = True
UserForm7.姓名.Enabled = True
UserForm7.性别.Enabled = True
UserForm7.区域.Enabled = True
UserForm7.店名.Enabled = True
UserForm7.卡项名称.Enabled = True
UserForm7.卡号.Enabled = True
UserForm7.老板电话.Enabled = True
UserForm7.激活日期.Enabled = True
UserForm7.到期日期.Enabled = True
MsgBox "删除学员成功!!!", 48, "温馨提示……"
End If
'学员电话.Text = ""
'姓名.Text = ""
'性别.Text = ""
'区域.Text = ""
'店名.Text = ""
'卡项名称.Text = ""
'卡号.Text = ""
'老板电话.Text = ""
'激活日期.Text = ""
'到期日期.Text = ""
End With
End Sub

3.PNG
4.PNG
5.PNG
6.PNG

蓝泰学校学员登记库存(修改版).zip

100.99 KB, 下载次数: 1

TA的精华主题

TA的得分主题

发表于 2020-3-11 21:31 | 显示全部楼层
Rows(j).Delete 是针对activesheet
sheets("表1". Rows(j).Delete测试看看

基础不牢地动山摇。。。

评分

1

查看全部评分

TA的精华主题

TA的得分主题

 楼主| 发表于 2020-3-11 22:02 | 显示全部楼层
liulang0808 发表于 2020-3-11 21:31
Rows(j).Delete 是针对activesheet
sheets("表1". Rows(j).Delete测试看看

老师sheets("表1". Rows(j).Delete是不是少了一个括号啊,代码是红色字体,然后我加了个括号,sheets("学员信息建档"). Rows(j).Delete就不会了,但是还是没用
Private Sub CommandButton8_Click() '删除记录
Dim xrow As Long
    With Sheet1
If Len(店名) = 0 Then
MsgBox "内容为空,删除学员信息失败!!!", 16, "温馨提示……": Exit Sub
End If
If MsgBox("是否真的要删除该学员信息?", vbQuestion + vbYesNo, "删除学员") = vbYes Then
    Dim j
   Application.ScreenUpdating = False
   xrow = Range("A2").CurrentRegion.Rows.Count + 1
   For j = Cells(Rows.Count, 2).End(3).Row To 3 Step -1
       If Cells(j, 1) = 学员电话.Text Then
           If Val(姓名.Text) = Val(Cells(j, 2)) And (性别.Text) = (Cells(j, 3)) And (区域.Text) = (Cells(j, 4)) And Val(店名.Text) = Val(Cells(j, 5)) And (卡项名称.Text) = (Cells(j, 6)) And Val(卡号.Text) = Val(Cells(j, 7)) And Val(老板电话.Text) = Val(Cells(j, 8)) And Val(激活日期.Text) = Val(Cells(j, 9)) And Val(到期日期.Text) = Val(Cells(j, 10)) Then
               Sheets("学员信息建档").Rows(j).Delete
           End If
       End If
   Next j
   Application.ScreenUpdating = True

TA的精华主题

TA的得分主题

发表于 2020-3-12 07:41 | 显示全部楼层
深秋红叶2019 发表于 2020-3-11 22:02
老师sheets("表1". Rows(j).Delete是不是少了一个括号啊,代码是红色字体,然后我加了个括号,sheets("学 ...

楼主要考虑下,判断条件等等部分是不是也需要带表名的?

TA的精华主题

TA的得分主题

 楼主| 发表于 2020-3-12 09:10 | 显示全部楼层
liulang0808 发表于 2020-3-12 07:41
楼主要考虑下,判断条件等等部分是不是也需要带表名的?

老师,不知道是不是这样改,也没有反应,请老师指点
Private Sub CommandButton8_Click() '删除记录
Dim xrow As Long
    With Sheet1
If Len(店名) = 0 Then
MsgBox "内容为空,删除学员信息失败!!!", 16, "温馨提示……": Exit Sub
End If
If MsgBox("是否真的要删除该学员信息?", vbQuestion + vbYesNo, "删除学员") = vbYes Then
    Dim j
   Application.ScreenUpdating = False
   xrow = Sheets("学员信息建档").Range("A2").CurrentRegion.Rows.Count + 1
   For j = Cells(Rows.Count, 2).End(3).Row To 3 Step -1
       If Cells(j, 1) = 学员电话.Text Then
           If Val(姓名.Text) = Val(Cells(j, 2)) And (性别.Text) = (Cells(j, 3)) And (区域.Text) = (Cells(j, 4)) And Val(店名.Text) = Val(Cells(j, 5)) And (卡项名称.Text) = (Cells(j, 6)) And Val(卡号.Text) = Val(Cells(j, 7)) And Val(老板电话.Text) = Val(Cells(j, 8)) And Val(激活日期.Text) = Val(Cells(j, 9)) And Val(到期日期.Text) = Val(Cells(j, 10)) Then
               Sheets("学员信息建档").Rows(j).Delete
           End If
       End If
   Next j
   Application.ScreenUpdating = True
学员电话.Value = ""
姓名.Value = ""
性别.Value = ""
区域.Value = ""
店名.Value = ""
卡项名称.Value = ""
卡号.Value = ""
老板电话.Value = ""
激活日期.Value = ""
到期日期.Value = ""
UserForm7.学员电话.Enabled = True
UserForm7.姓名.Enabled = True
UserForm7.性别.Enabled = True
UserForm7.区域.Enabled = True
UserForm7.店名.Enabled = True
UserForm7.卡项名称.Enabled = True
UserForm7.卡号.Enabled = True
UserForm7.老板电话.Enabled = True
UserForm7.激活日期.Enabled = True
UserForm7.到期日期.Enabled = True
MsgBox "删除学员成功!!!", 48, "温馨提示……"
End If
'学员电话.Text = ""
'姓名.Text = ""
'性别.Text = ""
'区域.Text = ""
'店名.Text = ""
'卡项名称.Text = ""
'卡号.Text = ""
'老板电话.Text = ""
'激活日期.Text = ""
'到期日期.Text = ""
End With
End Sub

TA的精华主题

TA的得分主题

发表于 2020-3-12 09:17 | 显示全部楼层
本帖最后由 网海遨游 于 2020-3-12 09:20 编辑

以下涉及单元格cells都要带表名或者不带表名,但在引用或循环前,先激活要引用的表。如:sheets("你的表").activate
For j = Cells(Rows.Count, 2).End(3).Row To 3 Step -1
       If Cells(j, 1) = 学员电话.Text Then
           If Val(姓名.Text) = Val(Cells(j, 2)) And (性别.Text) = (Cells(j, 3)) And (区域.Text) = (Cells(j, 4)) And Val(店名.Text) = Val(Cells(j, 5)) And (卡项名称.Text) = (Cells(j, 6)) And Val(卡号.Text) = Val(Cells(j, 7)) And Val(老板电话.Text) = Val(Cells(j, 8)) And Val(激活日期.Text) = Val(Cells(j, 9)) And Val(到期日期.Text) = Val(Cells(j, 10)) Then

TA的精华主题

TA的得分主题

 楼主| 发表于 2020-3-12 09:59 | 显示全部楼层
网海遨游 发表于 2020-3-12 09:17
以下涉及单元格cells都要带表名或者不带表名,但在引用或循环前,先激活要引用的表。如:sheets("你的表"). ...

老师改完后是这样的就弹出如图错误,然后我把学员信息建档的Private Sub Worksheet_Deactivate()     UserForm1.Hide  '.Hide
End Sub删掉了,但是点击删除按钮的时候就自动跳到了学员信息建档表,这样的话我又要重新点回表二看我的报名登记情况,我想要的效果是窗体仍然停在报名登记表界面也可以删掉学员的建档信息,应该怎么改呢
Private Sub CommandButton8_Click() '删除记录Dim xrow As Long                     '定义变量xrow,用来保存要输入数据的工作表行号
With Sheet1
If Len(店名) = 0 Then
MsgBox "内容为空,删除学员信息失败!!!", 16, "温馨提示……": Exit Sub
End If
If MsgBox("是否真的要删除该学员信息?", vbQuestion + vbYesNo, "删除学员") = vbYes Then
    Dim j
   Application.ScreenUpdating = False
   xrow = Range("A2").CurrentRegion.Rows.Count + 1
   Sheets("学员信息建档").Activate
   For j = Cells(Rows.Count, 2).End(3).Row To 3 Step -1
       If Cells(j, 1) = 学员电话.Text Then
           If Val(姓名.Text) = Val(Cells(j, 2)) And (性别.Text) = (Cells(j, 3)) And (区域.Text) = (Cells(j, 4)) And Val(店名.Text) = Val(Cells(j, 5)) And (卡项名称.Text) = (Cells(j, 6)) And Val(卡号.Text) = Val(Cells(j, 7)) And Val(老板电话.Text) = Val(Cells(j, 8)) And Val(激活日期.Text) = Val(Cells(j, 9)) And Val(到期日期.Text) = Val(Cells(j, 10)) Then
               Sheets("学员信息建档").Rows(j).Delete
           End If
       End If
   Next j
   Application.ScreenUpdating = True
学员电话.Value = ""
姓名.Value = ""
性别.Value = ""
区域.Value = ""
店名.Value = ""
卡项名称.Value = ""
卡号.Value = ""
老板电话.Value = ""
激活日期.Value = ""
到期日期.Value = ""
UserForm7.学员电话.Enabled = True
UserForm7.姓名.Enabled = True
UserForm7.性别.Enabled = True
UserForm7.区域.Enabled = True
UserForm7.店名.Enabled = True
UserForm7.卡项名称.Enabled = True
UserForm7.卡号.Enabled = True
UserForm7.老板电话.Enabled = True
UserForm7.激活日期.Enabled = True
UserForm7.到期日期.Enabled = True
MsgBox "删除学员成功!!!", 48, "温馨提示……"
End If
'学员电话.Text = ""
'姓名.Text = ""
'性别.Text = ""
'区域.Text = ""
'店名.Text = ""
'卡项名称.Text = ""
'卡号.Text = ""
'老板电话.Text = ""
'激活日期.Text = ""
'到期日期.Text = ""
End With
End Sub


2.PNG

TA的精华主题

TA的得分主题

发表于 2020-3-12 10:24 来自手机 | 显示全部楼层
想看哪个表,就在适当位置再激活它。不过这样不断激活会导致工作表不停切换。

不想这样就
方法一:利用
WITH  表名'cells前加.

..........
end with
方法二:cells前逐个加工作表名。

评分

1

查看全部评分

TA的精华主题

TA的得分主题

 楼主| 发表于 2020-3-12 10:47 | 显示全部楼层
网海遨游 发表于 2020-3-12 10:24
想看哪个表,就在适当位置再激活它。不过这样不断激活会导致工作表不停切换。

不想这样就

可以了,谢谢老师
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-4-27 10:05 , Processed in 0.045335 second(s), 13 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表