2016-05-31 75 views
0

目前我有一個excel宏,它在運行時會逐個單元格放在一列中,只從段落長度的字符串中提取粗體字。它將在字符串中找到的所有粗體字放在相鄰的單元格中。例如...單元格A1是一段文字,其中一些單詞用粗體顯示,宏運行時,所有在A1中發現的加粗單詞都放在單元格B1中。它的效果很好,只是所有粗體字都放在一個長字符串中,每個粗體字之間沒有空格。我需要幫助創建一個分隔符來放置在字符串中找到的所有粗體字之間。謝謝!使用Excel VBA添加Delimeter格式化數據提取

Sub GetBoldedInfo() 
    Dim txt As String 
    boldedRow = Cells(Rows.Count, "A").End(xlUp).Row 

    For Each c In Range(ActiveCell, ActiveCell.End(xlDown)) 
     myboldtext = "" 
    For i = 1 To Len(c.Value) 
     If c.Characters(Start:=i, Length:=1).Font.FontStyle = "Bold" Then 
      txt = Mid(c.Value, i, 1) 
    'text in bold 
      myboldtext = myboldtext & txt 
     End If 
    Next 
    c.Offset(0, 1) = myboldtext 
    Next 
End Sub 

B1輸出:

China – ABC:NIKEAccount # 1234567890RetailFreight - Ocean1 potential annual shipmentsannual revenue of US $1 

理想B1輸出:

China – ABC:;NIKE;Account # 1234567890;Retail;Freight - Ocean;1 potential annual shipment;annual revenue of US $1 

真的理想輸出:通過細胞在相鄰列分隔。

B1 = China – ABC: C1 = NIKE D1 = Account # 1234567890 etc. 

回答

1

嘗試類似這樣的事情。

更換

If c.Characters(Start:=i, Length:=1).Font.FontStyle = "Bold" Then 
     txt = Mid(c.Value, i, 1) 
'text in bold 
     myboldtext = myboldtext & txt 
    End If 

隨着

If c.Characters(Start:=i, Length:=1).Font.FontStyle = "Bold" Then 
     If c.Characters(Start:=i + 1, Length:=1).Font.FontStyle <> "Bold" Then 
'text in bold with delimiter 
      txt = Mid(c.Value, i, 1) 
      myboldtext = myboldtext & txt & ";" 
     else 
'text in bold 
      txt = Mid(c.Value, i, 1) 
      myboldtext = myboldtext & txt 
     End If 
    End If 

這應該自動添加一個分隔符時,大膽的字符之後的字符不是大膽。

  • 未測試代碼。提供來展示這個想法。
+0

完美的工作!非常感謝。 – Davey