【发布时间】:2016-01-05 20:44:44
【问题描述】:
让我快速介绍一下我们的流程是什么:
我将报告导出到 Excel 中(我们将此工作簿称为“原始数据”)。我在导入的文件上运行 Extract 宏:
Sub Extract_Sort_1601_January()
'
Dim ANS As Long
ANS = MsgBox("Is the January 2016 Swivel Master File checked out of SharePoint and currently open on this desktop?", vbYesNo + vbQuestion + vbDefaultButton1, "Master File Open")
If ANS = vbNo Or IsWBOpen("Swivel - Master - January 2016") = False Then
MsgBox "The required workbook is not currently open. Please open the correct file and restart the Extract process. This procedure will now terminate.", vbOKOnly + vbExclamation, "Terminate Procedure"
Exit Sub
End If
Cells.EntireRow.Hidden = False
Application.ScreenUpdating = False
' This line autofits the columns C, D, O, and P
Range("C:C,D:D,O:O,P:P").Columns.AutoFit
Dim LR As Long
For LR = Range("B" & Rows.Count).End(xlUp).Row To 2 Step -1
If Range("B" & LR).Value <> "1" Then
Rows(LR).EntireRow.Delete
End If
Next LR
With ActiveWorkbook.Worksheets("Extract").Sort
With .SortFields
.Clear
.Add Key:=Range("B2:B2000"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.Add Key:=Range("D2:D2000"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.Add Key:=Range("O2:O2000"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.Add Key:=Range("J2:J2000"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.Add Key:=Range("K2:K2000"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.Add Key:=Range("L2:L2000"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
End With
.SetRange Range("A2:Z2000")
.Apply
End With
Cells.WrapText = False
Sheets("Extract").Range("A2").Select
Dim LastRow As Integer, i As Integer, erow As Integer
LastRow = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
For i = 2 To LastRow
If Cells(i, 2) = "1" Then
' As opposed to selecting the cells, this will copy them directly
Range(Cells(i, 1), Cells(i, 26)).Copy
' As opposed to "Activating" the workbook, and selecting the sheet, this will paste the cells directly
With Workbooks("Swivel - Master - January 2016.xlsm").Sheets("Swivel")
erow = .Cells(.Rows.Count, 1).End(xlUp).Offset(1, 0).Row
.Cells(erow, 1).PasteSpecial xlPasteAll
End With
Application.CutCopyMode = False
End If
Next i
Application.ScreenUpdating = True
End Sub
这会将“提取”文件中的数据复制到另一个工作簿(该工作簿称为“Swivel”)。这部分成功完成。完成后,在“Swivel”工作簿中,我们运行删除重复宏:
Sub Remove_Duplicates()
'
Application.ScreenUpdating = False
ActiveSheet.Range("$A$1:$Z$2000").RemoveDuplicates Columns:=Array(10, 11, 12, 13, 14, 15, 16), Header:=xlYes
ActiveWindow.SmallScroll Down:=6
Range("C" & Rows.Count).End(xlUp).Offset(1).Select
Application.ScreenUpdating = True
End Sub
在将数据复制到“Swivel”工作簿和运行 Remove Duplicates 宏之间的某个位置,在刚刚粘贴的行中的 AD 列中的单元格中插入了一个空值(我认为)。我只知道这一点,因为此代码正在工作表中运行以进行更改:
Private Sub Worksheet_Change(ByVal Target As Range)
'
Dim r As Range
Set r = Target.EntireRow
If Target.row = 1 Then Exit Sub ' Don’t change header color
If r.Cells(1, "AD").Value <> "" Then
r.Font.Color = RGB(0, 176, 80)
Else
r.Font.ColorIndex = 1
End If
End Sub
为了澄清,这里是上述潜艇所在的位置:
Extract_Sort_1601_January 是我为“原始数据”文件创建的插件的一部分。
Remove_Duplicates 位于“Swivel”工作簿的一个模块中。
WorkSheet_Change 位于“Swivel”工作簿的 Sheet1 对象中。
- 报告网站的数据导出到“原始数据”工作簿
- Extract_Sort_1601_January 将数据复制到现有的“Swivel”中 工作簿(在这种情况下,工作簿名称为“Swivel - Master - 2016 年 1 月.xlsm")
- Remove_Duplicates 在“旋转”工作簿上启动。
如果“旋转”工作簿的 AD 列中没有数据,则该行中的文本应为黑色。但是,在运行 Remove Duplicates 宏后情况并非如此,文本是绿色的。如果我转到该行中的“空”单元格(AD 列)并单击删除,则该行将变为黑色文本。我还检查了单元格中是否有空格,但没有。如何编码删除使工作表更改子相信单元格中有值的“空”值?而且,这可以添加到“删除重复项”子项中吗?
感谢大家的帮助!
【问题讨论】:
-
Range(Cells(i, 1), Cells(i, 26)).Copy | .Cells(erow, 1).PasteSpecial你只是复制/粘贴列 A 到 Z。AD 是如何发挥作用的?此外,也许您可能需要考虑条件格式而不是worksheet_change事件。 -
这是这个谜团的一部分。我没有任何影响或改变 AD 列中的单元格的东西,但不知何故它们确实如此。条件格式不起作用。当我们有多个记录使用相同的事件编号时,我们只需要使用一个日期。因此,用户正在将日期复制并粘贴到接下来的 3 行中。这会弄乱 CF 规则中的范围(甚至是绝对引用)。所以这段代码被证明是无价的。
-
尽管我希望它是真的,但计算机还没有感知能力,因此有人正在更改 AD 列,或者您有其他代码,或者我遗漏了一些东西。尝试删除广告并重新开始?我不知道:/
-
@findwindow 好吧,我相信有人可能会更改 AD 列中的单元格。但是....我今天创建了这个新的并自己进行了所有测试。我想我可能也错过了一些东西,这就是我在这里发帖的原因。我只是不明白这些细胞是如何突然包含某种不可见的价值的。感谢您尝试 findwindow。您过去的所有帮助都非常有帮助。
-
等等。代码驻留在哪本书中?工作表更改代码需要在目标簿中,而不是源中。换句话说,您需要将 paste Swivel 复制到目标簿(代码所在的位置)。编辑:应该在前面提到过......应该限定你的范围。
标签: excel excel-2007 vba