2013-03-19 89 views
0

我對Excel不太好,但我會嘗試解釋我的問題。不知何故,通過計時器創建了一個excel,並以某種方式在整個表格中傳播了100多個不可見的超鏈接。我試圖找到一種方法從A1複製:k50刪除所有超鏈接,但保留公式,值和格式。我在網上發現了這個代碼,並且我嘗試添加HR.PasteSpecial xlPasteFormulas,但是這似乎不起作用。任何想法/想法將不勝感激。刪除超鏈接,保留公式和格式

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 

回答

0

(編輯)

我相信你將不得不每個屬性中的每個單元格複製(希望沒有合併的,這會導致額外的麻煩),然後刪除它的超級鏈接,之後恢復propertyes。

您可以錄製宏發現所有的屬性,下面是一些例子字體和內飾。爲了發現其他屬性,您可能需要這樣做,您將不得不開始錄製宏,選擇某個單元格,手動更改該屬性,停止錄製以及在生成的代碼中查看該屬性是什麼。

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

+0

當使用selection.hyperlinks.delete它刪除單元格(大膽,背景顏色等),這就是爲什麼這是行不通的格式。 – John 2013-03-19 20:19:31

+0

嗯,這個可以工作,但可能會導致合併單元格的問題或超時鏈接一次佔用很多單元格的問題。 – 2013-03-19 21:29:10

+0

(我發誓當我發佈代碼時縮進了代碼) – 2013-03-19 21:29:43

0

我也想知道爲什麼,但在通過這個代碼實際工作行閱讀,你需要做的就是按照提到的注意事項:

「從選擇細胞刪除鏈接,而不 」刪除單元格格式。

即高亮顯示/選擇列(或細胞)和運行該代碼

瞧,超鏈接去除,而保留格式。

丹尼斯