Copy List box contents on double click

User avatar
adam
SilverLounger
Posts: 2347
Joined: 23 Feb 2010, 12:07

Copy List box contents on double click

Post by adam »

Hi,

How could I make the following code to copy the list box items double clicked; to cells starting from I25 to I33 and then to K25 to K33.

Code: Select all

Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
  Dim r As Long
  With Worksheets("Card")
    If Range("I25") = "" Then
      r = 25
    Else
      r = .Range("I24").End(xlDown).Row + 1
    End If
    .Range("I" & r) = Me.ListBox1.Column(1)
  End With
End Sub
At present the code copies the double clicked rows from I25 and so on until the last excel sheet row.

Any help on this would be kindly appreciated.

Thanks in advance.
Best Regards,
Adam

User avatar
HansV
Administrator
Posts: 78487
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Copy List box contents on double click

Post by HansV »

Code: Select all

Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
  Dim r As Long
  Dim c As String
  With Worksheets("Card")
    c = "I"
    If .Range(c & "25") = "" Then
      r = 25
    Else
      r = .Range(c & "24").End(xlDown).Row + 1
      If r = 34 Then
        c = "K"
        If .Range(c & "25") = "" Then
          r = 25
        Else
          r = .Range(c & "24").End(xlDown).Row + 1
        End If
      End If
    End If
    .Range(c & r) = Me.ListBox1.Column(1)
  End With
End Sub
Last edited by HansV on 22 Apr 2011, 11:32, edited 1 time in total.
Reason: to correct errors in code
Best wishes,
Hans

User avatar
adam
SilverLounger
Posts: 2347
Joined: 23 Feb 2010, 12:07

Re: Copy List box contents on double click

Post by adam »

Thanks for the help Hans. But I'm getting error message End With without with. How could this be avoided?
Best Regards,
Adam

User avatar
sdckapr
3StarLounger
Posts: 392
Joined: 25 Jan 2010, 12:21

Re: Copy List box contents on double click

Post by sdckapr »

The error indicates that your code includes a line:
End With

WIthout having a "With ...."- type of statement

Did you mistakenly delete the line:
With Worksheets("Card")

Steve

User avatar
HansV
Administrator
Posts: 78487
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Copy List box contents on double click

Post by HansV »

adam wrote:Thanks for the help Hans. But I'm getting error message End With without with. How could this be avoided?
Sorry, I hit the Submit button too early, before the code was complete. I have corrected the code in my previous reply.
Best wishes,
Hans

User avatar
adam
SilverLounger
Posts: 2347
Joined: 23 Feb 2010, 12:07

Re: Copy List box contents on double click

Post by adam »

Thanks for the help once again. How could I limit the copying of the rows to K33. At present the code keeps on copying the rows until the last row of the excel sheet?
Best Regards,
Adam

User avatar
HansV
Administrator
Posts: 78487
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Copy List box contents on double click

Post by HansV »

Do you want to stop at K33 or do you want to continue copying to another column?
Best wishes,
Hans

User avatar
adam
SilverLounger
Posts: 2347
Joined: 23 Feb 2010, 12:07

Re: Copy List box contents on double click

Post by adam »

I want to stop at K33 and give the user a message saying that you cannot insert no more data.
Best Regards,
Adam

User avatar
HansV
Administrator
Posts: 78487
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Copy List box contents on double click

Post by HansV »

Code: Select all

Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
  Dim r As Long
  Dim c As String
  With Worksheets("Card")
    c = "I"
    If .Range(c & "25") = "" Then
      r = 25
    Else
      r = .Range(c & "24").End(xlDown).Row + 1
      If r = 34 Then
        c = "K"
        If .Range(c & "25") = "" Then
          r = 25
        Else
          r = .Range(c & "24").End(xlDown).Row + 1
          If r = 34 Then
            MsgBox "Sorry, you can't insert more data.", vbInformation
            Exit Sub
          End If
        End If
      End If
    End If
    .Range(c & r) = Me.ListBox1.Column(1)
  End With
End Sub
Best wishes,
Hans

User avatar
adam
SilverLounger
Posts: 2347
Joined: 23 Feb 2010, 12:07

Re: Copy List box contents on double click

Post by adam »

Thanks for the help Hans. That worked fine and I do really appreciate that.
Best Regards,
Adam

User avatar
adam
SilverLounger
Posts: 2347
Joined: 23 Feb 2010, 12:07

Re: Copy List box contents on double click

Post by adam »

The list box double click event works fine if I have text in row 24.

Suppose, I want to insert text stating from column I25 I have to put text in column I24 in order to keep inserting texts from I25 onwards to K33.

How could I make the code to insert text starting from I25 and onwards without having to write any header or that sort of text in cell I24?

Any help on this would be kindly appreciated.

Thanks in advance.
Best Regards,
Adam

User avatar
HansV
Administrator
Posts: 78487
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Copy List box contents on double click

Post by HansV »

Try this version:

Code: Select all

Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
  Dim r As Long
  Dim c As String
  With Worksheets("Card")
    c = "I"
    r = .Range(c & .Rows.Count).End(xlUp).Row + 1
    If r < 25 Then
      r = 25
    ElseIf r = 34 Then
      c = "K"
      r = .Range(c & .Rows.Count).End(xlUp).Row + 1
      If r < 25 Then
        r = 25
      ElseIf r = 34 Then
        MsgBox "Sorry, you can't insert more data.", vbInformation
        Exit Sub
      End If
    End If
    .Range(c & r) = Me.ListBox1.Column(1)
  End With
End Sub
Best wishes,
Hans

User avatar
adam
SilverLounger
Posts: 2347
Joined: 23 Feb 2010, 12:07

Re: Copy List box contents on double click

Post by adam »

Thanks for the Hans. But with this modification It does not seem to copy row contents to the active sheet on list box double click.

Note: I do have data rows starting from row 3 to 23. Is this due to this reason?
Best Regards,
Adam

User avatar
HansV
Administrator
Posts: 78487
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Copy List box contents on double click

Post by HansV »

It shouldn't matter whether there are data in rows 3-23 or not. The code will copy to the sheet named Card.
Best wishes,
Hans

User avatar
adam
SilverLounger
Posts: 2347
Joined: 23 Feb 2010, 12:07

Re: Copy List box contents on double click

Post by adam »

Hans I've figured out the problem. The code is not working because I have text below the I34. If I delete the texts below the code range; the code works fine.

Having this situation how could I modify the code so that it copies data to the cells even when there is text below the range specified on the code.
Best Regards,
Adam

User avatar
HansV
Administrator
Posts: 78487
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Copy List box contents on double click

Post by HansV »

It's difficult if the requirements are stated tiny bit by tiny bit instead of directly at the beginning. Here is yet another version:

Code: Select all

Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
  Dim r As Long
  Dim c As String
  With Worksheets("Card")
    c = "I"
    If .Range(c & 33) <> "" Then
      c = "K"
      If .Range(c & 33) <> "" Then
        MsgBox "Sorry, you can't insert more data.", vbInformation
        Exit Sub
      End If
    End If
    r = .Range(c & 34).End(xlUp).Row + 1
    If r < 25 Then
      r = 25
    End If
    .Range(c & r) = Me.ListBox1.Column(1)
  End With
End Sub
Best wishes,
Hans

User avatar
adam
SilverLounger
Posts: 2347
Joined: 23 Feb 2010, 12:07

Re: Copy List box contents on double click

Post by adam »

Thanks for the help Hans. This version works fine. The errors are detected each time I run the code. For this reason the requirements are stated "tiny" by "tiny".

Sorry for that and I do apologize for that.
Best Regards,
Adam