VBA copying WRONG cell

klutch

New Member
Joined
Jun 7, 2018
Messages
38
I have this
Code:
Sub CopyAndPaste()
Dim myfile, wdApp As New Word.Application, wdDoc As Word.Document
myfile = Application.GetOpenFilename(, , "Browse for Document")
Dim lrow As Long, rng As Range, cell As Range
    lrow = Cells(Cells.Rows.Count, "A").End(xlUp).Row
    Set rng = Range("A2:A" & lrow)
    For Each cell In rng
        If InStr(1, cell.Value, "avg", vbTextCompare) > 0 Then
            cell(0, 4).Select
            Selection.Copy
        End If
    Next cell
wdApp.Visible = True
Set wdDoc = wdApp.Documents.Open(myfile)
'select the word range you want to paste into
    wdDoc.Bookmarks("PRtable").Select
    'and paste the clipboard contents
    wdApp.Selection.Paste
End Sub
It was working fine yesterday but today it is being difficult.
Here are my objectives.
1. Search column A for the text "avg"
2. Select Column E of that same row
3. Paste selected cell into myfile, bookmark "PRTable" Cell(3,4).

Here is what is happening when I run the code.
1. Copies the wrong cell
2. Pastes into every cell of the table.

Yesterday it was copying the correct cell, but was still pasting into the entire table.
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
What happens if you step through the code using F8?
(btw you don't need to select cells to perform actions on them. You can use this single line of code:
Code:
If InStr(1,cell,"avg",vgTextCompare) > 0 Then cell.Copy
 
Upvote 0
Without altering my code, I am now getting error "User-defined type not defined"
for this line
Code:
, wdapp As New word.Application


Disregard This: For some reason my vb decided to unadd Microsoft Word 16.0 as a reference
 
Last edited:
Upvote 0
cell(0, 4) is column D

cell.offset(, 4) would be column E

If there's more than one occurrence of "avg" in rng you will only be copying the last one.
 
Upvote 0
Ok, I changed my code and am now copying the correct cell.
Code:
Sub CopyAndPaste()
Dim myfile, wdApp As New Word.Application, wdDoc As Word.Document
myfile = Application.GetOpenFilename(, , "Browse for Document")
Dim i As Integer
i = Application.Match("Avg", Sheet1.Range("A1:A20"), 0)
Range("E" & i).Select
Selection.Copy

wdApp.Visible = True
Set wdDoc = wdApp.Documents.Open(myfile)
'select the word range you want to paste into
    wdDoc.Bookmarks("PRtable").Select
    'and paste the clipboard contents
    wdApp.Selection.Paste
End Sub
Now I need to paste into the correct part of the table in word. It is pasting into every single cell where as I need it to specifically be in Cell (3,4) of the bookmarked table
 
Upvote 0
Presumably because you're selecting the entire table and then using Selection.Paste (can't offer a correction as I have no idea how to code in Word).
 
Upvote 0
Cross posted https://www.ozgrid.com/forum/forum/help-forums/excel-vba-macros/1204466-if-statements-using-vba-code

Cross-Posting
While we do not prohibit Cross-Posting on this site, we do ask that you please mention you are doing so and provide links in each of the threads pointing to the other thread (see rule 13 here along with the explanation: Forum Rules).
This way, other members can see what has already been done in regards to a question, and do not waste time working on a question that may already be answered.
 
Upvote 0

Forum statistics

Threads
1,223,098
Messages
6,170,100
Members
452,301
Latest member
QualityAssurance

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top