0

我编写了一个 Excel VBA 来将选定单元格中的数据从一个工作簿表复制到另一个工作簿表。在粘贴一些值之后,有时 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

请帮我

谢谢

4

1 回答 1

0

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

CellTarget.Value2 = CellSource.Value2

或者在你的情况下:

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

边注:

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

于 2013-03-20T09:42:37.637 回答