Optimization 复制工作簿时,在整个工作簿中保留彩色高亮显示的公式

Optimization 复制工作簿时,在整个工作簿中保留彩色高亮显示的公式,optimization,excel,excel-2010,vba,Optimization,Excel,Excel 2010,Vba,“我已经尝试使条件起作用,但没有结果,必须有一种方法将符合条件的单元格粘贴为格式(单元格颜色为RGB(128,128,128)),否则粘贴所有值,下面的编码不起作用,任何我希望我的问题这次会被接受,任何帮助都将受到高度赞赏! 请在DropBox链接>>>中找到我的Excel工作簿。” 在下面对代码的评论中,我假设您只希望复制指定单元格的公式,然后仅在这些单元格的格式为灰色时复制公式 Sub CopyPasteSave() . . . 1. CellsToCopy

“我已经尝试使条件起作用,但没有结果,必须有一种方法将符合条件的单元格粘贴为格式(单元格颜色为RGB(128,128,128)),否则粘贴所有值,下面的编码不起作用,任何我希望我的问题这次会被接受,任何帮助都将受到高度赞赏! 请在DropBox链接>>>中找到我的Excel工作簿。”


在下面对代码的评论中,我假设您只希望复制指定单元格的公式,然后仅在这些单元格的格式为灰色时复制公式

    Sub CopyPasteSave()

     . . .    

1.  CellsToCopy = Split(("B11,B12"), ",")

   <This use of split to assign array elements does not work. Better
    to go with CellsToCopy = Array("B11", "B12").>

    . . .

    For Each ws In wbTarget.Worksheets
       With ws

2.        .Cells.Copy        
          .[A1].PasteSpecial Paste:=xlValues

         <You have lost your reference to wbSource. Better written as
          wbSource.Worksheets(ws.Name).Cells.Copy and wbSource.Worksheets(ws.Name).
          [A1].PasteSpecialPaste:=xlValues. (I am assuming the reference to
          [A1] will work.)>

2.        Set acell = wbSource.workbook

         <You have declared acell as a String, but are trying to assign a 
          workbook to it. To iterate across all the cells in the source workbook, 
          you'll have to do it sheet-by-sheet.>

3.        Do While Not IsEmpty(acell)

         <Misplaced?>

4.        If acell.Interior.ColorIndex = 48 Then '-- make sure color index is correct
             For i = LBound(CellsToCopy) To UBound(CellsToCopy)
                wbSource.Worksheets(ws.Name).Range(CellsToCopy(i)).Copy
                ws.Range(CellsToCopy(i)).PasteSpecial xlPasteFormulas
             Next i
          End If

          <Would suggest that you move the Empty and color test into the For 
           ... Next loop. Also, no need to specifically reference ws, as you are
           already in With ws...End With.>
子复制粘贴保存()
. . .    
1.CellsToCopy=拆分((“B11,B12”),“,”)
. . .
对于wbTarget.工作表中的每个ws
与ws
2.细胞,复制
[A1]。粘贴特殊粘贴:=xlValues
2.设置acell=wbSource.workbook
“感谢Chuff提供宝贵的通知,现在它正在工作。我修改了下面的代码,但复制工作表时速度有点慢!”

子复制粘贴保存()
将wbSource设置为Excel.工作簿
将wbTarget设置为Excel.工作簿
名称为Dim nm
将ws设置为工作表
将路径设置为字符串
变暗rcell As范围
暗淡单元格作为范围
如果MsgBox(“将特定工作表复制到新工作簿”&vbCr&_
“新工作表将作为值粘贴,命名范围已删除”_
vbYesNo,“NewCopy”)=vbNo然后
出口接头
如果结束
设置wbSource=ActiveWorkbook
设置rcell=wbSource.Worksheets(“EPF每日报告”)。范围(“I5”)
Application.ScreenUpdating=False
Application.DisplayAlerts=False
'复制特定的工作表
'*在下面设置要复制的图纸名称*
'工作表名称放在引号内,用逗号分隔
关于错误转到错误捕获器
工作表(数组(“入口歧管”、“分隔符”_
“原油汽提塔和再沸器”、“水汽提塔和再沸器”_
“原油储存和出口”、“GSU、火炬和发电机”、“EPF公用设施”_
“EPF每日报告”、“节流阀尺寸”)。副本
错误转到0
'将图纸粘贴为值
'删除外部链接、超链接和硬代码公式
'确保在所有图纸上选择A1
设置wbTarget=ActiveWorkbook
对于wbTarget.工作表中的每个ws
与ws
对于.UsedRange中的每个单元格
如果cell.Interior.Color RGB(192192192),则
如果cell.HasArray那么
使用cell.CurrentArray
.Value=.Value'清除数组
以
其他的
cell.Value=cell.Value
如果结束
如果结束
下一个细胞
.Hyperlinks.Delete
以
下一个ws
有wbTarget
'删除命名范围
对于每个nm In.名称
删除
下一纳米
Path=“C:\”
.SaveAs文件名:=路径和“&”EPF每日报告“&”&rcell.Value和“.xls”
.Close SaveChanges:=False
以
出口点:
Application.ScreenUpdating=True
Application.DisplayAlerts=True
Application.Visible=False
出口接头
错误捕捉器:
MsgBox“此工作簿中不存在指定的工作表”
恢复退出点
端接头

与其复制整个工作簿,不如相应地对单元格应用条件格式。这比尝试你想做的事情要灵活得多。再经过几次接近票数的投票,问题就不复存在了$是的,但我正在尝试按我选择的颜色将XlFormats与彩色单元格粘贴在一起,这样就很容易选择保存公式和粘贴值的位置。
    Sub CopyPasteSave()

     . . .    

1.  CellsToCopy = Split(("B11,B12"), ",")

   <This use of split to assign array elements does not work. Better
    to go with CellsToCopy = Array("B11", "B12").>

    . . .

    For Each ws In wbTarget.Worksheets
       With ws

2.        .Cells.Copy        
          .[A1].PasteSpecial Paste:=xlValues

         <You have lost your reference to wbSource. Better written as
          wbSource.Worksheets(ws.Name).Cells.Copy and wbSource.Worksheets(ws.Name).
          [A1].PasteSpecialPaste:=xlValues. (I am assuming the reference to
          [A1] will work.)>

2.        Set acell = wbSource.workbook

         <You have declared acell as a String, but are trying to assign a 
          workbook to it. To iterate across all the cells in the source workbook, 
          you'll have to do it sheet-by-sheet.>

3.        Do While Not IsEmpty(acell)

         <Misplaced?>

4.        If acell.Interior.ColorIndex = 48 Then '-- make sure color index is correct
             For i = LBound(CellsToCopy) To UBound(CellsToCopy)
                wbSource.Worksheets(ws.Name).Range(CellsToCopy(i)).Copy
                ws.Range(CellsToCopy(i)).PasteSpecial xlPasteFormulas
             Next i
          End If

          <Would suggest that you move the Empty and color test into the For 
           ... Next loop. Also, no need to specifically reference ws, as you are
           already in With ws...End With.>
Sub CopyPasteSave()
    Dim wbSource As Excel.Workbook
    Dim wbTarget As Excel.Workbook
    Dim nm As Name
    Dim ws As Worksheet
    Dim Path As String
    Dim rcell As Range
    Dim cell As Range

    If MsgBox("Copy specific sheets to a new workbook" & vbCr & _
            "New sheets will be pasted as values, named ranges removed", _
            vbYesNo, "NewCopy") = vbNo Then
        Exit Sub
    End If
    Set wbSource = ActiveWorkbook

    Set rcell = wbSource.Worksheets("EPF Daily Report").Range("I5")
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    ' Copy specific sheets
    ' *SET THE SHEET NAMES TO COPY BELOW*
    ' Sheet names go inside quotes, separated by commas
    On Error GoTo ErrCatcher
    wbSource.Worksheets(Array("InletManifold", "Separator", _
        "Crude Strippers & Reboilers ", "Water Strippers  & Reboilers ", _
        "Crude Storage&Export", "GSU,FLARE & GEN", "EPF Utility", _
        "EPF Daily Report", "Choke Size")).Copy
    On Error GoTo 0
    ' Paste sheets as values
    ' Remove External Links, Hyperlinks and hard-code formulas
    ' Make sure A1 is selected on all sheets
    Set wbTarget = ActiveWorkbook
    For Each ws In wbTarget.Worksheets
        With ws
            For Each cell In .UsedRange
                If cell.Interior.Color <> RGB(192, 192, 192) Then
                    If cell.HasArray Then
                        With cell.CurrentArray
                       .Value = .Value 'clearing array
                        End With
                       Else
                       cell.Value = cell.Value
                    End If
                End If
            Next cell
            .Hyperlinks.Delete
        End With
    Next ws
    With wbTarget
        ' Remove named ranges
        For Each nm In .Names
            nm.Delete
        Next nm
        Path = "C:\"
        .SaveAs Filename:=Path & " " & "EPF Daily Report" & " " & rcell.Value & ".xls"
        .Close SaveChanges:=False
    End With

Exit_Point:
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
Application.Visible = False
    Exit Sub

ErrCatcher:
    MsgBox "specified sheets do not exist within this work book"
    Resume Exit_Point
End Sub