我有将数据从一个电子表格粘贴到另一个电子表格的代码。
我从中提取数据的电子表格在单元格中有数据或“NR”。
我将其设置为删除所有粘贴的“NR”。
我需要:
该代码每天都会运行,每月只有一天会有数据,需要保留在目标电子表格中。我不希望它被“NR”覆盖。
这是我所拥有的一个例子。这会用“NR”覆盖合法数据,然后删除“NR”,使单元格保持空白。
注意:我粘贴到的单元格是不连续的,这是一个小样本。
我想我需要类似
with wsElog2
的东西,如果单元格为空,则复制,否则转到下一个范围'。我不知道如何对我拥有的半随机单元格执行此操作。
wsElog2.Range("E60") = wseDNA2.Range("DE15") 'Filtered Effluent Calcium
wsElog2.Range("E61") = wseDNA2.Range("DO15") 'Filtered Effluent Magnesium
wsElog2.Range("E64") = wseDNA2.Range("EA15") 'Filtered Effluent Potassium
wsElog2.Range("E62") = wseDNA2.Range("EE15") 'Filtered Effluent Sodium
'Lagoons 1
wsElog2.Range("G55") = wseDNA2.Range("EU15") 'Ammonia
wsElog2.Range("G48") = wseDNA2.Range("EW15") 'Blue Green Algae
wsElog2.Range("G52") = wseDNA2.Range("EY15") 'E. coli
wsElog2.Range("G59") = wseDNA2.Range("FA15") 'EC
wsElog2.Range("G54") = wseDNA2.Range("FC15") 'Nitrate
wsElog2.Range("G53") = wseDNA2.Range("FE15") 'Nitrite
wsElog2.Range("G57") = wseDNA2.Range("FG15") 'TN
wsElog2.Range("G58") = wseDNA2.Range("FI15") 'pH
wsElog2.Range("G56") = wseDNA2.Range("FK15") 'TP
For Each c In wsElog2.Range("B1:L159")
If c.Text = "NR" Then
c.Value = ""
End If
Next
通过提供的答案解决了这个问题,并对代码进行了一些小改动,如下所示:
(注意:我没有让 DEBUG.PRINT 部分正常工作。我摆脱了它们。)
Sub CopyCellValues()
' Monitor the behavior in the Immediate window (Ctrl+G), or not.
Const DEBUG_PRINT As Boolean = True
' Write the cell addresses to arrays.
Dim SourceCells() As Variant: SourceCells = Array("DO15", "EA15", "EE15")
Dim TargetCells() As Variant: TargetCells = Array("E61", "E64", "E62")
' Existing code that e.g. sets the worksheets, ...
Dim sVal As Variant, n As Long, IsSourceValid As Boolean
For n = LBound(SourceCells) To UBound(SourceCells)
With wsElog2.Range(TargetCells(n, 1))
' Check target.
If Len(CStr(.Value)) = 0 Then ' blank
' Check source.
sVal = wseDNA2.Range(SourceCells(n, 1)).Value ' store in variable
If Not IsError(sVal) Then ' not an error value
If Len(sVal) > 0 Then ' not blank
If StrComp(sVal, "NR", vbTextCompare) <> 0 Then ' not equal
IsSourceValid = True
End If
End If
End If
End If
If IsSourceValid = True Then
' Write.
.Value = sVal
' Reset.
IsSourceValid = False
If DEBUG_PRINT = True Then Debug.Print "Copying """ & CStr(sVal) _
& """ from """ & wseDNA2.Name & "!" & SourceCells(n) _
& """ to """ & .Worksheet.Name & "!" & .Address(0, 0) & """!"
Else
If DEBUG_PRINT = true Then Debug.Print "Not copying """ & CStr(sVal) _
& """ from """ & wseDNA2.Name & "!" & SourceCells(n) _
& """ to """ & .Worksheet.Name & "!" & .Address(0, 0) & """!"
End If
End With
Next n
End Sub
Sub CopyCellValues()
' Monitor the behavior in the Immediate window (Ctrl+G), or not.
Const DEBUG_PRINT As Boolean = True
' Write the cell addresses to arrays.
Dim SourceCells() As Variant: SourceCells = Array("DO15", "EA15", "EE15")
Dim TargetCells() As Variant: TargetCells = Array("E61", "E64", "E62")
' Existing code that e.g. sets the worksheets, ...
Dim sVal As Variant, n As Long, IsSourceValid As Boolean
For n = LBound(SourceCells) To UBound(SourceCells)
With wsElog2.Range(TargetCells(n))
' Check target.
If Len(CStr(.Value)) = 0 Then ' blank
' Check source.
sVal = wseDNA2.Range(SourceCells(n)).Value ' store in variable
If Not IsError(sVal) Then ' not an error value
If Len(sVal) > 0 Then ' not blank
If StrComp(sVal, "NR", vbTextCompare) <> 0 Then ' not equal
IsSourceValid = True
End If
End If
End If
End If
If IsSourceValid Then
' Write.
.Value = sVal
' Reset.
IsSourceValid = False
If DEBUG_PRINT Then Debug.Print "Copying """ & CStr(sVal) _
& """ from """ & wseDNA2.Name & "!" & SourceCells(n) _
& """ to """ & .Worksheet.Name & "!" & .Address(0, 0) & """!"
Else
If DEBUG_PRINT Then Debug.Print "Not copying """ & CStr(sVal) _
& """ from """ & wseDNA2.Name & "!" & SourceCells(n) _
& """ to """ & .Worksheet.Name & "!" & .Address(0, 0) & """!"
End If
End With
Next n
End Sub