- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
对于以下excel数据:
1 Name Date Color_picked
2 John 8/1/2015 Red
3 Jason 8/13/2015 Blue
4 Kevin 8/12/2015 Yellow
5 Derek 8/13/2015 Blue
6 Cherry 8/1/2015 Red
我要执行以下操作:
1) 为每一行生成一个随机数(不包括标题行)
2) 根据颜色(红色、蓝色和黄色标签)将所有记录复制到新标签/工作表中
3) 在每个新标签(红色、蓝色和黄色标签)中,首先按日期对记录进行排序,如果重复日期,然后按随机数排序。
这是我目前所拥有的:
Sub myFoo()
Application.CutCopyMode = False
On Error GoTo Err_Execute
Sheet1.Range("B1:F3").Copy
Red.Range("A1").Rows("1:1").Insert Shift:=xlDown
Err_Execute:
If Err.Number = 0 Then MsgBox "Transformation Done!" Else _
MsgBox Err.Description
End Sub
我应该先创建副本还是先排序?
最佳答案
这应该可以解决问题:
Sub test_Ryan_Fung()
Dim WsSrc As Worksheet, _
WsRed As Worksheet, _
WsBlue As Worksheet, _
WsYellow As Worksheet, _
Ws As Worksheet, _
DateFilterRange As String, _
RandomRange As String, _
TotalRange As String, _
LastRow As Long, _
WriteRow As Long, _
ShArr(), _
Arr()
Set WsSrc = Sheet1
Set WsRed = Sheets("Red")
Set WsBlue = Sheets("Blue")
Set WsYellow = Sheets("Yellow")
ReDim ShArr(1 To 3)
Set ShArr(1) = WsRed: Set ShArr(2) = WsBlue: Set ShArr(3) = WsYellow
Application.CutCopyMode = False
On Error GoTo Err_Execute
With WsSrc
LastRow = .Range("A" & .Rows.Count).End(xlUp).Row
For i = 2 To LastRow
.Cells(i, 5) = Application.WorksheetFunction.RandBetween(1, 10000)
Next i
Arr = .Range("A2:E" & LastRow).Value
End With
For i = LBound(Arr, 1) To UBound(Arr, 1)
Select Case LCase(Arr(i, 4))
Case Is = "red"
With WsRed
WriteRow = .Range("A" & .Rows.Count).End(xlUp).Row + 1
For j = LBound(Arr, 2) To UBound(Arr, 2)
.Cells(WriteRow, j) = Arr(i, j)
Next j
End With
Case Is = "blue"
With WsBlue
WriteRow = .Range("A" & .Rows.Count).End(xlUp).Row + 1
For j = LBound(Arr, 2) To UBound(Arr, 2)
.Cells(WriteRow, j) = Arr(i, j)
Next j
End With
Case Is = "yellow"
With WsYellow
WriteRow = .Range("A" & .Rows.Count).End(xlUp).Row + 1
For j = LBound(Arr, 2) To UBound(Arr, 2)
.Cells(WriteRow, j) = Arr(i, j)
Next j
End With
Case Else
MsgBox "Color not recognised : " & Arr(i, 4), vbCritical + vbOKOnly
End Select
Next i
For i = LBound(ShArr, 1) To UBound(ShArr, 1)
Set Ws = ShArr(i)
With Ws
LastRow = .Range("A" & .Rows.Count).End(xlUp).Row
DateFilterRange = "C2:C" & LastRow
RandomRange = "E2:E" & LastRow
TotalRange = "A1:E" & LastRow
With .Sort
With .SortFields
.Clear
.Add Key:=Range(DateFilterRange), _
SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
.Add Key:=Range(RandomRange), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
End With
.SetRange Range(TotalRange)
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End With
Next i
Err_Execute:
If Err.Number = 0 Then
MsgBox "Transformation Done!"
Else
MsgBox Err.Description
End If
End Sub
关于vba - Excel 宏 : Copy data into new worksheet and sort base on date and random number,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33822153/
我知道使用对象变量是良好的编码习惯。我正在尝试创建一个名为 wsc 的对象变量,该变量将使用对事件工作簿的工作表集合的对象引用进行初始化。请参阅下面的代码。 我尝试将对象变量数据类型设置为工作表,但它
我在遍历 for 循环中的一系列项目时遇到问题: 该程序应该如何工作- 我从工作表 1(“跟踪电子表格”)开始,根据工作表 1 的变化(在单元格中选择"is"),它会将您转移到工作表 2(“延期提交”
我现在在 Excel 中使用宏仅大约 4 个月,基本上是通过查找现有代码并弄清楚其工作原理来自学。我现在有点陷入困境。 我的 Excel 工作簿中有一份报告。我需要根据 D 列中出现的数据跨多个工作表
工作簿workbook = this.getWorkBook(文件,路径); 工作表sheet = workbook.getSheet("WorkArea"); //sheet.save(); //s
use strict; use warnings; use Spreadsheet::ParseExcel; use Spreadsheet::ParseExcel::SaveParser; my $
我有 2 个工作簿....OdysseyDeposit 我的源数据所在的位置和 Civil Bank Accnt Recon,其中包含 OdysseyDeposit 的 VLOOKUP。 这是我正在使
我正在使用 Excel for Mac 2011,我在一张纸上有几个复选框。我正在尝试使用以下代码使它们自动化: Private Sub CheckBox12_Click() Dim ws A
我有一个简单的类 calc 可以对输入进行平方和立方: object calc2 { def square(nmr: Int) = { calc.square(nmr) } def
对于以下代码,我收到此错误 Method or Data Member not found. Private Sub Workbook_Open() Dim ws1 As Worksheet:
我的第一个问题是 Cells(i, 4) 命令的作用是什么,一直到 Cells(i, 7)。 Option Explicit Sub batch1() Sheets("Batch").Acti
我可能是瞎子,但我已经使用 VBA 几年了,但仍然写出来 Workbook("Book1").Sheets("Sheet1").Range("A1").Value 或者(将 Book1 调暗为工作簿,
此正则表达式出现错误: Matcher worksheetMatcher = Pattern.compile(output).matcher("var worksheet = (.*)\\};");
如何在 PHPExcel (1.7.7) 中删除一行或多行以使 getHighestDataRow 值递减? removeRow() 的使用似乎并没有真正调整 getHighestDataRow 值。
使用 C# 编写一个方法,从工作簿中取出单个工作表并将其另存为 HTML。 我正在使用 MSDN 中描述的 Worksheet.SaveAs 方法. 但是,当我查看输出时,它已经消失并保存了工作簿中的
我有一个名为 Sheet1 的选项卡。它从名为 Data_Summary 的选项卡中过滤数据. 如果我在 Sheet1 的单元格 A2 中输入此 FILTER() 函数,然后按回车键: =FILTER
我正在尝试使用 xlsxwriter 将格式写入一系列单元格。 worksheet.write_blank('A4:B4',None,cell_formatwhite) 此 write_blank 方
函数之间有什么区别:Worksheet_BeforeDoubleClickWorkbook_SheetBeforeDoubleClick? 最佳答案 Worksheet_BeforeDoubleCli
我想知道您能否帮我弄清楚为什么 worksheet.activate 在下面的情况下什么都不做。我关掉 .ScreenUpdating = False .EnableEvents = False .D
在 Intellij Scala Worksheet 支持中,运行类型(即 PLAIN 与 REPL)之间有什么区别? 最佳答案 Plain 评估模型在评估表达式之前一次性编译整个工作表,而 REPL
如果我在 VBA 编辑器中编写此内容: Dim ws As Worksheet: set ws = ActiveSheet ws.Columns( IntelliSense 向我显示了一个看似不相关的
我是一名优秀的程序员,十分优秀!