Simple task

LeeLee17

New Member
Joined
Jan 14, 2023
Messages
34
Office Version
  1. 365
Platform
  1. Windows
I would like to copy two cells from sheet 1 ("Today") into sheet2 "Notation" if column O (15) in sheet 1 is not blank. In other words, Sheets("Today").Cells(i,15) <> "" Then
Copy Column A (1) and column O (15) from sheet1 "Today" into sheet2 "Notation" in cells( i, A ) and cells (i, B). It will loop through an unknown number of rows in column O (15). Would really appreciate simple code to get this done. Appreciate your knowledge and time. Thank you!
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Welcome to the board.

Just a quick question. Why will it be an unknown number of rows in Column O?
 
Upvote 0
Also, I should have mentioned that column O in "Today" is a data validation field...
 
Upvote 0
Test this with a copy of your workbook. Assuming that I have understood correctly, it should copy all the relevant rows at once rather than checking every row one at a time.

VBA Code:
Sub Copy_Values()
  With Sheets("Today")
    .AutoFilterMode = False
    With .Range("A1", .Range("O" & Rows.Count).End(xlUp))
      .AutoFilter Field:=15, Criteria1:="?*"
      If .Columns(1).SpecialCells(xlVisible).Count > 1 Then
        Intersect(.Offset(1), .Range("A:A,O:O")).Copy Destination:=Sheets("Notation").Range("B" & Rows.Count).End(xlUp).Offset(1, -1)
      End If
    End With
    .AutoFilterMode = False
  End With
End Sub
 
Upvote 0
Solution
Thanks Peter, did you say that it copies the row? I actually need it to copy from "Today" (sheet1) only the relevant row for column A and column O where column O is not blank- and paste into "Notation" (Sheet2).

So, assuming there are entries in "Today" column O for the first 2 rows (taking into account the two rows used for headers), the cells A3 & O3 would be copied into "Notation" (here there is only one row for a header) and paste into A2 and B2. Then A4 & O4 into A3 and B3 in Notation...I greatly appreciate your time and effort. Thank you!
 
Upvote 0
I suggest that you give the code a try and let us know what went wrong if it does not do what you want.
 
Upvote 0
Good morning, and thank you for taking the time to provide this code. Please let me inform that it does copy the entire row- which extends beyond column "O" and returns the following error message: Run-time error '1004' Application defined or object defined error. The yellow highlighted code starts with Intersect. Ultimately, I need to copy only cells from column A & O when O is not blank. The sheet to copy from is "Today" and the destination sheet is "Notation." Thanks for any kind assistance you can provide.
 
Upvote 0
To clarify the task is:

Copy two non-adjacent cells into another sheet when condition is met​

 
Upvote 0
Please let me inform that it does copy the entire row- which extends beyond column "O" and returns the following error message: Run-time error '1004' Application defined or object defined error.
I can only imagine that you have changed the code somewhat or combined it with other code. Did you copy/paste the code or type it out yourself?
The code can be copied from the forum using this icon at the top right of the code window.
1673905284167.png


The code as posted above does not copy whole rows, it copies the values from column A and O only and pastes them in the Notation sheet.

Here is my sample Today sheet. You will notice that I have made certain varied formatting to the cells you want copied (Colour, Bold, Italics, Underline, font size, alignment.

LeeLee17.xlsm
ABCDEFGHIJKLMNOPQR
1Hdr1Hdr2Hdr3Hdr4Hdr5Hdr6Hdr7Hdr8Hdr9Hdr10Hdr11Hdr12Hdr13Hdr14Hdr15Hdr16Hdr17Hdr18
2A2Data 198Data 1Data 33Data 426Data 165Data 781Data 906Data 476Data 679Data 606Data 658Data 648Data 537wData 692Data 328Data 596
3A3Data 300Data 501Data 701Data 962Data 840Data 201Data 968Data 864Data 178Data 53Data 524Data 203Data 411Data 909Data 321Data 611
4A4Data 433Data 968Data 797Data 446Data 572Data 153Data 613Data 996Data 485Data 645Data 677Data 168Data 598abcData 881Data 565Data 6
5A5Data 23Data 337Data 376Data 996Data 790Data 599Data 69Data 349Data 610Data 227Data 916Data 86Data 381Data 657Data 969Data 928
6A6Data 520Data 958Data 489Data 695Data 937Data 511Data 68Data 720Data 572Data 751Data 735Data 167Data 318klData 855Data 629Data 568
7A7Data 855Data 575Data 722Data 451Data 958Data 885Data 87Data 76Data 329Data 501Data 215Data 735Data 636Data 766Data 273Data 32
8A8Data 733Data 423Data 551Data 330Data 239Data 360Data 305Data 275Data 106Data 783Data 535Data 403Data 100Data 597Data 6Data 256
9A9Data 17Data 426Data 635Data 472Data 823Data 759Data 499Data 170Data 326Data 564Data 699Data 33Data 726zData 622Data 367Data 167
Today


My Notation sheet is completely blank before running the code. After running the code it looks like this.

LeeLee17.xlsm
ABCD
1
2A2w
3A4abc
4A6kl
5A9z
6
Notation


If it is possible that any cells in column O in the 'Today' sheet contain numbers only, then my code would need a slight adjustment (but even so whole rows would not be copied) and I cannot generate the error you are reporting.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,550
Members
449,088
Latest member
davidcom

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