【问题标题】:Merged cells are no longer merged after SaveCopyAs Excel VBASaveCopyAs Excel VBA 后不再合并合并的单元格
【发布时间】:2019-03-19 15:50:24
【问题描述】:

我已经合并了一定范围内的单元格。合并区域的数量因工作表而异,有些有 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 或其他任何地方找到答案。

【问题讨论】:

  • F8 遍历代码,直到找到合并单元格变为未合并的点。然后告诉我们。另外,请查看这个关于不使用Selectstackoverflow.com/questions/10714251/…的非常好的资源
  • 我什至不知道F8工具,谢谢!看起来问题出现在 ActiveSheet.Paste 和 Application.CutCopyMode = False 之间
  • 我假设您是这样复制/粘贴的,因为您不希望公式出现在新工作表中?
  • 是的,该文件引用了某些人无权访问的数据库,因此我共享一个硬编码版本,因此没有公式。从代码中删除 ActiveSheet.Paste 似乎解决了我的问题。谢谢@Badja

标签: excel vba


【解决方案1】:

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

如果有什么问题请告诉我:

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

【讨论】:

  • 我应该提一下,您需要更改ws.Cells(lastRow, 1),其中1 是A 列,您需要将其更改为最右边的列
猜你喜欢
  • 2021-05-10
  • 1970-01-01
  • 2013-11-23
  • 2021-06-14
  • 1970-01-01
  • 1970-01-01
  • 2022-08-19
  • 2015-10-12
  • 2019-02-11
相关资源
最近更新 更多