表格不会复制数据

时间:2017-07-17 19:22:10

标签: excel excel-vba vba

       SourceWkb.Sheets("Price List").Range("C:E").Copy
       TargetWkb.Sheets("Price List").Range("C:E").PasteSpecial Paste:=xlValues

我的主要问题似乎是

   TargetWkb.Sheets("Price List").Range("A:A").Value = SourceWkb.Sheets("Price 
   List").Range("A:A")

它运行没有问题,但实际上并没有复制数据,我似乎无法解决为什么 我试过了

debounce()

Bit仍然得到了没有数据,任何想法的相同结果?

1 个答案:

答案 0 :(得分:1)

您的代码中存在一些奇怪之处。最值得注意的是:

 'Here you loop through every worksheet in your source workbook
    'but you only copy one specific sheet. This is superfluous and 
    'may be causing the issue (although it shouldn't)
    For Each SourceWksht In SourceWkb.Worksheets
        If SourceWksht.Visible Then
             SourceWkb.Sheets("Price List").Range("C:E").Copy
             TargetWkb.Sheets("Price List").Range("C:E").PasteSpecial Paste:=xlValues
        End If
    Next SourceWksht

快速重写一次,然后查看问题是否清除。我添加了一些注释来说明每个代码块正在做什么,以防它解决任何误解。

Sub CostPriceMain()

    Dim SourceWkb As Workbook
    Dim TargetWkb As Workbook       

    'shhh
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False

    'ask user for excel file to source from
    NewFile = Application.GetOpenFilename(FileFilter:="Microsoft Excel File (*.xlsx; *.xls), (*.xlsx; *.xls), All Files, *.*", FilterIndex:=1)

    'Did they pick a file?
    If Not NewFile = False Then 
        Set SourceWkb = Workbooks.Open(NewFile)
    Else 
        Exit Sub
    End If

    'Set up the target workbook
    Set TargetWkb = Workbooks.Open("C:\WK24.xlsx") ' warning - XLS file could cause problems - see note

    'Copy the price list from source workbook on the tab called "Price List"
    'For columns C through E. Copying it to the Target Workbook to the tab
    'called "Price List" using the same columns, only copying the values.
    SourceWkb.Sheets("Price List").Range("C:E").Copy
    TargetWkb.Sheets("Price List").Range("C:E").PasteSpecial Paste:=xlValues

    'Clean up
    TargetWkb.Close False
    SourceWkb.Close False

    Application.ScreenUpdating = True
    Application.DisplayAlerts = True

    'Notify user
    Done = MsgBox("Task Complete", vbOKOnly)

End Sub