ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

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

[求助] 从一列中不重复地随机提取文本,文本有相同的,但所指内容不同。(难,见文件可知)

[复制链接]

TA的精华主题

TA的得分主题

 楼主| 发表于 2019-8-2 10:15 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
一把小刀闯天下 发表于 2019-8-2 09:05
'a:c20,h8,b10,a2   -->输出A工作表,20个C类题,8个H类题,,,合计必须为40

Option Explicit

谢谢大师,佩服。想问一下,能在答案前把分类号中上去吗。

TA的精华主题

TA的得分主题

发表于 2019-8-2 12:44 | 显示全部楼层
zjjEH 发表于 2019-8-2 10:15
谢谢大师,佩服。想问一下,能在答案前把分类号中上去吗。

为了测试用的,忘了去除了

arr(m, 1) = data(a, 1) & data(a, 2)

改成:
arr(m, 1) = data(a, 2)

TA的精华主题

TA的得分主题

发表于 2019-8-2 14:16 | 显示全部楼层
  1. Option Explicit

  2. Sub Test()
  3.     Dim strType As String, lngCount As Long
  4.     Dim strConditions As String
  5.     Dim arrProblens As Variant, arrAnswer As Variant
  6.    
  7.     '在A表中,提取类型为A的10道题
  8.     strType = "A": lngCount = 20
  9.     If GetProblemsByOneType(strType, lngCount, arrProblens, arrAnswer) = False Then
  10.         Exit Sub
  11.     Else
  12.         Sheet2.Range("E5").Resize(lngCount, 1) = arrProblens
  13.         Sheet2.Range("I5").Resize(lngCount, 1) = arrAnswer
  14.     End If
  15.    
  16.     '在C表中,提取33道题,其中A型10道,B型11道,C型12道,
  17.     strConditions = "A,10;B,11;C,12"
  18.     If GetProblemsByAnyType(strConditions, arrProblens, arrAnswer) = False Then
  19.         Exit Sub
  20.     Else
  21.         Sheet4.Range("E5").Resize(33, 1) = arrProblens
  22.         Sheet4.Range("I5").Resize(33, 1) = arrAnswer
  23.     End If

  24. End Sub

  25. '根据指定的类型和数量,从题库中返回试题
  26. 'strConditions 类型和数量参数,格式为  类型1,数量1;类型2,数量2;类型3,数量3;……类型N,数量N
  27. 'GetProblemsByAnyType 成功返回True, 失败返回 False
  28. 'arrProblens 成功,返回试题
  29. 'arrAnswer 成功,返回答案
  30. Function GetProblemsByAnyType(ByVal strConditions As String, ByRef arrProblens As Variant, ByRef arrAnswer As Variant) As Boolean
  31.     Dim arrCondtions As Variant, strSplit() As String, strTemp() As String, strAnswer As String
  32.     Dim lngID As Long, strType As String, lngCount As Long, lngSum As Long
  33.     Dim arrP As Variant, arrA As Variant, lngCur As Long, lngRow As Long
  34.    
  35.     strConditions = Trim(strConditions)
  36.     If strConditions = "" Then
  37.         MsgBox "参数输入有误!"
  38.         GetProblemsByAnyType = False
  39.         Exit Function
  40.     End If
  41.    
  42.     strSplit = Split(strConditions, ";")
  43.     ReDim arrCondtions(1 To UBound(strSplit) + 1, 1 To 2)
  44.    
  45.     For lngID = LBound(strSplit) To UBound(strSplit)
  46.         strTemp = Split(strSplit(lngID), ",")
  47.         strType = Trim(strTemp(0))
  48.         lngCount = Val(strTemp(1))
  49.         If strType = "" Or lngCount = 0 Then
  50.             MsgBox "参数输入有误!"
  51.             GetProblemsByAnyType = False
  52.             Exit Function
  53.         End If
  54.         arrCondtions(lngID + 1, 1) = strType
  55.         arrCondtions(lngID + 1, 2) = lngCount
  56.         lngSum = lngSum + lngCount
  57.     Next
  58.    
  59.     ReDim arrProblens(1 To lngSum, 1 To 1)
  60.     ReDim arrAnswer(1 To lngSum, 1 To 1)
  61.     lngCur = 0
  62.    
  63.     For lngID = LBound(arrCondtions) To UBound(arrCondtions)
  64.         strType = arrCondtions(lngID, 1)
  65.         lngCount = arrCondtions(lngID, 2)
  66.         If GetProblemsByOneType(strType, lngCount, arrP, arrA) = False Then
  67.             GetProblemsByAnyType = False
  68.             Exit Function
  69.         Else
  70.             For lngRow = LBound(arrP) To UBound(arrP)
  71.                 arrProblens(lngRow + lngCur, 1) = arrP(lngRow, 1)
  72.                 arrAnswer(lngRow + lngCur, 1) = arrA(lngRow, 1)
  73.             Next
  74.             lngCur = lngCur + lngRow - 1
  75.         End If
  76.     Next
  77.    
  78.     '乱序
  79.     For lngID = LBound(arrProblens) To UBound(arrProblens)
  80.         lngCur = Int(Rnd * lngSum + 1)
  81.         strType = arrProblens(lngCur, 1)
  82.         strAnswer = arrAnswer(lngCur, 1)
  83.         arrProblens(lngCur, 1) = arrProblens(lngID, 1)
  84.         arrAnswer(lngCur, 1) = arrAnswer(lngID, 1)
  85.         arrProblens(lngID, 1) = strType
  86.         arrAnswer(lngID, 1) = strAnswer
  87.     Next
  88.    
  89.     GetProblemsByAnyType = True
  90. End Function


  91. '根据指定的单一类型和数量,从题库中返回试题
  92. 'strType 类型
  93. 'lngCount 数量
  94. 'GetProblemsByOneType 成功返回True, 失败返回 False
  95. 'arrProblens 成功,返回试题
  96. 'arrAnswer 成功,返回答案
  97. Function GetProblemsByOneType(ByVal strType As String, ByVal lngCount As Long, ByRef arrProblens As Variant, ByRef arrAnswer As Variant) As Boolean
  98.     Dim sh As Worksheet, arrData As Variant, lngRow As Long
  99.     Dim arrType As Variant, lngID As Long
  100.     Dim objDIc As Object, strKey As String, lngRND As Long
  101.     Dim arrKeys As Variant, arrItems As Variant
  102.    
  103.     If lngCount < 1 Then
  104.         MsgBox "抽取数量有误!"
  105.         GetProblemsByOneType = False
  106.         Exit Function
  107.     End If
  108.    
  109.     strType = Trim(strType)
  110.     If strType = "" Then
  111.         MsgBox "类型输入有误!"
  112.         GetProblemsByOneType = False
  113.         Exit Function
  114.     End If
  115.    
  116.     Set sh = Sheets("题库")
  117.     arrData = sh.UsedRange
  118.     Set objDIc = CreateObject("scripting.dictionary")
  119.     ReDim arrType(1 To UBound(arrData), 1 To 2)
  120.     lngID = 0
  121.    
  122.     For lngRow = LBound(arrData) To UBound(arrData)
  123.         If Trim(arrData(lngRow, 1)) Like strType & "*" Then
  124.             lngID = lngID + 1
  125.             arrType(lngID, 1) = arrData(lngRow, 2)
  126.             arrType(lngID, 2) = arrData(lngRow, 3)
  127.             strKey = Trim(arrData(lngRow, 3))
  128.             objDIc(strKey) = ""
  129.         End If
  130.     Next
  131.    
  132.     If lngCount > objDIc.Count Then
  133.         MsgBox "【" & strType & "】型题可抽题目不足!"
  134.         GetProblemsByOneType = False
  135.         Exit Function
  136.     End If
  137.    
  138.     ReDim arrProblens(1 To lngCount, 1 To 1)
  139.     ReDim arrAnswer(1 To lngCount, 1 To 1)
  140.     objDIc.RemoveAll
  141.    
  142.     Do Until objDIc.Count = lngCount
  143.         lngRND = Int(Rnd * lngID + 1)
  144.         strKey = arrType(lngRND, 2)
  145.         objDIc(strKey) = arrType(lngRND, 1)
  146.     Loop
  147.    
  148.     arrKeys = objDIc.keys
  149.     arrItems = objDIc.items
  150.    
  151.     Set objDIc = Nothing
  152.    
  153.     For lngID = LBound(arrKeys) To UBound(arrKeys)
  154.         arrProblens(lngID + 1, 1) = arrItems(lngID)
  155.         arrAnswer(lngID + 1, 1) = arrKeys(lngID)
  156.     Next
  157.     GetProblemsByOneType = True
  158. End Function
复制代码

TA的精华主题

TA的得分主题

发表于 2019-8-2 14:18 | 显示全部楼层
给你做成模块化,代码审核中,见附件哦
母版题库.rar (100.79 KB, 下载次数: 9)
傲游截图20190802141714.png

TA的精华主题

TA的得分主题

 楼主| 发表于 2023-12-25 08:18 | 显示全部楼层
一把小刀闯天下 发表于 2019-8-1 16:04
A就这么一点点数据?如果条件确定,,,


请前辈指教!不甚感谢!
image.png

工作簿11.rar

14.17 KB, 下载次数: 4

您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

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

GMT+8, 2024-6-26 17:12 , Processed in 0.037381 second(s), 9 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

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

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

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