使用Excel 365中的VBA复制从CSV到XLSX文件的数据

发布于 2025-01-28 23:13:53 字数 684 浏览 2 评论 0原文

我正在尝试创建一个宏,将所有数据从CSV文件复制到执行宏的工作簿。

中获得了以下代码

我从

我已经用.csv,.xlsx文件对此进行了测试;以及表名称和数字的组合。

我怀疑它找不到当前的工作簿,因为我已经将Marco保存到了个人宏观文件Excel生成。

有人可以建议我如何纠正粘贴位置吗?

Sub AS_and_L_Reports()
    Dim FileToOpen As Variant
    Dim OpenBook As Workbook
    Application.ScreenUpdating = False
    FileToOpen = Application.GetOpenFilename
    If FileToOpen <> False Then
        Set OpenBook = Application.Workbooks.Open(FileToOpen)
        OpenBook.Sheets(1).Range("A1:P20").Copy
        ThisWorkbook.Worksheets(1).Range("A1").PasteSpecial xlPasteValues
        OpenBook.Close True
    End If
    Application.ScreenUpdating = True
End Sub

I am trying to create a macro that copies all the data from a csv file to the workbook that executed the macro.

I got the code below from this Youtube video

It appears to copy, but fails to paste.

I have tested this with .csv, .xlsx files; and combinations of sheet names and numbers.

I suspect it can't find the current workbook, because I have save the Marco to the Personal Macro file excel generates.

Can someone please advise how I can correct the paste location?

Sub AS_and_L_Reports()
    Dim FileToOpen As Variant
    Dim OpenBook As Workbook
    Application.ScreenUpdating = False
    FileToOpen = Application.GetOpenFilename
    If FileToOpen <> False Then
        Set OpenBook = Application.Workbooks.Open(FileToOpen)
        OpenBook.Sheets(1).Range("A1:P20").Copy
        ThisWorkbook.Worksheets(1).Range("A1").PasteSpecial xlPasteValues
        OpenBook.Close True
    End If
    Application.ScreenUpdating = True
End Sub

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文