返回列表 上一主題 發帖

[發問] 請問如何讓出來的文字不同顏色及轉簡體字或者繁體字

[發問] 請問如何讓出來的文字不同顏色及轉簡體字或者繁體字

Worksheets(customer).Cells(cnt, 10).Value =  "發票金額:USD" & Worksheets("Oracle").Cells(I, 18).Value & "          箱數:" & Worksheets("Oracle").Cells(I, 41).Value & "               櫃號:" & Split(Worksheets("Oracle").Cells(I, 42).Value, "/")(0) & "               目的港:" & Worksheets("Oracle").Cells(I, 26).Value ”

請問:
如上的VBA, 如果想出來的效果某些字有顏色,可以嗎?
比如 :
"發票金額:USD" & Worksheets("Oracle").Cells(I, 18).Value & "  這段的文字要紅色
"               目的港:" & Worksheets("Oracle").Cells(I, 26).Value ”這段的文字要藍色

還有可否出來後把所有文件轉簡體字或者繁體字?

回復 1# 198188
換色可利用 Range.Characters ; 繁簡轉換我不會
  1. Sub Test()
  2.   Dim s1 As String, s2 As String
  3.   
  4.   s1 = "發票金額:USD" & Worksheets("Oracle").Cells(i, 18).Value
  5.   s2 = "               目的港:" & Worksheets("Oracle").Cells(i, 26).Value
  6.   s = s1 & "          箱數:" & Worksheets("Oracle").Cells(i, 41).Value & "               櫃號:" & Split(Worksheets("Oracle").Cells(i, 42).Value, "/")(0) & s2
  7.   
  8.   With Worksheets(customer).Cells(cnt, 10)
  9.     .Value = s
  10.     .Characters(InStr(s, s1), Len(s1)).Font.Color = vbRed
  11.     .Characters(InStr(s, s2), Len(s2)).Font.Color = vbBlue
  12.   End With
  13. End Sub
複製代碼

TOP

回復 1# 198188
回復 2# stillfish00
套用 stillfish00 大大的程式碼:
  1. Dim wrdApp As Object

  2. Sub Test()
  3.     Dim s1 As String, s2 As String
  4.   
  5.     Set wrdApp = CreateObject("Word.Document")
  6.     Application.ScreenUpdating = False
  7.    
  8.     s1 = "發票金額:USD" & Worksheets("Oracle").Cells(i, 18).Value
  9.     s2 = "               目的港:" & Worksheets("Oracle").Cells(i, 26).Value
  10.     s = s1 & "          箱數:" & Worksheets("Oracle").Cells(i, 41).Value & "               櫃號:" & Split(Worksheets("Oracle").Cells(i, 42).Value, "/")(0) & s2
  11.   
  12.     With Worksheets(customer).Cells(cnt, 10)
  13.         .Value = s
  14.         .Characters(InStr(s, s1), Len(s1)).Font.Color = vbRed
  15.         .Characters(InStr(s, s2), Len(s2)).Font.Color = vbBlue
  16.    
  17.         ' 1 繁轉簡  0 簡轉繁
  18.         .Value = T_S_Cvt(.Value, 1)
  19.     End With
  20.    
  21.     Application.ScreenUpdating = True
  22.     wrdApp.Close False
  23. End Sub

  24. Public Function T_S_Cvt(strData, bytOption) As String
  25.     With wrdApp
  26.         .Content = strData
  27.         ' 調用 Word TCSCConverter 方法來轉換繁簡體
  28.         .Range.TCSCConverter bytOption, True, True
  29.         T_S_Cvt = .Content
  30.     End With
  31. End Function
複製代碼

TOP

  1. Sub SCHPN()
  2.   Dim I As Double
  3.   Dim cnt As Double
  4.   Dim customer As String
  5.   Dim A As String
  6.   Dim LastRec As Integer
  7.   Dim k As Integer
  8.   Dim j As Integer
  9.   Dim l As Integer
  10.   Dim m As Double
  11.   Dim z As Integer
  12.   Dim y As Integer
  13.   Dim e As Integer
  14.   Dim d As Integer
  15.   Dim f As Integer
  16.   Dim FRng As Range
  17.   Dim s1 As String, s2 As String, s3 As String
  18.   Dim wrdApp As Object

  19.   j = Worksheets("Oracle").Range("A" & Worksheets("Oracle").Rows.Count).End(xlUp).Row
  20.   l = Worksheets("Follower").Range("A" & Worksheets("Follower").Rows.Count).End(xlUp).Row
  21.   z = Worksheets("Rule").Range("B" & Worksheets("Rule").Rows.Count).End(xlUp).Row
  22.   y = 39
  23.   Do
  24.    Set wrdApp = CreateObject("Word.Document")

  25.     Application.ScreenUpdating = False
  26.   f = 0
  27.   d = 0
  28.   e = 0
  29.   cnt = 74  

  30.   A = Worksheets("Rule").Cells(y, 2).Value
  31.   customer = A
  32.   Worksheets("SchPN").Copy After:=Worksheets(Worksheets.Count)
  33.   Worksheets(Worksheets.Count).Name = customer
  34.   Worksheets(customer).Range("H5").Value = Date
  35. For I = 2 To j
  36.   If Worksheets("Oracle").Cells(I, 5).Value = customer And Worksheets("Oracle").Cells(I, 19).Value > 0 And Trim(Worksheets("Oracle").Cells(I, 19).Value) <> "" And Trim(Worksheets("Oracle").Cells(I, 15).Value) = "" And (Trim(Worksheets("Oracle").Cells(I, 20).Value) = Trim(Worksheets("Oracle").Cells(I, 18).Value) Or Trim(Worksheets("Oracle").Cells(I, 20).Value) = "") Then
  37.   If Left((Worksheets("Oracle").Cells(I, 22).Value), 1) = 1 Or Left((Worksheets("Oracle").Cells(I, 22).Value), 1) = 2 Or Left((Worksheets("Oracle").Cells(I, 22).Value), 1) = 3 Or Left((Worksheets("Oracle").Cells(I, 22).Value), 1) = 4 Or Left((Worksheets("Oracle").Cells(I, 22).Value), 1) = 5 Or Left((Worksheets("Oracle").Cells(I, 22).Value), 1) = 6 Or Left((Worksheets("Oracle").Cells(I, 22).Value), 1) = 7 Or Left((Worksheets("Oracle").Cells(I, 22).Value), 1) = 8 Or Left((Worksheets("Oracle").Cells(I, 22).Value), 1) = 9 Then
  38.   Set FRng = Worksheets(customer).Range("A:A").Find(Worksheets("Oracle").Cells(I, 1).Value, lookat:=xlWhole, SearchDirection:=xlPrevious)
  39.   If InStr(UCase(Worksheets("Oracle").Cells(I, 2).MergeArea(1)), "SPOT") = 0 And InStr(UCase(Worksheets("Oracle").Cells(I, 2).MergeArea(1)), "通内斯现货") = 0 Then
  40.   If FRng Is Nothing Then
  41.   Worksheets(customer).Cells(cnt, 1).Value = Worksheets("Oracle").Cells(I, 1).Value
  42.   Worksheets(customer).Cells(cnt, 2).Value = Worksheets("Oracle").Cells(I, 24).Value
  43.   Worksheets(customer).Cells(cnt, 3).Value = Worksheets("Oracle").Cells(I, 14).Value
  44.   If IsError(Application.VLookup(Worksheets(customer).Cells(cnt, 1).Value, Sheets("State").Range("A:B"), 2, False)) Then Worksheets(customer).Cells(cnt, 8).Value = "待通知" Else If Trim(Application.VLookup(Worksheets(customer).Cells(cnt, 1).Value, Sheets("State").Range("A:B"), 2, False)) = "" Then Worksheets(customer).Cells(cnt, 8).Value = "待通知" Else Worksheets(customer).Cells(cnt, 8).Value = Application.VLookup(Worksheets(customer).Cells(cnt, 1).Value, Sheets("State").Range("A:B"), 2, False)
  45.   Worksheets(customer).Cells(cnt, 19).Value = Left((Worksheets("Oracle").Cells(I, 22).Value), 3)
  46.   Worksheets(customer).Cells(cnt, 9).Value = "USD" & Format(Worksheets("Oracle").Cells(I, 19).Value * Worksheets(customer).Cells(cnt, 19).Value, "#.00")
  47.   Worksheets(customer).Cells(cnt, 19).Value = ""
  48.   
  49.   If Left((Worksheets("Oracle").Cells(I, 1).Value), 1) = 8 Then
  50.     s1 = Left((Worksheets("Oracle").Cells(I, 22).Value), 3) & "定金"
  51.     s2 = "          代理:" & Worksheets("Oracle").Cells(I, 10).Value
  52.     s3 = " 美國 "
  53.     S = s1 & "               合同金額:USD" & Worksheets("Oracle").Cells(I, 19).Value & "          目的港:" & Worksheets("Oracle").Cells(I, 26).Value & s2 & s3
  54.     With Worksheets(customer).Cells(cnt, 10)
  55.     .Value = S
  56.     .Characters(InStr(S, s1), Len(s1)).Font.Color = vbRed
  57.     .Characters(InStr(S, s2), Len(s2)).Font.Color = -16777024
  58.     .Characters(InStr(S, s3), Len(s3)).Font.Color = vbBlue
  59.     .Value = T_S_Cvt(.Value, 1)
  60.     End With
  61.   End If
  62.   
  63.   If Left((Worksheets("Oracle").Cells(I, 1).Value), 1) = 2 Then
  64.     s1 = Left((Worksheets("Oracle").Cells(I, 22).Value), 3) & "定金"
  65.     s2 = "          代理:" & Worksheets("Oracle").Cells(I, 10).Value
  66.     s3 = " 瑞士"
  67.     S = s1 & "               合同金額:USD" & Worksheets("Oracle").Cells(I, 19).Value & "          目的港:" & Worksheets("Oracle").Cells(I, 26).Value & s2 & s3
  68.    With Worksheets(customer).Cells(cnt, 10)
  69.    .Value = S
  70.    .Characters(InStr(S, s1), Len(s1)).Font.Color = vbRed
  71.    .Characters(InStr(S, s2), Len(s2)).Font.Color = -16777024
  72.    .Characters(InStr(S, s3), Len(s3)).Font.Color = vbBlue
  73.    .Value = T_S_Cvt(.Value, 1)
  74.    End With
  75.   End If
  76.   
  77.   Worksheets(customer).Cells(cnt, 12).Value = Worksheets("Oracle").Cells(I, 19).Value
  78.   Worksheets(customer).Cells(cnt, 15).Value = Worksheets("Oracle").Cells(I, 22).Value
  79.   Worksheets(customer).Cells(cnt, 11).Value = Worksheets("Oracle").Cells(I, 15).Value
  80.   If IsError(Application.VLookup(Worksheets(customer).Cells(cnt, 1).Value, Sheets("State").Range("A:B"), 2, False)) Then Worksheets(customer).Cells(cnt, 13).Value = Worksheets("Oracle").Cells(I, 28).Value Else Worksheets(customer).Cells(cnt, 13).Value = Application.VLookup(Worksheets(customer).Cells(cnt, 1).Value, Sheets("State").Range("A:C"), 2, False)
  81.   Worksheets(customer).Cells(cnt, 14).Value = Worksheets("Oracle").Cells(I, 27).Value
  82.   If IsError(Application.VLookup(Worksheets(customer).Cells(cnt, 1).Value, Sheets("State").Range("A:B"), 2, False)) Then Worksheets(customer).Cells(cnt, 16).Value = "" Else Worksheets(customer).Cells(cnt, 16).Value = Application.VLookup(Worksheets(customer).Cells(cnt, 1).Value, Sheets("State").Range("A:Q"), 17, False)
  83.   If IsError(Application.VLookup(Worksheets(customer).Cells(cnt, 1).Value, Sheets("State").Range("A:B"), 2, False)) Then Worksheets(customer).Cells(cnt, 17).Value = "" Else Worksheets(customer).Cells(cnt, 17).Value = Application.VLookup(Worksheets(customer).Cells(cnt, 1).Value, Sheets("State").Range("A:E"), 5, False)
  84.   If IsError(Application.VLookup(Worksheets(customer).Cells(cnt, 1).Value, Sheets("State").Range("A:B"), 2, False)) Then Worksheets(customer).Cells(cnt, 18).Value = Worksheets("Oracle").Cells(I, 25).Value Else Worksheets(customer).Cells(cnt, 18).Value = Application.VLookup(Worksheets(customer).Cells(cnt, 1).Value, Sheets("State").Range("A:X"), 24, False)
  85.   Worksheets(customer).Cells(cnt, 19).Value = Worksheets("Oracle").Cells(I, 18).Value
  86.   If Trim(Worksheets("Oracle").Cells(I, 8).Value) = "" Then Worksheets(customer).Cells(cnt, 5).Value = "沒有" Else Worksheets(customer).Cells(cnt, 5).Value = "有"
  87.   If Worksheets(customer).Cells(cnt, 5).Value = "沒有" Then Worksheets(customer).Cells(cnt, 5).Font.Color = vbRed
  88.   Worksheets(customer).Cells(cnt, 4).Value = Worksheets("Oracle").Cells(I, 7).Value
  89.   Worksheets(customer).Cells(cnt, 6).Value = Worksheets("Oracle").Cells(I, 12).Value
  90.   If Trim(Worksheets("Oracle").Cells(I, 27).Value) = "" Then Worksheets(customer).Cells(cnt, 7).Value = "待通知" Else Worksheets(customer).Cells(cnt, 7).Value = Worksheets("Oracle").Cells(I, 27).Value
  91.   With Worksheets(customer).Range(Worksheets(customer).Cells(cnt, 1), Worksheets(customer).Cells(cnt, 10))

  92.         .Borders.LineStyle = 1

  93.         .Borders.LineStyle = 1

  94.         .Borders.ColorIndex = 0

  95.         .BorderAround , 2, 0

  96.     End With
  97.   cnt = cnt + 1
  98.   d = d + 1
  99.   e = e + 1
  100.     End If
  101.    End If
  102.     End If
  103.    End If
  104.     Next I
  105.   Loop Until y > z
  106. Application.ScreenUpdating = True

  107.     wrdApp.Close False
  108. End Sub
  109. Public Function T_S_Cvt(strData, bytOption) As String

  110.     With wrdApp

  111.         .Content = strData

  112.         ' 調用 Word TCSCConverter 方法來轉換繁簡體

  113.         .Range.TCSCConverter bytOption, True, True

  114.         T_S_Cvt = .Content

  115.     End With

  116. End Function
複製代碼
回復 3# c_c_lai

.content = strData 出現RUN-TIME ERROR'424': Object required

TOP

回復 4# 198188
在你目前的程式碼看來,不出錯才怪!
請仔細看看範例,問題出在哪裡?
你呼叫的物件根本不存在啊。

TOP

本帖最後由 c_c_lai 於 2014-7-7 20:26 編輯

回復 4# 198188
想想還是直接給你答案吧!
為了要處理『轉簡體字或者繁體字』,我們引用了
Word 的物件:
  1. Set wrdApp = CreateObject("Word.Document")
複製代碼
但在設定之前,必須要預先宣告一個 wrdApp 物件 (Object),
在你目前的程式碼雖然有宣告此變數:
  1.     Dim wrdApp As Object
複製代碼
但卻忽略了 .Value = T_S_Cvt(.Value, 1) 的 Function Call 內的內容引用,
  1. Public Function T_S_Cvt(strData, bytOption) As String
  2.     With wrdApp
  3.         .Content = strData
  4.         ' 調用 Word TCSCConverter 方法來轉換繁簡體
  5.         .Range.TCSCConverter bytOption, True, True
  6.         T_S_Cvt = .Content
  7.     End With
  8. End Function
複製代碼
結果除了 SCHPN() 外卻找不著 wrdApp 的物件,
到此你已經了解問題之所在了嗎?

如果還不明瞭,請注意你是在何處宣告 wrdApp 的。
目前 wrdApp 只能在  SCHPN() 裏頭可被識別,出了 SCHPN()
外便會產生:
  1. RUN-TIME ERROR '424': Object required
複製代碼
因為 T_S_Cvt() 內無法識別 wrdApp, 所以它才告訴你
『此處需要物件』。
總結,你必須在 SCHPN() 上頭需告:
  1. Dim wrdApp As Object
  2. Sub SCHPN()
複製代碼
而非在 SCHPN() 裏頭宣告;如此 wrdApp 才能成為『外域變數』。
T_S_Cvt() 才得以正確執行。

TOP

回復 4# 198188
再補充說明:

TOP

回復 3# c_c_lai


    明白,已經解決,謝謝

TOP

回復 8# 198188
如果你覺得 wrdApp 無必要宣告為『外域變數』(Global Variable)
那亦可修正成為『區域變數』(Local Variable),但是引用之功能函式
則必須加以修改成,即必須同時帶入 wrdApp 物件 (引入參數):
  1. Public Function T_S_Cvt(wrdApp, strData, bytOption) As String
  2.     With wrdApp
  3.         .Content = strData
  4.         ' 調用 Word TCSCConverter 方法來轉換繁簡體
  5.         .Range.TCSCConverter bytOption, True, True
  6.         T_S_Cvt = .Content
  7.     End With
  8. End Function
複製代碼
如此,便能直接在呼叫它的函式內去直接使用它了。
  1. Sub Test()
  2.     Dim wrdApp As Object
  3.     Dim s1 As String, s2 As String
  4.   
  5.     Set wrdApp = CreateObject("Word.Document")
  6.     Application.ScreenUpdating = False
  7.    
  8.     s1 = "發票金額:USD350.00"
  9.     s2 = "               目的港:台灣基隆港"
  10.     s = s1 & "          箱數:120箱" & "               櫃號:LSK122345P78" & s2
  11.   
  12.     With Worksheets("工作表1").Cells(1, 1)
  13.         .Value = s
  14.         ' 1 繁轉簡  0 簡轉繁
  15.         .Value = T_S_Cvt(wrdApp, .Value, 1)
  16.         ' .Value = T_S_Cvt(wrdApp, .Value, 0)
  17.         
  18.         .Characters(InStr(s, s1), Len(s1)).Font.Color = vbRed
  19.         .Characters(InStr(s, s2), Len(s2)).Font.Color = vbBlue
  20.     End With
  21.    
  22.     Application.ScreenUpdating = True
  23.     wrdApp.Close False
  24. End Sub
複製代碼
祝你順意!

TOP

回復 9# c_c_lai


    我發現用這個方法,會有個問題出來,就是不斷製造一些隱藏的word,每次關機都會顯示要不要儲存word.

TOP

        靜思自在 : 有時當思無時苦,好天要積雨來糧。
返回列表 上一主題