将数据从一个 excel 工作簿复制到另一个工作簿,同时保留格式

Copy data from one excel workbook to another while retaining formatting

我是 excel VBA 的新手。我已经将 VBA 代码写入 select 任何 Excel 文件并将该文件的路径复制到单元格 A1。使用我尝试复制源文件 Sheet7 的内容的路径,同时保留单元格格式,即粗体、边框、颜色等。

我的第一个错误是文件路径。当前单元格 A1 值 = C:\Users\Personal\Documents\Excel files\Dummy-Data - Copy.xlsx。 当我尝试读取 A1 单元格的值时,VBA 抛出错误 "Sorry, we couldn't find. Is it possible it was moved, renamed or deleted?" 并自动清除单元格 A1 的值。但是当我直接在 VBA 脚本中给出相同的路径时,它起作用了!谁能告诉我如何解决这个问题?

我的第二个疑问是关于复制单元格格式。当我使用 wksht.paste 将复制的内容粘贴到 Sheet2 时,它只是粘贴所有单元格值而没有格式化。但是当我尝试使用 PasteSpecial 时发生以下错误 - "Application-defined or object-defined error" 。有人可以帮我改正吗?

    Sub Button1_Click()
        ' define variables
        Dim lastRow As Long
        Dim myApp As Excel.Application
        Dim wbk As Workbook
        Dim wkSht As Object
        Dim filePath As Variant

    'on error statement
    On Error GoTo errHandler:

        ' Select file path
        Set myApp = CreateObject("Excel.application")
        Sheet2.Range("A1").Value = filePath
        Set wbk = myApp.Workbooks.Open(filePath)
        'Set wbk = myApp.Workbooks.Open("C:\Users\Personal\Documents\Excel files\Dummy-Data - Copy.xlsx")

    ' Copy contents
        Application.ScreenUpdating = False
        lastRow = wbk.Sheets(7).Range("A" & Rows.Count).End(xlUp).Row
        wbk.Sheets(7).Range("A2:Q" & lastRow).Copy
        myApp.DisplayAlerts = False
        wbk.Close
        myApp.Quit

    ' Paste contents
        Set wbk = Nothing
        Set myApp = Nothing
        Set wbk = ActiveWorkbook
        Set wkSht = wbk.Sheets("Sheet2")
        wkSht.Activate
        Range("A2").Select
        wkSht.Paste
        'wkSht.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
                False, Transpose:=False

        Application.ScreenUpdating = True
        Exit Sub

    'error block
errHandler:
            MsgBox "An Error has Occurred " & vbCrLf & "The error number is: " _
            & Err.Number & vbCrLf & Err.Description & vbCrLf & _
            "Please follow instruction sheet"

        End Sub

My first error is appearing for file path. Currently cell A1 value = C:\Users\Personal\Documents\Excel files\Dummy-Data - Copy.xlsx. When I try to read value of A1 cell, VBA throws me an error "Sorry, we couldn't find. Is it possible it was moved, renamed or deleted?" and automatically clears the value of cell A1.

您不是将变量的值设置为单元格的值,而是将单元格的值设置为空白变量,从而擦除单元格的值。它应该是 filePath = Sheet2.Range("A1").Value,(与上面的相反)。

When I use wksht.paste to paste copied content to Sheet2, it just pastes all cell values without formatting.

您不仅仅是在工作簿之间粘贴;您在单独的应用程序实例中打开的工作簿之间进行粘贴。跨实例粘贴时会丢失格式等细节。无论如何,单独的 Excel.Application 似乎完全没有必要。

Option Explicit

Sub Button1_Click()
    ' define variables
    Dim lastRow As Long
    Dim wbk As Workbook
    Dim filePath As Variant

    'on error statement
    On Error GoTo errHandler:

    ' Select file path
    filePath = Sheet2.Range("A1").Value
    Set wbk = Workbooks.Open(filePath)
    'Set wbk = Workbooks.Open("C:\Users\Personal\Documents\Excel files\Dummy-Data - Copy.xlsx")

' Copy contents & Paste contents
    Application.ScreenUpdating = False

    lastRow = wbk.Sheets(7).Range("A" & Rows.Count).End(xlUp).Row
    wbk.Sheets(7).Range("A2:Q" & lastRow).Copy _
        Destination:=Sheet2.Range("A2")

    'shouldn't have to disable alerts
    'Application.DisplayAlerts = False
    wbk.Close savechanges:=False
    'Application.DisplayAlerts = True
'
    Application.ScreenUpdating = True
    Exit Sub

'error block
errHandler:
    MsgBox "An Error has Occurred " & vbCrLf & "The error number is: " _
        & Err.Number & vbCrLf & Err.Description & vbCrLf & _
        "Please follow instruction sheet"

End Sub

裸工作表代号引用应该在 ThisWorkbook 中有效。