修改数据的粘贴(限制范围)
Modify the pasting of data (constraining the range)
我正在使用以下代码:
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
这按预期工作,但我需要限制粘贴的范围。当这段代码是运行时,它复制了A2:Z2的范围(这个问题的样本范围,它实际上复制了比这更多的行),但它粘贴到Z列以外的单元格。我最关心的是AD 列,因为当有一个值插入该列时,有代码将该行的文本更改为绿色。在 copy/paste 代码为 运行 之后,该行变为绿色文本,即使 AD 中没有任何内容。下面是将行中的文本更改为绿色的代码(此代码位于工作簿的 Sheet1 对象中)。
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
现在,在我们完成工作的行的 AD 列中,我们以这种格式插入日期和时间:1/4/2016 13:20。我可以更改此行吗:
If r.Cells(1, "AD").Value <> "" Then
要检查格式而不是值?
我还在学习VBA,但我知道我还有很多东西要学。感谢任何帮助。
编辑:直到此代码 运行(位于目标工作簿 "Swivel" 中)之后才会出现异常:
Sub Remove_Duplicates()
'
Application.ScreenUpdating = False
ActiveSheet.Range("$A:$Z00").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
这会将文本更改为绿色,但该行的 AD 列中没有任何内容可以触发更改。
因为我看到你在这里问的唯一问题是:
Can I change this line: If r.Cells(1, "AD").Value <> "" Then
to check for the format rather than the value?
这是一种方法:
Me.Cells(Target.Row,30).Activate 'column 30 for AD
If Application.ExecuteExcel4Macro("GET.CELL(7)") = "m/d/yyyy h:mm;@" Then
根据完全需要调整您的格式。我只是根据你问题中的信息猜测的。
我没有尝试在 Worksheet_Change 子中解决这个问题,而是将 Remove_Duplicates 子修改为:
Sub Remove_Duplicates()
'
Application.ScreenUpdating = False
Dim usedrng As Range
ActiveSheet.Range("$A:$Z00").RemoveDuplicates Columns:=Array(5, 10, 11, 12, 13, 14, 15, 16), Header:=xlYes
For Each usedrng In ActiveSheet.UsedRange
If usedrng.Value = "" Then
usedrng.ClearContents
End If
Next
Range("C" & Rows.Count).End(xlUp).Offset(1).Select
Application.ScreenUpdating = True
End Sub
这已经有效地消除了我一直在争论的假空值。 WorkSheet_Change sub 现在按照上面第一个问题中所写的那样工作,工作表的行为方式与添加新数据行时的方式相同,即文本应保持黑色直到 date/time在该行的 AD 列中输入。
感谢所有提供帮助的人。我希望这个答案对其他人也有帮助。
我正在使用以下代码:
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
这按预期工作,但我需要限制粘贴的范围。当这段代码是运行时,它复制了A2:Z2的范围(这个问题的样本范围,它实际上复制了比这更多的行),但它粘贴到Z列以外的单元格。我最关心的是AD 列,因为当有一个值插入该列时,有代码将该行的文本更改为绿色。在 copy/paste 代码为 运行 之后,该行变为绿色文本,即使 AD 中没有任何内容。下面是将行中的文本更改为绿色的代码(此代码位于工作簿的 Sheet1 对象中)。
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
现在,在我们完成工作的行的 AD 列中,我们以这种格式插入日期和时间:1/4/2016 13:20。我可以更改此行吗:
If r.Cells(1, "AD").Value <> "" Then
要检查格式而不是值?
我还在学习VBA,但我知道我还有很多东西要学。感谢任何帮助。
编辑:直到此代码 运行(位于目标工作簿 "Swivel" 中)之后才会出现异常:
Sub Remove_Duplicates()
'
Application.ScreenUpdating = False
ActiveSheet.Range("$A:$Z00").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
这会将文本更改为绿色,但该行的 AD 列中没有任何内容可以触发更改。
因为我看到你在这里问的唯一问题是:
Can I change this line: If r.Cells(1, "AD").Value <> "" Then
to check for the format rather than the value?
这是一种方法:
Me.Cells(Target.Row,30).Activate 'column 30 for AD
If Application.ExecuteExcel4Macro("GET.CELL(7)") = "m/d/yyyy h:mm;@" Then
根据完全需要调整您的格式。我只是根据你问题中的信息猜测的。
我没有尝试在 Worksheet_Change 子中解决这个问题,而是将 Remove_Duplicates 子修改为:
Sub Remove_Duplicates()
'
Application.ScreenUpdating = False
Dim usedrng As Range
ActiveSheet.Range("$A:$Z00").RemoveDuplicates Columns:=Array(5, 10, 11, 12, 13, 14, 15, 16), Header:=xlYes
For Each usedrng In ActiveSheet.UsedRange
If usedrng.Value = "" Then
usedrng.ClearContents
End If
Next
Range("C" & Rows.Count).End(xlUp).Offset(1).Select
Application.ScreenUpdating = True
End Sub
这已经有效地消除了我一直在争论的假空值。 WorkSheet_Change sub 现在按照上面第一个问题中所写的那样工作,工作表的行为方式与添加新数据行时的方式相同,即文本应保持黑色直到 date/time在该行的 AD 列中输入。
感谢所有提供帮助的人。我希望这个答案对其他人也有帮助。