二维码
微世推网

扫一扫关注

当前位置: 首页 » 快闻头条 » 科技资讯 » 正文

05_随机提取指定行Excel表格VBA编程实例_代

放大字体  缩小字体 发布日期:2023-03-02 23:29:32    作者:高子琦    浏览次数:136
导读

Public addrow As Long'提取并删除Public rndrow2 As LongPublic minnum2 As LongPublic maxnum2 As LongPrivate Sub CommandButton清空结果_Click()addrow = 0With ThisWorkbook.Worksheets("提取结果"

Public addrow As Long

'提取并删除

Public rndrow2 As Long

Public minnum2 As Long

Public maxnum2 As Long

Private Sub CommandButton清空结果_Click()

addrow = 0

With ThisWorkbook.Worksheets("提取结果")

.UsedRange.ClearFormats

.UsedRange.ClearContents

End With

End Sub

Private Sub CommandButton提取1_Click()

With ThisWorkbook.Worksheets("操作界面")

'判断输入参数,并赋值到变量

Dim minnum As Long

Dim maxnum As Long

If .Cells(2, "C").Value <> "" And .Cells(5, "C").Value <> "" Then

minnum = .Cells(2, "C").Value

maxnum = .Cells(5, "C").Value

Else

MsgBox "参数未输入完全"

Exit Sub

End If

'生成随机提取得行

Dim rndrow As Long

rndrow = 生成随机整数(minnum, maxnum)

If rndrow > 0 Then '大于0就提取行

addrow = addrow + 1 '提取数据放置得行位置

ThisWorkbook.Worksheets("原数据").Rows(rndrow).Copy

ThisWorkbook.Worksheets("提取结果").Rows(addrow).PasteSpecial Paste:=xlPasteAll

End If

End With

'ThisWorkbook.Worksheets("提取结果").Activate

'ThisWorkbook.Worksheets("提取结果").Cells(addrow, 1).Select

End Sub

Public Function 生成随机整数(ByVal lrnd As Long, ByVal urnd As Long) As Long

'--------------------------------随机整数

生成随机整数 = Application.WorksheetFunction.RandBetween(lrnd, urnd)

End Function

Private Sub CommandButton提取2_Click()

With ThisWorkbook.Worksheets("操作界面")

'判断输入参数,并赋值到变量

'判断是否是首次提取

If addrow = 0 Then

If .Cells(2, "C").Value <> "" And .Cells(5, "C").Value <> "" Then

minnum2 = .Cells(2, "C").Value

maxnum2 = .Cells(5, "C").Value

Else

MsgBox "参数未输入完全"

Exit Sub

End If

'生成随机提取得行

rndrow2 = 生成随机整数(minnum2, maxnum2)

End If

If rndrow2 > 0 Then '大于0就提取行

addrow = addrow + 1 '提取数据放置得行位置

ThisWorkbook.Worksheets("原数据").Rows(rndrow2).Copy

ThisWorkbook.Worksheets("提取结果").Rows(addrow).PasteSpecial Paste:=xlPasteAll

ThisWorkbook.Worksheets("原数据").Rows(rndrow2).Delete

maxnum2 = maxnum2 - 1

rndrow2 = 生成随机整数(minnum2, maxnum2)

End If

End With

'ThisWorkbook.Worksheets("提取结果").Activate

'ThisWorkbook.Worksheets("提取结果").Cells(addrow, 1).Select

End Sub

 
(文/高子琦)
打赏
免责声明
• 
本文为高子琦原创作品•作者: 高子琦。欢迎转载,转载请注明原文出处:http://www.udxd.com/news/show-372141.html 。本文仅代表作者个人观点,本站未对其内容进行核实,请读者仅做参考,如若文中涉及有违公德、触犯法律的内容,一经发现,立即删除,作者需自行承担相应责任。涉及到版权或其他问题,请及时联系我们邮件:weilaitui@qq.com。
 

Copyright©2015-2023 粤公网安备 44030702000869号

粤ICP备16078936号

微信

关注
微信

微信二维码

WAP二维码

客服

联系
客服

联系客服:

24在线QQ: 770665880

客服电话: 020-82301567

E_mail邮箱: weilaitui@qq.com

微信公众号: weishitui

韩瑞 小英 张泽

工作时间:

周一至周五: 08:00 - 24:00

反馈

用户
反馈