+ Reply to Thread
Results 1 to 5 of 5

Copy And Paste Row Into New Worksheet Based On User Listbox Selection

  1. #1
    Registered User
    Join Date
    10-03-2012
    Location
    Melbourne, Australia
    MS-Off Ver
    Excel 2007
    Posts
    6

    Copy And Paste Row Into New Worksheet Based On User Listbox Selection

    Hi All,

    Very new to VBA here and I am having a bit of trouble with this.

    I have created a UserForm which has a list box that contains dates of various "jobs" to be completed. The details of each job are contained within a row (i.e. row 1 contains all information for job 112; row 2 contains all information for job 113 etc.) Column "C" of each row contains the dates that will be selected in the list box.

    I want to arrange it so that when the User selects a specific date, all the "jobs" correlating with that date (i.e. can be more than one job) are selected, copied, and pasted into a new worksheet.

    This is my code at the moment:

    Private Sub cmdViewJob_Click()

    Const DATE_COLUMN As String = "C"
    Dim LastRow As Long
    Dim cell As Range
    Dim strSelectedDate As String

    strSelectedDate = lstJobDate.Value
    JobInformationSheet = ActiveSheet.Name

    With Worksheets(JobInformationSheet)
    LastRow = .Cells(.Rows.Count, DATE_COLUMN).End(xlUp).Row

    For Each cell In Range("C2:C" & LastRow)
    If cell.Value = strSelectedDate Then
    cell.EntireRow.Copy PrintJobs.Cells(LCopyToRow, 1)

    End If
    Next
    End With
    End Sub


    I should add that, as it stands, this code does not give me any errors. It runs fine. It just doesn't select, copy, or paste, anything to the new sheet.

    If you need any more information, just let me know!
    Any help would be greatly appreciated!
    Thanks.

  2. #2
    Registered User
    Join Date
    10-03-2012
    Location
    Melbourne, Australia
    MS-Off Ver
    Excel 2007
    Posts
    6

    Re: Copy And Paste Row Into New Worksheet Based On User Listbox Selection

    Sorry to be a pain, but does anybody have any ideas on this one?

  3. #3
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,420

    Re: Copy And Paste Row Into New Worksheet Based On User Listbox Selection

    I suspect the clue is in the variable types. You have a string variable to accept the date from the form. Fair enough. But a "real" date is a number. So you are comparing a string to a number and they won't match. Try converting the date to a text string using Application.WorksheetFunction.Text(...). You'll need to ensure the format used matches the format of the input.

    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  4. #4
    Registered User
    Join Date
    10-03-2012
    Location
    Melbourne, Australia
    MS-Off Ver
    Excel 2007
    Posts
    6

    Re: Copy And Paste Row Into New Worksheet Based On User Listbox Selection

    Quote Originally Posted by TMShucks View Post
    I suspect the clue is in the variable types. You have a string variable to accept the date from the form. Fair enough. But a "real" date is a number. So you are comparing a string to a number and they won't match. Try converting the date to a text string using Application.WorksheetFunction.Text(...). You'll need to ensure the format used matches the format of the input.

    Regards, TMS
    You were right on!
    Changed the string variable to a date variable (Just changed strSelectedDate As String to dtmSelectedDate As Date, and replaced it in the code).
    Thanks a million!

  5. #5
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,420

    Re: Copy And Paste Row Into New Worksheet Based On User Listbox Selection

    You're welcome. Thanks for the rep.

    A slightly belated welcome to the forum. Just so you get the best out of the forum, I suggest you spend a few minutes reading through the forum rules. You'll avoid confrontation with the moderators .. for example, you should really add code tags to the code excerpt in your original post.

    Regards, TMS

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1