2016-01-07 39 views
1

專家,快速運行時錯誤而在Excel中複製/粘貼範圍

什麼,我這裏做的是在第35行循環通在C列的單元格,複製活動的價值,並在M9粘貼,並然後我循環另一個並使用「& Chr(10)&」粘貼在同一個單元格中。 這將基本上這樣做「A」& Chr(10)&「B」,並將在同一單元格 - M9中顯示一個條目下方的其他條目。

這基本上是一個提醒程序,將收集所有未完成的任務,並收集在一個單元格 - M9。 宏將在workbook_open事件上執行。

的代碼如下

Private Sub Workbook_Open() 
Dim Activity As String            ' Variables for: Activity Name 
Dim RowNrNumeric As Integer           ' Number of Row as Numeric with Activity 
Dim RowNrString As String           ' Number of Row as String with Activity 
Dim CloumnNameActivity As String         ' column name of cell with Activity 
Dim CloumnNameDate As String          ' column name of cell with due date 
Dim CloumnNameRemStatus As String         ' column name of cell with status 
Dim DueDate As Date             ' Due Day value 
Dim RemStatus As String            ' Status 
Dim TextDay As String            ' string for due date day 
Dim TextMonth As String            ' string for due date month 
Dim TextYear As String            ' string for due date year 
Dim ActCopy, ActPas As Range 

CloumnNameActivity = "C"           ' ----ENTER name of cloumn with full Activity 
CloumnNameDate = "D"            ' ----ENTER name of cloumn with full due date 
CloumnNameRemStatus = "F"           ' ----ENTER name of cloumn with reminder status 

RowNrNumeric = 4             ' ----ENTER first row number with Activity 
RowNrString = RowNrNumeric           ' assigning numeric type to string 
Activity = Range(CloumnNameActivity + RowNrString).Value   ' reading and assigning Activity from the cell 
DueDate = Range(CloumnNameDate + RowNrString).Value     ' reading and assigning due date from the cell 
RemStatus = Range(CloumnNameRemStatus + RowNrString).Value   ' reading and assigning reminder status from the cell 

Do While Activity <> ""  ' loop till the issue is not empty 
    Range(CloumnNameDate + RowNrString).Interior.Color = vbWhite  'changing fill for ever for white 
    If (RemStatus <> "Done" And DateDiff("d", DueDate, Date) >= -2) Then ' if reminder status is ON and (system day - DueDate) is >= than.... . Additionl note : You can put -30 instead 0 when it should reminde 30 days before due date 
     TextDay = Day(DueDate)          ' to text 
     TextMonth = Month(DueDate)         ' to text 
     TextYear = Year(DueDate)          ' to text 
     'Range(Activity).Activate 
     MsgBox "ACTIVITY: " + Activity + " DUE DATE is : " + TextMonth + "-" + TextDay + "-" + TextYear ' gluing message 
    Range(CloumnNameDate + RowNrString).Interior.Color = vbRed  ' for those wiht reminder changing fill for red 

    Range(CloumnNameActivity, 0).Select 
     Selection.Copy 

     ' ActPas = Range(CloumnNameDate).Offset(0, 9).Value.PasteSpecial 

     End If 


    RowNrNumeric = RowNrNumeric + 1         ' row down 
    RowNrString = RowNrNumeric          ' to string 
    Activity = Range(CloumnNameActivity + RowNrString).Value  ' reading and assigning Activity from the cell 


If ((IsDate(Range(CloumnNameDate + RowNrString).Value)) = True And (IsEmpty(Range(CloumnNameDate + RowNrString).Value)) = False) Then 
    DueDate = Range(CloumnNameDate + RowNrString).Value    ' reading and assigning due date from the cell 
    RemStatus = Range(CloumnNameRemStatus + RowNrString).Value  ' reading and assigning reminder status from the cell 
Else 
RemStatus = "" 
End If 

    ' DueDate = "12-30-2016" 



Loop 

End Sub 
+0

什麼錯誤你獲得和哪一行? – Linga

回答

0

有一個語法錯誤,用下面的代碼行:

Range(CloumnNameActivity, 0).Select 
Selection.copy 

也許你想做的事:

Range(CloumnNameActivity + RowNrString).Select 
Selection.copy 

,然後像:

Range(CloumnNameDate + RowNrString).Offset(0, 9).Select 
Selection.PasteSpecial 

爲了粘貼在一個單細胞的所有值,你可以替換上面代碼:

Range("your_destination_cell").value = Range("your_destination_cell").value & _ 
    Range(CloumnNameActivity + RowNrString).value 
+0

hi baudo,你是對的這個..我想要的是粘貼所有複製的值在一個單元格中說「M9」 – rkayasth

+0

baudo,另一件事,你的代碼 範圍(「your_destination_cell」)。value = Range(「your_destination_cell 「).value&_ 範圍(CloumnNameActivity + RowNrString).value 只是給我的活動名稱,而我也需要日期。 – rkayasth