圆月山庄资源网 Design By www.vgjia.com
在我们实际工作中经常遇到将工作表按某一表头字段分开的情况,我们一般的做法是先按指定表头排序然后分段复制粘贴出去,不但麻烦还很容易搞错。
下面的VBS脚本就是实现的工作表按指定表头(由用户选择)自动分表功能。需要的朋友只要将要操作的工作表拖放到脚本文件上即可轻松实现工作表分表(暂时只适用于xp系统):
复制代码 代码如下:
'拖动工作表至VBS脚本实现按指定表头自动分表
On Error Resume Next
If WScript.Arguments(0) = "" Then WScript.Quit
Dim objExcel, ExcelFile, MaxRows, MaxColumns, SHCount
ExcelFile = WScript.Arguments(0)
If LCase(Right(ExcelFile,4)) <> ".xls" And LCase(Right(ExcelFile,4)) <> ".xls" Then WScript.Quit
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = False
objExcel.Workbooks.Open ExcelFile
'获取工作表初始sheet总数
SHCount = objExcel.Sheets.Count
'获取工作表有效行列数
MaxRows = objExcel.ActiveSheet.UsedRange.Rows.Count
MaxColumns = objExcel.ActiveSheet.UsedRange.Columns.Count
'获取工作表首行表头列表
Dim StrGroup
For i = 1 To MaxColumns
StrGroup = StrGroup & "[" & i & "]" & vbTab & objExcel.Cells(1, i).Value & vbCrLf
Next
'用户指定分表表头及输入性合法判断
Dim Num, HardValue
Num = InputBox("请输入分表表头的序号" & vbCrLf & StrGroup)
If Num <> "" Then
Num = Int(Num)
If Num > 0 And Num <= MaxColumns Then
HardValue = objExcel.Cells(1, Num).Value
Else
objExcel.Quit
Set objExcel = Nothing
WScript.Quit
End If
Else
objExcel.Quit
Set objExcel = Nothing
WScript.Quit
End If
'获取分表表头值及分表数
Dim ValueGroup : j = 0
Dim a() : ReDim a(10000)
For i = 2 To MaxRows
str = objExcel.Cells(i, Num).Value
If InStr(ValueGroup, str) = 0 Then
a(j) = str
ValueGroup = ValueGroup & str & ","
j = j + 1
End If
Next
ReDim Preserve a(j-1)
'创建新SHEET并以指定表头值命名
For i = 0 To UBound(a)
If i + 2 > SHCount Then objExcel.Sheets.Add ,objExcel.Sheets("sheet" & i + 1),1,-4167
Next
For i = 0 To UBound(a)
objExcel.Sheets("sheet" & i + 2).Name = HardValue & "_" & a(i)
Next
'分表写数据
For i = 1 To MaxRows
For j = 1 To MaxColumns
objExcel.sheets(1).Select
str = objExcel.Cells(i,j).Value
If i = 1 Then
For k = 0 To UBound(a)
objExcel.sheets(HardValue & "_" & a(k)).Select
objExcel.Cells(i,j).Value = str
objExcel.Cells(1, MaxColumns + 1).Value = 1
Next
Else
objExcel.sheets(HardValue & "_" & objExcel.Cells(i,Num).Value).Select
If j = 1 Then x = objExcel.Cells(1, MaxColumns + 1).Value + 1
objExcel.Cells(x ,j).Value = str
If j = MaxColumns Then objExcel.Cells(1, MaxColumns + 1).Value = x
End If
Next
Next
For i = 0 To UBound(a)
objExcel.sheets(HardValue & "_" & a(i)).Select
objExcel.Cells(1, MaxColumns + 1).Value = ""
Next
objExcel.ActiveWorkbook.Save
objExcel.Quit
Set objExcel = Nothing
WScript.Echo "提示:对" & ExcelFile & "的分表操作完成"
下面的VBS脚本就是实现的工作表按指定表头(由用户选择)自动分表功能。需要的朋友只要将要操作的工作表拖放到脚本文件上即可轻松实现工作表分表(暂时只适用于xp系统):
复制代码 代码如下:
'拖动工作表至VBS脚本实现按指定表头自动分表
On Error Resume Next
If WScript.Arguments(0) = "" Then WScript.Quit
Dim objExcel, ExcelFile, MaxRows, MaxColumns, SHCount
ExcelFile = WScript.Arguments(0)
If LCase(Right(ExcelFile,4)) <> ".xls" And LCase(Right(ExcelFile,4)) <> ".xls" Then WScript.Quit
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = False
objExcel.Workbooks.Open ExcelFile
'获取工作表初始sheet总数
SHCount = objExcel.Sheets.Count
'获取工作表有效行列数
MaxRows = objExcel.ActiveSheet.UsedRange.Rows.Count
MaxColumns = objExcel.ActiveSheet.UsedRange.Columns.Count
'获取工作表首行表头列表
Dim StrGroup
For i = 1 To MaxColumns
StrGroup = StrGroup & "[" & i & "]" & vbTab & objExcel.Cells(1, i).Value & vbCrLf
Next
'用户指定分表表头及输入性合法判断
Dim Num, HardValue
Num = InputBox("请输入分表表头的序号" & vbCrLf & StrGroup)
If Num <> "" Then
Num = Int(Num)
If Num > 0 And Num <= MaxColumns Then
HardValue = objExcel.Cells(1, Num).Value
Else
objExcel.Quit
Set objExcel = Nothing
WScript.Quit
End If
Else
objExcel.Quit
Set objExcel = Nothing
WScript.Quit
End If
'获取分表表头值及分表数
Dim ValueGroup : j = 0
Dim a() : ReDim a(10000)
For i = 2 To MaxRows
str = objExcel.Cells(i, Num).Value
If InStr(ValueGroup, str) = 0 Then
a(j) = str
ValueGroup = ValueGroup & str & ","
j = j + 1
End If
Next
ReDim Preserve a(j-1)
'创建新SHEET并以指定表头值命名
For i = 0 To UBound(a)
If i + 2 > SHCount Then objExcel.Sheets.Add ,objExcel.Sheets("sheet" & i + 1),1,-4167
Next
For i = 0 To UBound(a)
objExcel.Sheets("sheet" & i + 2).Name = HardValue & "_" & a(i)
Next
'分表写数据
For i = 1 To MaxRows
For j = 1 To MaxColumns
objExcel.sheets(1).Select
str = objExcel.Cells(i,j).Value
If i = 1 Then
For k = 0 To UBound(a)
objExcel.sheets(HardValue & "_" & a(k)).Select
objExcel.Cells(i,j).Value = str
objExcel.Cells(1, MaxColumns + 1).Value = 1
Next
Else
objExcel.sheets(HardValue & "_" & objExcel.Cells(i,Num).Value).Select
If j = 1 Then x = objExcel.Cells(1, MaxColumns + 1).Value + 1
objExcel.Cells(x ,j).Value = str
If j = MaxColumns Then objExcel.Cells(1, MaxColumns + 1).Value = x
End If
Next
Next
For i = 0 To UBound(a)
objExcel.sheets(HardValue & "_" & a(i)).Select
objExcel.Cells(1, MaxColumns + 1).Value = ""
Next
objExcel.ActiveWorkbook.Save
objExcel.Quit
Set objExcel = Nothing
WScript.Echo "提示:对" & ExcelFile & "的分表操作完成"
标签:
工作表,自动分表
圆月山庄资源网 Design By www.vgjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
圆月山庄资源网 Design By www.vgjia.com
暂无评论...
更新日志
2024年11月01日
2024年11月01日
- 外媒:《死亡岛2》或是今年PS+2档最受欢迎游戏
- 群星.1997-世纪之歌第二辑6CD【宝丽金】【WAV+CUE】
- 邵萱.1997-是是非非【捷登】【WAV+CUE】
- 巫启贤.1998-我是你的【风格】【WAV+CUE】
- 【原神手游】「月草的赐慧」祈愿
- 【原神手游】「赤团开时」祈愿
- 【原神手游】「法器·千夜浮梦」介绍
- 陈立农《青春为名 上部曲 - 恋》[FLAC/分轨][290.58MB]
- 张乔西《明星》[320K/MP3][55.23MB]
- 张乔西《明星》[FLAC/分轨][143.08MB]
- 《P3R:Episode Aegis》:重复爬塔的悲伤
- 《公会传说:遗落的世界》EA版评测:怀旧感CRPG还在追我
- 诛仙老玩家会喜欢《诛仙世界》吗?
- ABC唱片-《母帶直刻神奇黑胶CD》[FLAC+CUE]
- 柏菲·李一凤《真爱过关》限量开盘母带ORMCD[低速原抓WAV+CUE]