简体   繁体   English

VBA:仅复制和粘贴特定范围内的值,并保存在新工作簿中

[英]VBA: Copy and Paste values only of a specific range and save in a new workbook

This seems to be a simple question so I am sorry if I should have been able to find it in the search but none of the answers have been able to help me. 这似乎是一个简单的问题,所以很抱歉,如果我应该能够在搜索中找到它,但是没有答案可以帮助我。 I am looking for a way to copy range A1:D14 and save it in a new workbook where only the format and values get saved to the new workbook. 我正在寻找一种方法来复制范围A1:D14并将其保存在新的工作簿中,其中只有格式和值会保存到新的工作簿中。

So basiaclly I have a range of data that has a lot of formulas and values that come from other sheets, but when my current code saves it, it has to do some weird delete method and it currently saves all of the data which means the values show up, but when I click on them it is the formula inside not the actual data. 因此,很奇怪,我有一系列包含来自其他工作表的大量公式和值的数据,但是当我当前的代码保存它时,它必须执行某种奇怪的删除方法,并且当前保存所有的数据,这意味着这些值出现,但是当我单击它们时,它是内部公式而不是实际数据。

Sub SaveData()


Dim SaveFile As String

Dim Title As String


Title = "DigitalStorage"



SaveFile = Application.GetSaveAsFilename(InitialFileName:=Title & "_" & Format(Now, "yyyy-MM-dd hh-mm-ss"), _
                                         fileFilter:="Excel Workbooks (*.xlsx),*.xlsx")


ThisWorkbook.Worksheets("SaveSheet").Copy



With ActiveWorkbook

    With .Worksheets("SaveSheet")

        ThisWorkbook.Sheets(1).Range("A1:D14").Copy

        .Columns("E:ABC").EntireColumn.Delete

        .Rows("14:100").EntireRow.Delete

    End With

    .SaveAs Filename:=SaveFile, FileFormat:=xlOpenXMLWorkbook

    .Close savechanges:=False

End With

End Sub 结束子

I have tried adding lines where I copy the sheet and PasteSpecial XlValues but that seems to overwrite my original workbook, and I just want the values and format in a plain xlsx file. 我尝试在复制工作表和PasteSpecial XlValues的位置添加行,但这似乎覆盖了我的原始工作簿,我只想在纯xlsx文件中保存值和格式。 And I also feel like my code is clunky and convoluted and that there is a much easier way to go about this that looks totally different from my method. 而且我还觉得我的代码很笨拙且令人费解,并且有一种更简单的方法来实现,这看起来与我的方法完全不同。

Try this code, read the comments inside and look for the <<<< Customize this >>> lines: 尝试以下代码,阅读其中的注释,并查找<<<< Customize this >>>行:

Sub SaveData()

    ' Declare objects
    Dim sourceWorkbook As Workbook
    Dim targetWorkbook As Workbook
    Dim sourceRange As Range
    Dim targetRange As Range
    Dim cellRange As Range

    ' Declare other variables
    Dim targetWorkbookName As String
    Dim targetWorkbookTitle As String

    Dim sourceSheetName As String
    Dim sourceRangeAddress As String
    Dim targetRangeAddress As String

    Dim rowCounter As Long


    ' <<< Customize this >>>
    sourceSheetName = "SaveSheet" ' Name of the source sheet
    sourceRangeAddress = "A1:D14" ' Address of the range you want to copy in the source workbook
    targetRangeAddress = "A2" ' Cell address where you want to paste the copied range
    targetWorkbookTitle = "DigitalStorage" ' Base file name

    ' Reference source workbook
    Set sourceWorkbook = ThisWorkbook

    ' Create a new workbook
    Set targetWorkbook = Application.Workbooks.Add

    ' Set reference to source range
    Set sourceRange = sourceWorkbook.Sheets(sourceSheetName).Range(sourceRangeAddress)

    ' Copy the range to clipboard
    sourceRange.Copy

    ' This copies the range in the first available worksheet begining in the cell address specified
    targetWorkbook.Sheets(1).Range(targetRangeAddress).PasteSpecial Paste:=xlPasteValues
    targetWorkbook.Sheets(1).Range(targetRangeAddress).PasteSpecial Paste:=xlPasteFormats
    targetWorkbook.Sheets(1).Range(targetRangeAddress).PasteSpecial Paste:=xlPasteColumnWidths

    Set targetRange = targetWorkbook.Sheets(1).Range(targetRangeAddress).Resize(sourceRange.Rows.Count, sourceRange.Columns.Count)

    ' Adjust row heights
    For Each cellRange In sourceRange.Columns(1).Cells

        rowCounter = rowCounter + 1

        targetRange.Rows(rowCounter).RowHeight = cellRange.RowHeight

    Next cellRange

    ' Set the name of the new workbook
    targetWorkbookName = Application.GetSaveAsFilename(InitialFileName:=targetWorkbookTitle & "_" & Format(Now, "yyyy-MM-dd hh-mm-ss"), _
                                         fileFilter:="Excel Workbooks (*.xlsx),*.xlsx")

    If targetWorkbookName = vbNullString Then
        MsgBox "Saving operation canceled"
        Exit Sub
    End If

    ' Save the new workbook
    targetWorkbook.SaveAs Filename:=targetWorkbookName ' Un comment this if you want it in OpenXML format: , FileFormat:=xlOpenXMLWorkbook

    ' Close the new saved workbook (in this line couldn't figure out if you wanted to close the new or the old workbook
    targetWorkbook.Close  ' savechanges:=False


End Sub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 VBA复制范围从工作簿,并粘贴到电子邮件? - vba copy range from workbook and paste into email? VBA复制到新工作簿并保存 - VBA to copy into a new workbook and save Excel VBA 添加新工作簿并复制/粘贴 - Excel VBA Add new Workbook and Copy/Paste 如何运行 VBA 代码从活动工作表中复制一系列单元格并将其粘贴到新工作簿中(只有值和格式,没有公式)? - How to run a VBA code that copies a range of cells from an active sheet and paste it in a new workbook (only values and formatting and no formulates)? VBA复制和粘贴工作簿中每个工作表的值 - VBA to copy and paste values for each sheet in workbook 在单独的工作簿中具有宏以查找工作簿,将其作为值从工作表中复制并粘贴到新工作簿中,并保存在原始工作簿的位置 - Have macro in separate workbook to locate workbook, copy and paste as values from worksheet into new workbook and save in original workbook's location 如何复制特定的单元格并将其粘贴到新工作簿 - How to copy specific cells and paste to a new workbook 仅将过滤后的数据复制并粘贴到新工作簿 - Copy and paste only filtered data to new workbook VBA 循环将特定列的范围复制并粘贴到另一个工作簿多次 - VBA loop to copy & paste range of specific columns to another workbook multiple times VBA-粘贴特殊值并转置到新工作簿中 - VBA - paste special values and transpose into a new workbook
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM