我有一个包含完整数据混合的excel文件(第1列,名称)。我想基于第一列(即Name)将数据拆分成同一工作簿中的多个工作表。我在VBA中找到了解决方案,但我希望这是VB脚本。请帮忙。提前致谢。
`Sub parse_data()
Dim lr As Long
Dim ws As Worksheet
Dim vcol, i As Integer
Dim icol As Long
Dim myarr As Variant
Dim title As String
Dim titlerow As Integer
vcol = 1
Set ws = Sheets("ZPC_STATS")
lr = ws.Cells(ws.Rows.Count, vcol).End(xlUp).Row
title = "A1:G1"
titlerow = ws.Range(title).Cells(1).Row
icol = ws.Columns.Count
ws.Cells(1, icol) = "Unique"
For i = 2 To lr
On Error Resume Next
If ws.Cells(i, vcol) <> "" And Application.WorksheetFunction.Match(ws.Cells(i, vcol), ws.Columns(icol), 0) = 0 Then
ws.Cells(ws.Rows.Count, icol).End(xlUp).Offset(1) = ws.Cells(i, vcol)
End If
Next
myarr = Application.WorksheetFunction.Transpose(ws.Columns(icol).SpecialCells(xlCellTypeConstants))
ws.Columns(icol).Clear
For i = 2 To UBound(myarr)
ws.Range(title).AutoFilter field:=vcol, Criteria1:=myarr(i) & ""
If Not Evaluate("=ISREF('" & myarr(i) & "'!A1)") Then
Sheets.Add(after:=Worksheets(Worksheets.Count)).Name = myarr(i) & ""
Else
Sheets(myarr(i) & "").Move after:=Worksheets(Worksheets.Count)
End If
ws.Range("A" & titlerow & ":A" & lr).EntireRow.Copy Sheets(myarr(i) & "").Range("A1")
Sheets(myarr(i) & "").Columns.AutoFit
Next
ws.AutoFilterMode = False
ws.Activate
End Sub`
答案 0 :(得分:0)
您可以在VBS中重写整个代码。但是,如果仅用于调用Excel宏&#34;不可见&#34;,则可以使用以下脚本。您将代码保存在Excel中,只需从VBScript中调用它:
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = false
objExcel.Application.Run "'C:\test1.xlsm'!module1.testSub"
objExcel.DisplayAlerts = False
objExcel.Application.Quit
Set objExcel = Nothing
我建议在测试一切正常后,将visible
- 属性设置为false
。请注意,此脚本关闭excel而不保存任何内容,因此您必须将save-command添加到VBA代码中。
<强>更新强>
如果你真的想把所有东西放到VBS中:
variant
类型。 application
对象的方法(例如evaluate
)这个片段可以给你一个想法:
option explicit
const xlDown = -4121
const xlUp = -4162
dim objExcel
Set objExcel = CreateObject("Excel.Application")
' objExcel.Visible = false
dim wb, ws
set wb = objExcel.Workbooks.open("C:\Test1.xlsm")
with wb
Set ws = .Sheets("Sheet1")
dim lastRow
lastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
' ...
dim newSheet
set newSheet = .Sheets.Add(, .Worksheets(.Worksheets.Count))
newSheet.name = myarr(i)
' ...
end with
' ...
wb.close true ' At the end, do not forget to save the work
set wb = nothing
Set objExcel = Nothing
重要:如果您的脚本失败了一半(在开发过程中将),请确保关闭Excel实例。如果有Microsoft Excel
个可疑实例正在运行,请检查任务管理器。此外,如果在工作结束时弹出SaveAs
- 对话框,则可能是先前的Excel实例未终止,现在文件以ReadOnly模式打开。无法添加On error goto CleanUp
来强制关闭Excel,请参阅https://stackoverflow.com/a/157785/7599798
答案 1 :(得分:0)
尝试一下。
在代码中,您可以看到四个可以使用的过滤器示例,我们在此宏中使用示例1,我在代码中注释了其他3个示例。
app