Results 1 to 9 of 9

Thread: Help with writing this code.

  1. #1
    New Member
    Join Date
    May 12
    Posts
    5

    Smile Help with writing this code.

    Hi All,

    I've just got to grips with the basics of VBA but I need your expertise with a particular code. Looking at the data below, I would like the code to be written so if the the answer is yes is Column D (contractor info) I want the entire row to be cut and added to sheet 2. Sheet 2 would have the same headings. there will be no blanks at all.

    District Work Request Audit Result Contractor Info provided
    21 39NGWS D4 yes
    81 01MNRK D4 No
    81 01MQQZ D5 No
    81 01MSCF D7 Yes
    12 37JZLW D8 Yes

    I'd be grateful if someone could help me with this.

    thanks in advance

  2. #2
    New Member
    Join Date
    May 12
    Posts
    5

    Re: Help with writing this code.

    sorry the format changed so it would be something like this

    District WR AR Contractor info
    12 3654774LQ D1 Yes

  3. #3
    PowerPoster
    Join Date
    Dec 04
    Posts
    18,524

    Re: Help with writing this code.

    try like
    vb Code:
    1. dim sumrows as range, c as range
    2. nextrow = sheets("sheet2").cells(sheets("sheet2").rows.count, 1).end(xlup).row + 1
    3.  
    4. for each c in range("d:d")
    5.    if isempty(c) then exit for    ' finish on empty cell
    6.    if c.value = "Yes" then
    7.       if sumrows is nothing then
    8.           set sumrows = c.entirerow
    9.          else
    10.           set sumrows = union(sumrows, c.entirerow)
    11.       end if
    12.    end if
    13. next
    14. sumrows.Copy Sheets("sheet2").Range("a"  & nextrow)
    15. sumrows.Delete xlShiftUp
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  4. #4
    New Member
    Join Date
    May 12
    Posts
    5

    Re: Help with writing this code.

    Quote Originally Posted by westconn1 View Post
    try like
    vb Code:
    1. dim sumrows as range, c as range
    2. nextrow = sheets("sheet2").cells(sheets("sheet2").rows.count, 1).end(xlup).row + 1
    3.  
    4. for each c in range("d:d")
    5.    if isempty(c) then exit for    ' finish on empty cell
    6.    if c.value = "Yes" then
    7.       if sumrows is nothing then
    8.           set sumrows = c.entirerow
    9.          else
    10.           set sumrows = union(sumrows, c.entirerow)
    11.       end if
    12.    end if
    13. next
    14. sumrows.Copy Sheets("sheet2").Range("a"  & nextrow)
    15. sumrows.Delete xlShiftUp
    Thank you very much for this.

    I copied and pasted this in the workbook but to no avail. Please forgive my ignorance but I'm unable to run this macro when I press F5. As I said I'm a novice so please be patient with me.

  5. #5
    Addicted Member
    Join Date
    Jul 09
    Posts
    208

    Re: Help with writing this code.

    Put the posted code within Sub MyCode() and End Sub lines in a module. Run it by pressing F5 within the Visual Basic editor, or from the workbook via Tools -> Macro menu.

  6. #6
    New Member
    Join Date
    May 12
    Posts
    5

    Re: Help with writing this code.

    Quote Originally Posted by His Nibbs View Post
    Put the posted code within Sub MyCode() and End Sub lines in a module. Run it by pressing F5 within the Visual Basic editor, or from the workbook via Tools -> Macro menu.
    Hi,

    I did and it came with the following error:

    Runtime Error '450'

    wrong number of arguments or invalid property assignment

    please let me know.

  7. #7
    PowerPoster
    Join Date
    Dec 04
    Posts
    18,524

    Re: Help with writing this code.

    wrong number of arguments or invalid property assignment
    dunno, which line?
    i tested before posting, show the code as you have it
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  8. #8
    New Member
    Join Date
    May 12
    Posts
    5

    Re: Help with writing this code.

    Quote Originally Posted by westconn1 View Post
    dunno, which line?
    i tested before posting, show the code as you have it
    This is what it looks like.
    Attached Files Attached Files

  9. #9
    PowerPoster
    Join Date
    Dec 04
    Posts
    18,524

    Re: Help with writing this code.

    it would help if you answered the question
    which line?
    though from your picture you have no sheet with name of sheet2, change code to suit
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •