2017-10-19 63 views
2

我試圖創建基於C列中的值來複制整行的宏,該值等於我定義的名稱之一(它是日期),然後,它會將這些值完全粘貼在相同的位置 - 凍結函數的排序。複製基於等於定義名稱的列中的值的行

這裏我到目前爲止所做的事情,但代碼一旦運行就沒有反應(沒有錯誤,沒有字面意思)。我一直在研究在這裏類似主題內和下面是一種混合物,我發現匹配什麼,我想獲得:

Sub testIt() 
Dim r As Long, endRow As Long, pasteRowIndex As Long 

endRow = 1000 ' preferably change for a function that will retrieve the last used row number via a function 

For r = 7 To endRow 'Loop through sheet1 and search for your criteria 

    If Cells(r, Columns("C").Column).Value = ThisWorkbook.Names("MyDate").Value Then Rows(r).Select 

    For Each cell In Selection 
    cell.Value = cell.Value 
    Next cell 
    End If 
Next r 
End Sub 

我將是一些提示非常感謝。

+1

FWIW(與您的問題中提到的問題無關)'Cells(r,Columns(「C」)。Column)'可以通過簡單地說'Cells(r,「C」) ' – YowE3K

回答

1

當我測試這個時,我發現名稱的值返回它的地址,而不是它的實際值,所以使用Range來代替。此外,沒有必要選擇並遍歷每個單元格(儘管我懷疑你需要在整行上操作)。

Sub testIt() 

Dim r As Long, endRow As Long, pasteRowIndex As Long 

endRow = 1000 ' preferably change for a function that will retrieve the last used row number via a function 

For r = 7 To endRow 'Loop through sheet1 and search for your criteria 
    If Cells(r, 3).Value = Range("MyDate").Value Then 
     Rows(r).Value = Rows(r).Value 
    End If 
Next r 
'Alternatively you could use ThisWorkbook.Names("MyDate").Referstorange.Value 
End Sub 
+0

它工作完美!正如預期:)非常感謝您的快速響應! – Dozens