Removing hyperlinks, retaining formulas and format

2019-08-16 00:38发布

问题:

I'm not very good with Excel but I'm going to try and explain my problem. Somehow an excel was created via a Timer and somehow has 100's of invisible hyperlinks spread throughout the sheet. I am trying to find a way to copy from A1:k50 remove all hyperlinks but keep the formulas, values, and format. I found this code online, and I've tried adding HR.PasteSpecial xlPasteFormulas but that doesnt seem to work. Any thoughts/ideas would be much appreciated.

 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

回答1:

(edited)

I believe you will have to copy every property in each cell (hope there's no merged ones, that would cause additional troubles), then delete it's hyperlink, and after that restore the propertyes.

You can record macros to discover all that properties, here is some example with fonts and interior. To discover wich other properties you might need to do that, you will have to start recording a macro, select some cell, change that properties manually, stop recording, and see in generated code what that properties are.

    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

(original) You could simply copy everything manually or automatically (including hyperlinks). And in the new sheet where you paste those things, just delete the hyperlinks using:

Selection.Hyperlinks.Delete



回答2:

I was also wondering why, but upon reading through the lines this code actually works, all you need to do is to follow the note mentioned:

'Remove hyperlinks from selected cells without 'removing the cell formatting.

i.e. Highlight/select the column (or cells) and run the code

Voila, Hyperlinks removed while format retained.

Dennis