- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一本主工作簿和一些简单地称为Master
的子项和Child 1
、Child 2
和Child 3
.数据填充到Master
中,需要进行排序、复制并粘贴到相关的子表中。所有子工作簿的目标都是桌面,所需的过滤只是第一列中所需工作簿的名称(也与每个工作簿的名称匹配)。
我尝试使用下面的代码来完成此任务,这是我从几个地方收集到的代码,但没有成功。我认为,由于我缺乏知识,我只是把坑挖得更深,代码开始变得非常冗长:
Private Sub CommandButton21_Click()
Dim My_Range As Range
Dim DestSh As Worksheet
Dim CalcMode As Long
Dim ViewMode As Long
Dim FilterCriteria As String
Dim CCount As Long
Dim rng As Range
Dim strActiveSheet As String
Dim varCellvalue As String
Dim fpath As String
Dim owb As Workbook
varCellvalue = Range("A2").Value
fpath = "C:\Users\User\Desktop\Templates\" & varCellvalue & "".xlsm"
strActiveSheet = ActiveSheet.Name
Set My_Range = Range("A1:U" & LastRow(ActiveSheet))
My_Range.Parent.Select
With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
.EnableEvents = False
End With
ViewMode = ActiveWindow.View
ActiveWindow.View = xlNormalView
ActiveSheet.DisplayPageBreaks = False
My_Range.Parent.AutoFilterMode = False
My_Range.AutoFilter Field:=1, Criteria1:="=User 1"
Set owb = Application.Workbooks.Open(fpath)
Set DestSh = Workbooks(" & varCellvalue & ").Sheets("Work")
CCount = 0
On Error Resume Next
CCount = My_Range.Columns(1).SpecialCells(xlCellTypeVisible).Areas(1).Cells.Count
On Error GoTo 0
If CCount = 0 Then
MsgBox "There are more than 8192 areas:" _
& vbNewLine & "It is not possible to copy the visible data." _
& vbNewLine & "Tip: Sort your data before you use this macro.", _
vbOKOnly, "Copy to worksheet"
Else
With My_Range.Parent.AutoFilter.Range
On Error Resume Next
Set rng = .Offset(1, 0).Resize(.Rows.Count - 1, .Columns.Count) _
.SpecialCells(xlCellTypeVisible)
On Error GoTo 0
If Not rng Is Nothing Then
rng.Copy
With DestSh.Range("A" & LastRow(DestSh) + 1)
.PasteSpecial Paste:=8
.PasteSpecial xlPasteValues
.PasteSpecial xlPasteFormats
Application.CutCopyMode = False
End With
rng.EntireRow.Delete
End If
End With
End If
My_Range.Parent.AutoFilterMode = False
'Restore ScreenUpdating, Calculation, EnableEvents, ....
ActiveWindow.View = ViewMode
Application.Goto DestSh.Range("A1")
With Application
.Calculation = xlCalculationAuto
.ScreenUpdating = True
.EnableEvents = True
.Calculation = CalcMode
.Calculation = xlCalculationAutomatic
End With
Worksheets(strActiveSheet).Activate
End Sub
Function LastRow(sh As Worksheet)
On Error Resume Next
LastRow = sh.Cells.Find(What:="*", _
After:=sh.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlValues, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
On Error GoTo 0
End Function
示例数据:
Workbook Requested by ID Date Raised
-------------- --------------- -----------
Child 1 Ben 10000586 01/01/2015
Child 2 John 10000587 02/02/2015
Child 1 Jack 10000588 03/03/2015
Child 2 Percy 10000589 04/04/2015
Child 1 Jill 10000590 05/05/2015
Child 3 George 10000591 06/06/2015
最佳答案
这有点通用 - 它会识别 A 列中的任何名称
总结:
迭代所有项目时
清理并恢复所有设置
Option Explicit
Public Sub splitMaster()
Dim ws As Worksheet, ur As Range, lr As Long, lc As Long, cel1 As Range
Dim itms As Variant, itm As Variant, thisPath As String, newWs As Worksheet
If ws Is Nothing Then Set ws = ThisWorkbook.ActiveSheet
Set ur = ws.UsedRange
'if UsedRange contains more than 1 row
If ur.Row + ur.Rows.Count > 2 Then
thisPath = ThisWorkbook.Path & "\" 'get path of current file
enableXl False 'disables ScreenUpdating, Events, and Alerts
itms = getDistinct(ws, 1) 'removes duplicates and sorts items (col 1)
'determine last row and column on current sheet, based on UsedRange
lr = ws.Cells(ur.Row + ur.Rows.Count + 1, ur.Column).End(xlUp).Row
lc = ws.Cells(ur.Row, ur.Column + ur.Columns.Count + 1).End(xlToLeft).Column
'turn on Autofilter if it's off
If ws.AutoFilter Is Nothing Then ur.AutoFilter
Set newWs = getNewSheet 'creates a new Workbook with a single sheet
For Each itm In itms 'for each item in column 1 (names)
'AutoFilter UsedRange based on (exact) value of itm
ur.Columns(1).AutoFilter Field:=1, Criteria1:=itm 'or: "*" & itm & "*"
'if there are any visible rows besides the header, continue
If ur.SpecialCells(xlCellTypeVisible).Count > lc Then
ur.Copy 'copy visible range (implied)
Set cel1 = newWs.Cells(ur.Row, ur.Column) 'cell to copy to
'(this is in new Workbook.Worksheet)
cel1.PasteSpecial xlPasteColumnWidths 'get column widths
cel1.PasteSpecial xlPasteAll 'get vals, formulas, cell & font formats
cel1.Select 'save file with 1st cell selected (instead of paste area)
newWs.Name = itm 'rename the sheet in the new file to current item
newWs.Parent.SaveAs thisPath & itm 'save the file
'delete all data, to prepare the sheet for the next iteration
newWs.UsedRange.Columns(ur.Column).EntireRow.Delete
End If
Next
newWs.Parent.Close False 'close the new file
'(which was re-used to save several previous children)
ur.AutoFilter 'remove the AutoFilter on initial file
'go to the first cell in initial file, after and copy operations
Application.Goto ur.Cells(ur.Row, ur.Column)
enableXl True 'enables ScreenUpdating, Events, and Alerts
ThisWorkbook.Saved = True 'there were no changes made to initial file
'(to skip "Save Changes" confirmation)
End If
End Sub
Public Sub enableXl(ByVal opt As Boolean) 'turns 3 Excel settings on\off
Application.ScreenUpdating = opt
Application.EnableEvents = opt
Application.DisplayAlerts = opt
End Sub
Public Function getNewSheet() As Worksheet
Dim wb As Workbook, totalNewSheets As Long
totalNewSheets = Application.SheetsInNewWorkbook 'remember current Excel setting
Application.SheetsInNewWorkbook = 1 'change setting to 1 sheet
Set wb = Application.Workbooks.Add 'create the new file
Application.SheetsInNewWorkbook = totalNewSheets 'restore initial setting
Set getNewSheet = wb.Worksheets(1) 'return new sheet to calling sub
End Function
'Returns a 2D array (rng) of unique values extracted from colID, sorted a-z
Public Function getDistinct(Optional ByRef ws As Worksheet = Nothing, _
Optional ByVal colID As Long = 0) As Variant
Dim lr As Long, lc As Long, ur As Range, tmp As Range
'if the optional parameter (sheet) was not provided, use the active sheet
If ws Is Nothing Then Set ws = ThisWorkbook.ActiveSheet
Set ur = ws.UsedRange
'if optional column # parameter was not provided, use the 1st column in used range
If colID < ur.Column And colID > ur.Columns.Count Then colID = ur.Column
'determine last row and last column un UsedRange
lr = ws.Cells(ur.Row + ur.Rows.Count + 1, ur.Column).End(xlUp).Row
lc = ws.Cells(ur.Row, ur.Column + ur.Columns.Count + 1).End(xlToLeft).Column
'set the temporary rng variable to the 1st empty column on current sheet
Set tmp = ws.Range(ws.Cells(ur.Row, lc + 1), ws.Cells(lr, lc + 1))
If tmp.Count > 1 Then 'if data to be processed contains more than 1 item continue
'set first cell in the new col to get the (trimmed) value from processed col
With tmp.Cells(1, 1)
.Formula = "=Trim(" & ws.Cells(ur.Row, colID).Address(False) & ")"
'copy the formula down to the last row
.AutoFill Destination:=tmp
End With
'convert formulas to values
tmp.Value2 = tmp.Value2
'remove duplicates in the new column only
tmp.RemoveDuplicates Columns:=1, Header:=xlNo
'reset the last row
lr = ws.Cells(ur.Row + ur.Rows.Count + 1, lc + 1).End(xlUp).Row
'setup the sort (new column only)
With ws.Sort
'sort object belongs to the sheet, but sorted field is our new column
.SortFields.Add Key:=ws.Cells(lr + 1, lc + 1), Order:=xlAscending
'the actual sorted range is also our new column
.SetRange tmp
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.Apply
End With
'reset the tmp variable to contain only the distinct (and sorted) values
Set tmp = ws.Range(ws.Cells(ur.Row, lc + 1), ws.Cells(lr, lc + 1))
End If
'return the new items
getDistinct = tmp 'VBA does not exit the function with this assignment
'remove the temporary column
tmp.Cells(1, 1).EntireColumn.Delete
End Function
'--------------------------------------------------------------------------------------
<小时/>
它将所有子文件保存在与主文件相同的位置
关于vba - 自动筛选(或循环)并根据单元格值复制到另一个工作簿,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32697946/
我正在研究这个领域以获得实时对象检测。 视频示例: http://www.youtube.com/watch?v=Bm5qUG-06V8 http://www.youtube.com/watch?v=
问题 我正在为 C 语言的项目寻找一种数据结构来存储列表列表。我需要能够访问仅给定 n 的第 n 个列表(这些术语将被乱序访问)。各个列表将包含 1 到 M 之间的整数(为了具体起见,假设 M = 2
我有一个List (Maybe a),我想过滤出Nothing的实例。我大概已经做到了,但是对所需的代码量却不满意: removeNothingFromList : List (Maybe a) ->
我对 Blazor 有基本的输入 当我输入我认为无效的字符(例如“/”或“:/\\""|?* ]"; Search = Regex.Replace(args.Value.ToSt
我有一个主应用程序,允许用户编辑所有数据(大约 20 个字段)。更新后,我将其添加到服务总线主题中,系统的其他区域也订阅了该主题。 其中一个订阅仅关心单个字段(电话号码)是否更新。我想知道处理这个问题
我是ElasticSearch的新手,需要帮助解决以下问题: 我有一组包含多个产品的文档。我想通过“Apple”过滤产品属性product_brand并获取与过滤器匹配的产品数量。但是,结果应按文档I
我想过滤查询集中特定范围内的项目。这就是我的模型的样子 class modelEmployee(models.Model): user = models.ForeignKey(User, on
尽管数组 a 的大小不断增加,但我无法找到 8 位素数。它适用于较小的数字: #include #include int main() { int n,a[100000],i,m,k;
我不希望能够使用代码隐藏来搜索我的 gridview 结果。 我有一个按钮和一个 GridView :
我想使用 sift/surf 进行模板匹配。图像可以有 1...n 个目标。使用 surf/sift 只能提取一个目标。一种想法是将图像分割成多个片段,然后寻找筛选/冲浪匹配。它有效,但显然由于速度和
这是使用 Xodus API 限制实体查询的方法: final EntityIterable allUsers = txn.getAll(storeName).skip(skip).take(limi
我有 2 个 excel 文件:IDList.csv 和 Database.csv。 IDList 包含我要从数据库中过滤掉的 300 个 ID 号的列表,其中包含 2000 个条目(在数据库中留下
过滤 Treeview 节点的最佳/有效方法是什么? 例如:我输入“abc”,只有包含“abc”的节点可见。然后我输入 “abcd”,我应该会看到唯一包含 “abcd” 文本的节点。依此类推,所以每次
我有两个关于报告的 tablix,以及一个用于向 tablix 提供数据的数据集。我在报告中有一个多选参数,我需要根据该参数中的值过滤结果。 有什么区别,如果有,什么是更好的解决方案: 直接在数据集或
我对 flex 搜索/ NEST还是很陌生,需要一些帮助才能查询/过滤我的数据。 我有一个产品 list 。而且这些产品可以具有任意数量的选项,并具有与之相关的值。而且我需要能够按选项名称及其值来过滤
我正在使用过滤条件从原始表创建一个表,其中我的过滤值来自 SELECTEDVALUE 表格没有根据 SELECTEDVALUE 进行过滤,如果我用一个真实的值替换它就可以了。 代码(不起作用) Tra
我正在设置一个 MSBuild 项目来运行一些 NUnit 测试,使用 MSBuild Community Tasks Project 。 通过这些设置,我将能够运行 NUnit 测试:
我正在使用过滤条件从原始表创建一个表,其中我的过滤值来自 SELECTEDVALUE 表格没有根据 SELECTEDVALUE 进行过滤,如果我用一个真实的值替换它就可以了。 代码(不起作用) Tra
例如,我们有一个列表,我们想用特定的谓词将其分成两部分。 首先,我们可以使用filter和filterNot。 val trueList = list.filter(predicate) val fa
我尝试为 [Start] 使用某种范围: var calendar = outlookApplication.GetNamespace("MAPI").GetDefaultFolder(OlDefau
我是一名优秀的程序员,十分优秀!