Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 2891|回复: 6

[已解决]随机提取连续行数据

[复制链接]
发表于 2014-5-22 06:14 | 显示全部楼层 |阅读模式
提取连续行数据
最佳答案
2014-5-22 09:41
请看附件。

800.rar

1.99 KB, 下载次数: 14

发表于 2014-5-22 08:45 | 显示全部楼层
本帖最后由 wp8680 于 2014-5-22 08:48 编辑
  1. <blockquote>Sub tq()
复制代码
回复

使用道具 举报

发表于 2014-5-22 08:48 | 显示全部楼层
  1. Sub tq()
  2.     Dim pat, ir%, il%, n%, arr
  3.     Application.DisplayAlerts = Fasle    '表示禁止显示提示和警告消息
  4.     pat = ThisWorkbook.Path
  5.     Workbooks.Open pat & "/800.txt"
  6.     ir = Range("a65536").End(xlUp).Row
  7.     il = Cells(1, Cells.Columns.Count).End(xlToLeft).Column
  8.     n = Int(Rnd() * (ir - 4)) + 1
  9.     arr = Range(Cells(n, il), Cells(n + 4, il)).Value
  10.     ActiveWorkbook.Close
  11.     Range("a2").Resize(4, UBound(arr, 2)) = arr
  12.     Range("A2:A5").Select
  13.     Selection.TextToColumns Destination:=Range("A2"), DataType:=xlFixedWidth, _
  14.             FieldInfo:=Array(Array(12, 1), Array(13, 1), Array(14, 1), Array(15, 1), Array(16, 1)) _
  15.             , TrailingMinusNumbers:=True
  16.     Application.DisplayAlerts = True    '表示显示提示和警告消息
  17. End Sub
复制代码

评分

参与人数 1 +6 收起 理由
qh8600 + 6 很给力!

查看全部评分

回复

使用道具 举报

发表于 2014-5-22 09:22 | 显示全部楼层
wp8680 发表于 2014-5-22 08:48

获取txt文件,要好好学学
回复

使用道具 举报

发表于 2014-5-22 09:40 | 显示全部楼层
  1. Sub ReadTxt()
  2.     fn = FreeFile
  3.     Dim arr()
  4.     Open ThisWorkbook.Path & "\800.txt" For Input As #fn
  5.     Do While Not EOF(fn)
  6.         n = n + 1
  7.         ReDim Preserve arr(n)
  8.         Line Input #fn, arr(n)
  9.         Debug.Print arr(n)
  10.     Loop
  11.     Close #fn
  12.     Dim brr(1 To 4, 1 To 5)
  13.     Randomize
  14.     myrand = Int(((n - 3) * Rnd) + 1)
  15.     For i = myrand To myrand + 3
  16.         k = k + 1: x = arr(i): y = Right(x, 4)
  17.         brr(k, 1) = Left(x, Len(x) - 5)
  18.         brr(k, 2) = Mid(y, 1, 1)
  19.         brr(k, 3) = Mid(y, 2, 1)
  20.         brr(k, 4) = Mid(y, 3, 1)
  21.         brr(k, 5) = Mid(y, 4, 1)
  22.     Next
  23.     [a14].Resize(4, 5) = brr
  24. End Sub
复制代码
回复

使用道具 举报

发表于 2014-5-22 09:41 | 显示全部楼层    本楼为最佳答案   
请看附件。

800.rar

10.25 KB, 下载次数: 11

回复

使用道具 举报

发表于 2014-5-22 10:46 | 显示全部楼层
高手不少嘛!
回复

使用道具 举报

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

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-4-18 17:23 , Processed in 0.334012 second(s), 14 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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