Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 删除超链接,保留公式和格式_Excel_Hyperlink_Format_Formula - Fatal编程技术网

Excel 删除超链接,保留公式和格式

Excel 删除超链接,保留公式和格式,excel,hyperlink,format,formula,Excel,Hyperlink,Format,Formula,我对Excel不是很在行,但我将尝试解释我的问题。不知怎的,一个excel是通过计时器创建的,不知怎的,它有100个不可见的超链接散布在整个工作表中。我试图找到一种从A1:k50复制的方法,删除所有超链接,但保留公式、值和格式。我在网上找到了这段代码,并尝试添加HR.paste公式,但这似乎不起作用。如有任何想法/想法,将不胜感激 Sub RemoveHlinks() 'Remove hyperlinks from selected cells without 'removing the ce

我对Excel不是很在行,但我将尝试解释我的问题。不知怎的,一个excel是通过计时器创建的,不知怎的,它有100个不可见的超链接散布在整个工作表中。我试图找到一种从A1:k50复制的方法,删除所有超链接,但保留公式、值和格式。我在网上找到了这段代码,并尝试添加HR.paste公式,但这似乎不起作用。如有任何想法/想法,将不胜感激

 Sub RemoveHlinks()
'Remove hyperlinks from selected cells without
'removing the cell formatting.
Dim Hlink      As Hyperlink
Dim HR         As Range
Dim Temp       As Range
Dim MaxCol     As Integer

With ActiveSheet.UsedRange
   MaxCol = .Column + .Columns.Count
End With

Set Temp = Cells(1, MaxCol)

For Each Hlink In Selection.Hyperlinks
 Set HR = Hlink.Range
 HR.Copy Destination:=Temp
 HR.ClearContents
 Set Temp = Temp.Resize(HR.Rows.Count, HR.Columns.Count)
 Temp.Copy
 HR.PasteSpecial xlPasteFormats
 HR.PasteSpecial xlPasteValues
 Temp.Clear
Next Hlink

End Sub
(编辑)

我相信您必须复制每个单元格中的每个属性(希望没有合并的属性,这会造成额外的麻烦),然后删除它的超链接,然后恢复属性Yes

您可以录制宏来发现所有这些属性,下面是一些字体和内部的示例。要发现可能需要的其他属性,您必须开始录制宏,选择一些单元格,手动更改该属性,停止录制,并在生成的代码中查看这些属性是什么

    Sub Macro1()
    '
    ' Macro1 Macro
    '


        Dim Cell As Range
        Dim SelectedRange As Range

        Set SelectedRange = ActiveSheet.Range("A1:K50")

        Dim Rows As Integer
        Dim Columns As Integer
        Dim i As Integer
        Dim j As Integer


        Rows = SelectedRange.Rows.Count
        Columns = SelectedRange.Columns.Count

        For i = 1 To Rows
            For j = 1 To Columns
                Set Cell = SelectedRange.Cells(i, j)
                Call ClearHyperlinks(Cell)
            Next
        Next

    End Sub


    Sub ClearHyperlinks(Cell As Range)
        '''''''''' Font Properties''''''''''''''

        Dim fName As Variant
        Dim fFontStyle As Variant
        Dim fSize As Variant
        Dim fStrikethrough As Variant
        Dim fSuperscript As Variant
        Dim fSubscript As Variant
        Dim fOutlineFont As Variant
        Dim fShadow As Variant
        Dim fUnderline As Variant
        Dim fThemeColor As Variant
        Dim fTintAndShade As Variant
        Dim fThemeFont As Variant

        With Cell.Font
            fName = .Name
            fFontStyle = .FontStyle
            fSize = .Size
            fStrikethrough = .Strikethrough
            fSuperscript = .Superscript
            fSubscript = .Subscript
            fOutlineFont = .OutlineFont
            fShadow = .Shadow
            fUnderline = .Underline
            fThemeColor = .ThemeColor
            fTintAndShade = .TintAndShade
            fThemeFont = .ThemeFont
        End With



        ''''''''''Interior Properties''''''''''''''

        Dim iPattern As Variant
        Dim iPatternColorIndex As Variant
        Dim iThemeColor As Variant
        Dim iTintAndShade As Variant
        Dim iPatternTintAndShade As Variant

        With Cell.Interior
            iPattern = .Pattern
            iPatternColorIndex = .PatternColorIndex
            iThemeColor = .ThemeColor
            iTintAndShade = .TintAndShade
            iPatternTintAndShade = .PatternTintAndShade
        End With


        ''''''''''''' Number Format '''''''''
        Dim NumberFormat As Variant
        NumberFormat = Cell.NumberFormat

        '''''''''''''' Delete Hyeperlinks
        Cell.Hyperlinks.Delete



        ''''''''''''''''''Restore properties'''''''''''''''

        Cell.NumberFormat = NumberFormat


        With Cell.Font
            .Name = fName
            .FontStyle = fFontStyle
            .Size = fSize
            .Strikethrough = fStrikethrough
            .Superscript = fSuperscript
            .Subscript = fSubscript
            .OutlineFont = fOutlineFont
            .Shadow = fShadow
            .Underline = fUnderline
            .ThemeColor = fThemeColor
            .TintAndShade = fTintAndShade
            .ThemeFont = fThemeFont
        End With

        With Cell.Interior
            .Pattern = iPattern
            .PatternColorIndex = iPatternColorIndex
            .ThemeColor = iThemeColor
            .TintAndShade = iTintAndShade
            .PatternTintAndShade = iPatternTintAndShade
        End With


    End Sub
(原件) 您可以简单地手动或自动复制所有内容(包括超链接)。 在粘贴这些内容的新工作表中,只需使用以下命令删除超链接:


Selection.Hyperlinks.Delete

我也在想为什么,但是在通读了这些代码之后,您只需要按照上面提到的注释进行操作:

'从选定的单元格中删除超链接,而不删除 '正在删除单元格格式设置

i、 e.突出显示/选择列(或单元格)并运行代码

瞧,超链接被删除,而格式被保留


Dennis

当使用selection.hyperlinks.delete时,它会删除单元格的格式(粗体、背景色等),这就是为什么它不起作用的原因。好吧,这一个可行,但可能会导致合并单元格或一次占用多个单元格的超链接出现问题。