2016-03-16 80 views
1

我編寫了計算if-then條件值然後出現在消息框中的代碼。如何將消息框導出到MS Word?有沒有一個函數,而不是Msgbox,我可以導出到MS Word?我可以將它從Debug.print轉換爲單詞嗎?信息出現在消息框中,我如何將消息框中的信息導出到MS Word?

Sub Way_EatFresh() 

Dim ws As Worksheet, wsB As Worksheet 
Set ws = Worksheets("Overview") 
Set wsB = Worksheets("Billing Rates") 

Dim trueCount As Integer 
Dim i As Integer 
Dim Cst, Hrs 


For i = 1 To 11 

If ws.Range(Chr(65 + i) & "36").Value = "True" Then 
    trueCount = trueCount + 1 
    Cst = Cst + wsB.Range(Chr(66 + i) & "33").Value 
    Hrs = Hrs + wsB.Range(Chr(66 + i) & "25").Value 
    Scope = Scope + wsB.Range(Chr(66 + i) & "150").Value 
End If 

If ws.Range(Chr(65 + i) & "37").Value = "True" Then 
    trueCount = trueCount + 1 
    Cst = Cst + wsB.Range(Chr(66 + i) & "34").Value 
    Hrs = Hrs + wsB.Range(Chr(66 + i) & "26").Value 
    Scope = Scope + wsB.Range(Chr(66 + i) & "150").Value 
End If 

If ws.Range(Chr(65 + i) & "38").Value = "True" Then 
    trueCount = trueCount + 1 
    Cst = Cst + wsB.Range(Chr(66 + i) & "35").Value 
    Hrs = Hrs + wsB.Range(Chr(66 + i) & "27").Value 
    Scope = Scope + wsB.Range(Chr(66 + i) & "150").Value 
End If 

If ws.Range(Chr(65 + i) & "40").Value = "True" Then 
    trueCount = trueCount + 1 
    Cst = Cst + wsB.Range(Chr(66 + i) & "49").Value 
    Hrs = Hrs + wsB.Range(Chr(66 + i) & "41").Value 
    Scope = Scope + wsB.Range(Chr(66 + i) & "150").Value 
End If 

If ws.Range(Chr(65 + i) & "41").Value = "True" Then 
    trueCount = trueCount + 1 
    Cst = Cst + wsB.Range(Chr(66 + i) & "50").Value 
    Hrs = Hrs + wsB.Range(Chr(66 + i) & "42").Value 
    Scope = Scope + wsB.Range(Chr(66 + i) & "150").Value 
End If 

If ws.Range(Chr(65 + i) & "42").Value = "True" Then 
    trueCount = trueCount + 1 
    Cst = Cst + wsB.Range(Chr(66 + i) & "51").Value 
    Hrs = Hrs + wsB.Range(Chr(66 + i) & "43").Value 
    Scope = Scope + wsB.Range(Chr(66 + i) & "150").Value 
End If 

Next i 

If trueCount > 0 Then 
    MsgBox "Cost: " & Cst _ 
    & vbNewLine & "Hours: " & Hrs _ 
    & vbNewLine & "Scope: " & Scope _ 

End If 

If trueCount = 0 Then 
    MsgBox "Please select engagement components." 
End If 
End Sub 
+0

在哪裏?一份新文件?現有的文件? –

+0

給一個新的word文檔 – CodingNewb

+0

儘管我肯定會解決一個現有的文檔 – CodingNewb

回答

2

我在Word中使用這種類型的代碼爲我工作

Sub demo() 
'If trueCount > 0 Then 
Cst = "1" 
hrs = "12" 
scope = "none" 
    Data = "Cost: " & Cst _ 
    & vbNewLine & "Hours: " & hrs _ 
    & vbNewLine & "Scope: " & scope 
    'MsgBox Data 
    Dim objWord 
    Dim objDoc 
    Set objWord = CreateObject("Word.Application") 
    Set objDoc = objWord.Documents.Add 
    objWord.Visible = True 
    objDoc.Range.Text = Data 
End Sub 
+0

這真是太棒了!!謝謝!謝謝!謝謝! – CodingNewb