Hello, I have written code to copy and paste selected data from excel to a word table. In this I have accomplished A.) identifying the cell I need to copy B.) pasting that cell in a specific row in a word table. C.) type text "Performance Review" in column A of the new data row. D.) changing the font to match the word doc, as well as changing the color of that row of the table.
Here it is:
Now what I am seeking to do is:
1. Write another few lines of code to determine if the data pass or fails our standards (if x is between # and # then pass. If x is outside of # and # then fail) and then type text PASS/FAIL into another cell of the word table. I have failed at this thus far, I have been trying to use IF statements.
2. Is it possible for VBA to identify if a word table cell is already populated with text? and if so, is there a way so that the macro will paste the new data in the cell below?
Here it is:
Code:
Sub CopyAndPaste()
Dim myfile, wdApp As New Word.Application, wDoc As Word.Document
'select truck report file
ChDrive "E:\"
ChDir "E:\WG\TVAL\"
myfile = Application.GetOpenFilename(, , "Browse for Document")
Dim i As Integer
'searches for row with "avg" then selects column E(avg of temperature mean) of that row.
i = Application.Match("Avg", Sheet1.Range("A1:A20"), 0)
'copies the cell
Range("E" & i).Select
Selection.Copy
'makes the file appear
wdApp.Visible = True
Set wDoc = wdApp.Documents.Open(myfile)
With wDoc
'selects the paste range in the performance review table, depending on the set point
If Range("c2") = 22 Then wDoc.Tables(8).Cell(4, 1).Select
If Range("c2") = 5 Then wDoc.Tables(8).Cell(4, 2).Select
If Range("c2") = -20 Then wDoc.Tables(8).Cell(4, 3).Select
'and paste the clipboard contents
wdApp.Selection.Collapse wdCollapseEnd
wdApp.Selection.Paste
wdApp.Selection.Font.Name = "Times New Roman"
wdApp.Selection.Font.Size = 12
wdApp.Selection.Font.Bold = wdToggle
Application.CutCopyMode = False
Application.ScreenUpdating = True
wDoc.Tables(8).Cell(4, 0).Select
wdApp.Selection.TypeText Text:="Performance Review"
wDoc.Tables(8).Rows(4).Select
wdApp.Selection.Shading.Texture = wdTextureNone
wdApp.Selection.Shading.ForegroundPatternColor = wdColorAutomatic
wdApp.Selection.Shading.BackgroundPatternColor = -603914241
End With
wDoc.Save
End Sub
1. Write another few lines of code to determine if the data pass or fails our standards (if x is between # and # then pass. If x is outside of # and # then fail) and then type text PASS/FAIL into another cell of the word table. I have failed at this thus far, I have been trying to use IF statements.
2. Is it possible for VBA to identify if a word table cell is already populated with text? and if so, is there a way so that the macro will paste the new data in the cell below?