Excel VBA Selection.Copy正在粘贴目标工作簿表中的空值

时间:2013-03-20 06:48:41

标签: excel excel-vba vba

我编写了一个Excel VBA,用于将选定单元格中的数据从一个工作簿工作表复制到另一个工作簿工作表。在粘贴一些值之后,在粘贴空值之后,它在某些单元格中工作正常。我的意思是,尽管源单元格不是空的,但它正在粘贴空值。我已经把断点和锯,但价值在那里。请帮我解决这个问题。

代码如下。

Set objClickScriptWB = objExcelWB.Workbooks.Open(SourceWBPath)
    For intSheet = 9 To 12 'objClickScriptWB.Worksheets.Count
        If InStr(1, objClickScriptWB.Worksheets(intSheet).Name, "SC", vbTextCompare) > 0 Then
            blnScriptSheet = 1
            objClickScriptWB.Worksheets(intSheet).Activate
            For r = 24 To objClickScriptWB.Worksheets(intSheet).UsedRange.Rows.Count
                    If Trim(LCase(objClickScriptWB.Worksheets(intSheet).Cells(r, 6).Value)) <> Trim(LCase("Transaction")) And Trim(LCase(objClickScriptWB.Worksheets(intSheet).Cells(r, 6).Value)) <> Empty And objClickScriptWB.Worksheets(intSheet).Cells(r, 6).MergeArea.Cells.Count = 1 Then
  objClickScriptWB.Worksheets(intSheet).Cells(r, 6).Select



                                If blnCompSht = 0 Then
                                    Set objComparisonSheet = ThisWorkbook.Worksheets.Add
                                    objComparisonSheet.Name = "Comparison"
                                    objComparisonSheet.Activate

                                    objComparisonSheet.Cells(2, 2).Value = "Clickscript Transaction names"

                                    i = 3
                                    objExcelWB.Selection.Copy
                                    objComparisonSheet.Activate
                                    objComparisonSheet.Cells(i, 2).Select
                                    'Sheet3.Range("B2").Select
                                    'objComparisonSheet.Range("B" & i).PasteSpecial Paste:=xlPasteValues
                                    objComparisonSheet.Paste
                                    'Sheet2.Range("G2").Cells
                                    i = i + 1
                                    blnCompSht = 1
                                    'Application.Wait (Now + TimeValue("00:00:01"))
                                ElseIf blnCompSht = 1 Then
                                    ThisWorkbook.Worksheets("Comparison").Activate

                                    Dim LastRow As Integer
                                    For intRow = 2 To ThisWorkbook.Worksheets("Comparison").Rows.Count
                                        If ThisWorkbook.Worksheets("Comparison").Cells(intRow, 2).Value = Empty Then
                                            i = intRow
                                            Exit For
                                        End If

                                    Next

                                    objExcelWB.Selection.Copy
                                    ThisWorkbook.Worksheets("Comparison").Cells(i, 2).Select
                                    'ThisWorkbook.Worksheets("Comparison").Range("B" & intRow).PasteSpecial Paste:=xlPasteValues
                                    ThisWorkbook.Worksheets("Comparison").Paste
                                    i = i + 1
                                    'Application.Wait (Now + TimeValue("00:00:01"))
                                End If

                            'End If
                        'Next
                'Call CompareTxnNames(objClickScriptWB.Worksheets(intSheet).Name)
                    End If
                'Next
            Next

        End If
    Next
End Sub

请帮帮我

由于

1 个答案:

答案 0 :(得分:0)

您还可以使用以下代码直接将一个单元格的值应用于另一个单元格:

CellTarget.Value2 = CellSource.Value2

或者在你的情况下:

objComparisonSheet.Cells(i, 2).Value2 = objClickScriptWB.Worksheets(intSheet).Cells(r, 6).Value2

旁注:

养成使用Value2的习惯,因为这是与Value相比的单元格的真实值,Value是应用格式的值。后者较慢,如果您在Excel中不使用美国日期格式,日期值可能会给出错误的日期和月份。