在excel中复制/粘贴范围时出现运行时错误

时间:2016-01-07 12:15:57

标签: excel-vba vba 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

1 个答案:

答案 0 :(得分: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