【问题标题】:Sheets not copying data over表格不复制数据
【发布时间】:2017-07-18 04:05:41
【问题描述】:
Sub CostPriceMain()

Dim SourceWkb As Workbook
Dim TargetWkb As Workbook
Dim SourceWksht As Worksheet

Application.ScreenUpdating = False
Application.DisplayAlerts = False

NewFile = Application.GetOpenFilename(FileFilter:="Microsoft Excel Files         
(*.xlsx; *.xls), (*.xlsx; *.xls), All Files, *.*", FilterIndex:=1)

If NewFile = False Then Exit Sub
If NewFile <> False Then
Set SourceWkb = Workbooks.Open(NewFile)
End If

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

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

TargetWkb.Close False
SourceWkb.Close False

Application.ScreenUpdating = True
Application.DisplayAlerts = True

Done = MsgBox("Task Complete", vbOKOnly)

End Sub

我的主要问题似乎与

       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")

Bit 在没有数据的情况下仍然得到相同的结果,有什么想法吗?

【问题讨论】:

  • 您确定工作表名称和范围是否正确?是不是在复制数据语句的那一部分没有触发?使用F8 单步执行宏,看看它是否运行了该行。我认为问题可能出在逻辑上。您正在循环访问源代码中的工作表,但没有对可见工作表执行任何操作。
  • 为什么要遍历工作表(例如,For Each SourceWksht In SourceWkb.Worksheets,如果您要复制特定工作表的数据?

标签: excel vba


【解决方案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

试试这个快速重写,看看问题是否解决了。我添加了 cmets 来说明每个代码块在做什么,以防它阐明任何误解。

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

【讨论】:

  • 这给了我一个关于 NewFile = Application.GetOpenFilename(FileFilter:="Microsoft Excel Files 的语法错误
  • @JamesPavett 对此感到抱歉。从您的原始代码复制/粘贴问题。我已经修好了。
  • 现在似乎是 If NewFile Then 的类型不匹配。
  • 再次抱歉。在这里调试遥控器并抓住机会。我已经更新了它,它现在应该很高兴。
  • 由于某种原因它起作用了,但又没有复制数据。我开始怀疑我的源文件是否有问题,但我根本看不到。
猜你喜欢
  • 1970-01-01
  • 2017-09-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-21
相关资源
最近更新 更多