首页 > 解决方案 > SaveCopyAs Excel VBA 后不再合并合并的单元格

问题描述

我已经合并了一定范围内的单元格。合并区域的数量因工作表而异,有些有 2 个,有些有 10 个。创建并保存新文件后,所有合并区域会将文本拉回范围中的第一个单元格。我真的想用不同的文件名保存一个精确的硬编码副本。

这是用于保存值和 SaveCopyAs 的代码部分:

Sheets("Send").Visible = True
Sheets.Select
Cells.Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
ActiveSheet.Paste
Application.CutCopyMode = False

Dim thisWb As Workbook, d As Integer

Set thisWb = ActiveWorkbook
d = InStrRev(thisWb.FullName, ".")
'ActiveWorkbook.SaveAs Filename:=Left(thisWb.FullName, d - 1) & "-Prelims" & Mid(thisWb.FullName, d)
Sheets("Send").Visible = False
Dim newFileName As String
newFileName = Left(thisWb.FullName, d - 1) & "-Prelims" & Mid(thisWb.FullName, d)
thisWb.SaveCopyAs Filename:=newFileName

这似乎应该很容易,但我无法在 SO 或其他任何地方找到答案。

标签: excelvba

解决方案


这是您的代码的外观。这对你来说应该更有效率

让我知道是否有任何问题:

Sub test()

Dim thisWb As Workbook, ws As Worksheet, d As Integer, lastRow As Long

Set ws = Sheets("Send")

lastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row      'Finds the bottom populated row

    With ws.Range(ws.Cells(1, 1), ws.Cells(lastRow, 1)) 'This find the bottom of column A
        .Value = .Value                                 'Change to text rather than formula
    End With

Set thisWb = ActiveWorkbook
d = InStrRev(thisWb.FullName, ".")

    Sheets("Send").Visible = False

Dim newFileName As String

    newFileName = Left(thisWb.FullName, d - 1) & "-Prelims" & Mid(thisWb.FullName, d)
    thisWb.SaveCopyAs Filename:=newFileName

End Sub

推荐阅读