尝试复制和粘贴使用不同的工作簿,但正确的值不被复制
我正在尝试复制并粘贴两个不同的工作簿,但正确的值不被复制。 我相信这是因为我在另一个“with”语句中使用“with”语句,但是我不知道如何避免这种情况,因为我正在使用for循环。 这是我的代码到目前为止:
Sub Approval_Flow() Dim AppFlowWkb As Workbook, ConfigWkb As Workbook Dim AppFlowWkst As Worksheet, ConfigWkst As Worksheet Dim aCell As Range Dim targetRng As Range Set AppFlowWkb = Workbooks.Open("C:\Users\clara\Documents\Templates and Scripts\Flow Change.xlsx") Set ConfigWkb = ThisWorkbook Set AppFlowWkst = AppFlowWkb.Sheets("Editor") Set ConfigWkst = ConfigWkb.Worksheets("Approval Flows") With ConfigWkst 'looking through each column value before moving on to next row For Each aCell In .Range("A1:K" & .UsedRange.Rows.Count) 'if cell is highlighted, copy that row's column D value If Not aCell.Interior.Color = RGB(255, 255, 255) Then Range("D" & (aCell.row)).Copy 'in appflow workbook, find empty row in column A and paste With AppFlowWkst Set targetRng = .Range("A" & Rows.Count).End(xlUp).Offset(1) targetRng.PasteSpecial xlPasteValues End With 'Range("C" & (aCell.row)).Copy 'With AppFlowWkst 'Set targetRng = .Range("B" & Rows.Count).End(xlUp).Offset(1) 'targetRng.PasteSpecial xlPasteValues 'once the rows' values have been pasted, move on to next row End If Next aCell End With End Sub
我希望这些评论可以让你很好地理解我正在做的事情。 此外,一旦我复制并粘贴该行的所有值,我想For循环不继续为该行,但为下一行。 任何build议,我怎么能做到这一点? 谢谢!
你错过了.
在这一行的Range
前面,所以它是从ActiveSheet
复制的:
Range("D" & (aCell.Row)).Copy
它应该是.Range("D" & (aCell.Row)).Copy
。
也就是说,你甚至不需要使用.Copy
和.Copy
,因为你.Paste
的唯一东西就是价值。 只需直接分配它们:
With ConfigWkst For Each aCell In .Range("A1:K" & .UsedRange.Rows.Count) If Not aCell.Interior.Color = RGB(255, 255, 255) Then Set targetRng = .Range("A" & Rows.Count).End(xlUp).Offset(1) 'Use a direct assignment. targetRng.Value = .Range("D" & (aCell.Row)).Value End If Next aCell End With
我不认为与声明双重是你的问题,但你可以删除第二个:
Set targetRng = AppFlowWkst.Range("A" & Rows.Count).End(xlUp).Offset(1) targetRng.PasteSpecial xlPasteValues
看看它是否能解决你的问题。 它至less会让你消除你的担忧。
事实上,你不需要声明,可以做一些事情:
For Each aCell In ConfigWkst.Range("A1:K" & ConfigWkst.UsedRange.Rows.Count) 'if cell is highlighted, copy that row's column D value If Not aCell.Interior.Color = RGB(255, 255, 255) Then ConfigWkst.Range("D" & (aCell.row)).Copy 'in appflow workbook, find empty row in column A and paste Set targetRng = AppFlowWkst.Range("A" & Rows.Count).End(xlUp).Offset(1) targetRng.PasteSpecial xlPasteValues End If Next aCell
- pandas:比较pandas中的2个excel文件,返回其中一列中的值存在于另一列中的行
- 在Excel中匹配一个特定的string,并使用python打印这些行
- 另一个工作簿的AdvancedFilter Criteriarange
- VBA AddressOf崩溃办公室应用程序
- 在Mac OS X Sierra上设置DYLD_LIBRARY_PATH(CoolProp)
- 如果从ArrayList传递给Excel,如何从XML中检测新的行字符?
- macros在单元格中插入单词TRUE
- 在尝试从Java(POI API)设置excel中的cellvalue时,单元格值不会复制到正确的行中
- 我怎样才能使用MATCH()与不同的catagorized数据?