2017-03-23 85 views
0

根據我的觀點,我陷入了一個非常簡單的問題,但是我找不到任何解決方案。用Excel格式化文本創建默認文本框

我想創建一個默認的文本框(插入 - >形狀 - >文本框)與一定的填充顏色(藍色,重音1,更輕80%)和一定的文字(工作完成:[空白段落]發現:[空白段落]結論:[空白段落]),文本框內的文字具有紅色字體顏色並且是粗體字enter image description here。 我在創建這個文本框時試圖記錄一個宏,但是當我運行宏時,我總是收到一條錯誤消息:enter image description here。 因爲我經常需要這個文本框(沒有黑色文本,這只是一個例子),所以如果有一個宏可以附加到我的自定義功能區上,這將非常棒。

我發現使用VBA很難在文本框中更改格式化內容。然而,還有誰有一個想法如何使用VBA完成我的默認文本框?

代碼:

Sub Textbox() 
' 
' Textbox Macro 
' 
' Keyboard Shortcut: Ctrl+Shift+Y 
' 
    ActiveSheet.Shapes.AddTextbox(msoTextOrientationHorizontal, 59.25, 48.75, 292.5 _ 
     , 109.5).Select 
    With Selection.ShapeRange.Fill 
     .Visible = msoTrue 
     .ForeColor.ObjectThemeColor = msoThemeColorText2 
     .ForeColor.TintAndShade = 0 
     .ForeColor.Brightness = 0.8000000119 
     .Transparency = 0 
     .Solid 
    End With 
    DEBUG HERE -> With Selection.ShapeRange(1).TextFrame2.TextRange.Characters(1, 11).Font.Fill 
     .Visible = msoTrue 
     .ForeColor.RGB = RGB(255, 0, 0) 
     .Transparency = 0 
     .Solid 
    End With 
    With Selection.ShapeRange(1).TextFrame2.TextRange.Characters(13, 10).Font.Fill 
     .Visible = msoTrue 
     .ForeColor.RGB = RGB(255, 0, 0) 
     .Transparency = 0 
     .Solid 
    End With 
    With Selection.ShapeRange(1).TextFrame2.TextRange.Characters(24, 11).Font.Fill 
     .Visible = msoTrue 
     .ForeColor.RGB = RGB(255, 0, 0) 
     .Transparency = 0 
     .Solid 
    End With 
    Selection.ShapeRange(1).TextFrame2.TextRange.Characters.Text = _ 
     "Work Done:" & Chr(13) & "" & Chr(13) & "Findings:" & Chr(13) & "" & Chr(13) & "Conclusion:" 
    Selection.ShapeRange(1).TextFrame2.TextRange.Characters(1, 11).ParagraphFormat. _ 
     FirstLineIndent = 0 
    With Selection.ShapeRange(1).TextFrame2.TextRange.Characters(1, 4).Font 
     .Bold = msoTrue 
     .NameComplexScript = "+mn-cs" 
     .NameFarEast = "+mn-ea" 
     .Fill.Visible = msoTrue 
     .Fill.ForeColor.RGB = RGB(255, 0, 0) 
     .Fill.Transparency = 0 
     .Fill.Solid 
     .Size = 11 
     .Name = "+mn-lt" 
    End With 
    With Selection.ShapeRange(1).TextFrame2.TextRange.Characters(5, 7).Font 
     .BaselineOffset = 0 
     .Bold = msoTrue 
     .NameComplexScript = "+mn-cs" 
     .NameFarEast = "+mn-ea" 
     .Fill.Visible = msoTrue 
     .Fill.ForeColor.RGB = RGB(255, 0, 0) 
     .Fill.Transparency = 0 
     .Fill.Solid 
     .Size = 11 
     .Name = "+mn-lt" 
    End With 
    Selection.ShapeRange(1).TextFrame2.TextRange.Characters(12, 1).ParagraphFormat. _ 
     FirstLineIndent = 0 
    With Selection.ShapeRange(1).TextFrame2.TextRange.Characters(12, 1).Font 
     .BaselineOffset = 0 
     .NameComplexScript = "+mn-cs" 
     .NameFarEast = "+mn-ea" 
     .Fill.Visible = msoTrue 
     .Fill.ForeColor.ObjectThemeColor = msoThemeColorDark1 
     .Fill.ForeColor.TintAndShade = 0 
     .Fill.ForeColor.Brightness = 0 
     .Fill.Transparency = 0 
     .Fill.Solid 
     .Size = 11 
     .Name = "+mn-lt" 
    End With 
    Selection.ShapeRange(1).TextFrame2.TextRange.Characters(13, 10).ParagraphFormat _ 
     .FirstLineIndent = 0 
    With Selection.ShapeRange(1).TextFrame2.TextRange.Characters(13, 10).Font 
     .BaselineOffset = 0 
     .Bold = msoTrue 
     .NameComplexScript = "+mn-cs" 
     .NameFarEast = "+mn-ea" 
     .Fill.Visible = msoTrue 
     .Fill.ForeColor.RGB = RGB(255, 0, 0) 
     .Fill.Transparency = 0 
     .Fill.Solid 
     .Size = 11 
     .Name = "+mn-lt" 
    End With 
    Selection.ShapeRange(1).TextFrame2.TextRange.Characters(23, 1).ParagraphFormat. _ 
     FirstLineIndent = 0 
    With Selection.ShapeRange(1).TextFrame2.TextRange.Characters(23, 1).Font 
     .BaselineOffset = 0 
     .NameComplexScript = "+mn-cs" 
     .NameFarEast = "+mn-ea" 
     .Fill.Visible = msoTrue 
     .Fill.ForeColor.ObjectThemeColor = msoThemeColorDark1 
     .Fill.ForeColor.TintAndShade = 0 
     .Fill.ForeColor.Brightness = 0 
     .Fill.Transparency = 0 
     .Fill.Solid 
     .Size = 11 
     .Name = "+mn-lt" 
    End With 
    Selection.ShapeRange(1).TextFrame2.TextRange.Characters(24, 11).ParagraphFormat _ 
     .FirstLineIndent = 0 
    With Selection.ShapeRange(1).TextFrame2.TextRange.Characters(24, 11).Font 
     .BaselineOffset = 0 
     .Bold = msoTrue 
     .NameComplexScript = "+mn-cs" 
     .NameFarEast = "+mn-ea" 
     .Fill.Visible = msoTrue 
     .Fill.ForeColor.RGB = RGB(255, 0, 0) 
     .Fill.Transparency = 0 
     .Fill.Solid 
     .Size = 11 
     .Name = "+mn-lt" 
    End With 
End Sub 
+0

您應該包括錄製的宏代碼。另外,不是運行宏,而是調試它來找出究竟是什麼導致了問題。要進行調試,可以在VBA編輯器中使用STEP INTO(通常是F8)。 –

+0

@AdamVincent:我上傳了我的代碼並在調試的地方插入了一條評論。但是我不知道是什麼原因導致了這個問題。 –

+4

您的宏正在嘗試格式化文本,但該文本框中沒有文本。 – Rory

回答

1

試試這個:

Sub Textbox() 
' 
' Textbox Macro 
' 
' Keyboard Shortcut: Ctrl+Shift+Y 
' 
    With ActiveSheet.Shapes.AddTextbox(msoTextOrientationHorizontal, 59.25, 48.75, 292.5, 109.5) '<--| add and reference a new shape 
     With .Fill '<--| reference referenced shape 'Fill' property 
      .Visible = msoTrue 
      .ForeColor.ObjectThemeColor = msoThemeColorText2 
      .ForeColor.TintAndShade = 0 
      .ForeColor.Brightness = 0.8000000119 
      .Transparency = 0 
      .Solid 
     End With 
     With .TextFrame2 '<--| reference referenced shape 'TextFrame2' property 
      .TextRange.Characters.Text = "Work Done:" & Chr(13) & "" & Chr(13) & "Findings:" & Chr(13) & "" & Chr(13) & "Conclusion:" 
      With .TextRange.Characters(1, 11).Font.Fill 
       .Visible = msoTrue 
       .ForeColor.RGB = RGB(255, 0, 0) 
       .Transparency = 0 
       .Solid 
      End With 
      With .TextRange.Characters(13, 10).Font.Fill 
       .Visible = msoTrue 
       .ForeColor.RGB = RGB(255, 0, 0) 
       .Transparency = 0 
       .Solid 
      End With 
      With .TextRange.Characters(24, 11).Font.Fill 
       .Visible = msoTrue 
       .ForeColor.RGB = RGB(255, 0, 0) 
       .Transparency = 0 
       .Solid 
      End With 
      .TextRange.Characters(1, 11).ParagraphFormat.FirstLineIndent = 0 
      With .TextRange.Characters(1, 4).Font 
       .BaselineOffset = 0 
       .Bold = msoTrue 
       .NameComplexScript = "+mn-cs" 
       .NameFarEast = "+mn-ea" 
       .Fill.Visible = msoTrue 
       .Fill.ForeColor.RGB = RGB(255, 0, 0) 
       .Fill.Transparency = 0 
       .Fill.Solid 
       .Size = 11 
       .Name = "+mn-lt" 
      End With 
      With .TextRange.Characters(5, 7).Font 
       .BaselineOffset = 0 
       .Bold = msoTrue 
       .NameComplexScript = "+mn-cs" 
       .NameFarEast = "+mn-ea" 
       .Fill.Visible = msoTrue 
       .Fill.ForeColor.RGB = RGB(255, 0, 0) 
       .Fill.Transparency = 0 
       .Fill.Solid 
       .Size = 11 
       .Name = "+mn-lt" 
      End With 
     End With 
    End With 
End Sub 
+0

這工作也很好!非常感謝您的幫助! –

+0

不客氣。您可能希望將答案標記爲已接受謝謝 – user3598756

0

這是我到底最終代碼:

Sub Textbox() ' ' Textbox Macro ' ' Keyboard Shortcut: Ctrl+Shift+Y ' ActiveSheet.Shapes.AddTextbox(msoTextOrientationHorizontal, 59.25, 48.75, 292.5 _ , 109.5).Select Selection.ShapeRange(1).TextFrame2.TextRange.Characters.Text = _ "Work Done:" & Chr(13) & "" & Chr(13) & "Findings:" & Chr(13) & "" & Chr(13) & "Conclusion:" Selection.ShapeRange(1).TextFrame2.TextRange.Characters(1, 11).ParagraphFormat. _ FirstLineIndent = 0 With Selection.ShapeRange.Fill .Visible = msoTrue .ForeColor.ObjectThemeColor = msoThemeColorText2 .ForeColor.TintAndShade = 0 .ForeColor.Brightness = 0.8000000119 .Transparency = 0 .Solid End With With Selection.ShapeRange(1).TextFrame2.TextRange.Characters(1, 11).Font.Fill .Visible = msoTrue .ForeColor.RGB = RGB(255, 0, 0) .Transparency = 0 .Solid End With With Selection.ShapeRange(1).TextFrame2.TextRange.Characters(13, 10).Font.Fill .Visible = msoTrue .ForeColor.RGB = RGB(255, 0, 0) .Transparency = 0 .Solid End With With Selection.ShapeRange(1).TextFrame2.TextRange.Characters(24, 11).Font.Fill .Visible = msoTrue .ForeColor.RGB = RGB(255, 0, 0) .Transparency = 0 .Solid End With With Selection.ShapeRange(1).TextFrame2.TextRange.Characters(1, 4).Font .Bold = msoTrue .NameComplexScript = "+mn-cs" .NameFarEast = "+mn-ea" .Fill.Visible = msoTrue .Fill.ForeColor.RGB = RGB(255, 0, 0) .Fill.Transparency = 0 .Fill.Solid .Size = 11 .Name = "+mn-lt" End With With Selection.ShapeRange(1).TextFrame2.TextRange.Characters(5, 7).Font .BaselineOffset = 0 .Bold = msoTrue .NameComplexScript = "+mn-cs" .NameFarEast = "+mn-ea" .Fill.Visible = msoTrue .Fill.ForeColor.RGB = RGB(255, 0, 0) .Fill.Transparency = 0 .Fill.Solid .Size = 11 .Name = "+mn-lt" End With Selection.ShapeRange(1).TextFrame2.TextRange.Characters(12, 1).ParagraphFormat. _ FirstLineIndent = 0 With Selection.ShapeRange(1).TextFrame2.TextRange.Characters(12, 1).Font .BaselineOffset = 0 .NameComplexScript = "+mn-cs" .NameFarEast = "+mn-ea" .Fill.Visible = msoTrue .Fill.ForeColor.ObjectThemeColor = msoThemeColorDark1 .Fill.ForeColor.TintAndShade = 0 .Fill.ForeColor.Brightness = 0 .Fill.Transparency = 0 .Fill.Solid .Size = 11 .Name = "+mn-lt" End With Selection.ShapeRange(1).TextFrame2.TextRange.Characters(13, 10).ParagraphFormat _ .FirstLineIndent = 0 With Selection.ShapeRange(1).TextFrame2.TextRange.Characters(13, 10).Font .BaselineOffset = 0 .Bold = msoTrue .NameComplexScript = "+mn-cs" .NameFarEast = "+mn-ea" .Fill.Visible = msoTrue .Fill.ForeColor.RGB = RGB(255, 0, 0) .Fill.Transparency = 0 .Fill.Solid .Size = 11 .Name = "+mn-lt" End With Selection.ShapeRange(1).TextFrame2.TextRange.Characters(23, 1).ParagraphFormat. _ FirstLineIndent = 0 With Selection.ShapeRange(1).TextFrame2.TextRange.Characters(23, 1).Font .BaselineOffset = 0 .NameComplexScript = "+mn-cs" .NameFarEast = "+mn-ea" .Fill.Visible = msoTrue .Fill.ForeColor.ObjectThemeColor = msoThemeColorDark1 .Fill.ForeColor.TintAndShade = 0 .Fill.ForeColor.Brightness = 0 .Fill.Transparency = 0 .Fill.Solid .Size = 11 .Name = "+mn-lt" End With Selection.ShapeRange(1).TextFrame2.TextRange.Characters(24, 11).ParagraphFormat _ .FirstLineIndent = 0 With Selection.ShapeRange(1).TextFrame2.TextRange.Characters(24, 11).Font .BaselineOffset = 0 .Bold = msoTrue .NameComplexScript = "+mn-cs" .NameFarEast = "+mn-ea" .Fill.Visible = msoTrue .Fill.ForeColor.RGB = RGB(255, 0, 0) .Fill.Transparency = 0 .Fill.Solid .Size = 11 .Name = "+mn-lt" End With End Sub

+0

絕對應該與您分享最終代碼,但適當的位置是在原始問題中添加一個新部分 –