0
votes

Following code I've used allows the data from the Range of cells on a sheet:

Private Sub UserForm_Initialize()
    listTo.Clear
    listCC.Clear
    listTo.List = Worksheets("Main").Range("C12:C26").Value
    listCC.List = Worksheets("Main").Range("D12:D26").Value
End Sub

This works fine. Then my problem now is how to return them back to the sheet if the user clicks the Update button. I've found some answer here, but I can't see any changes or seems to be not working. Below is my edited code:

Private Sub btnUpdate_Click()

Dim dataItems As Range
With Me.listTo
    Dim Data()
    ReDim Data(1 To .ListCount, 1 To 1)
    Data = .List

    With Worksheets("Main")
        Set dataItems = .Range("C12", .Range("C12").Offset(Me.listTo.ListCount - 1, 0))
    End With
    With dataItems
        .Value = Data
    End With
End With

End Sub

This works only if I run the code from the first time, but if I re-run it, it's not working anymore. Any help?

1
It might be best if you post the part of your code that isn't working rather than posting the part of your code that is working. The part that is working is helpful, but the part that isn't working is the bit we really need. Simply knowing that you started off with someone else's answer does not allow us to see whether you have implemented it correctly for your situation.YowE3K
@YowE3K Updated my question. Thanks :)Eem Jee

1 Answers

2
votes

I don't know why your code would return different results if you ran it twice.

Here is a simpler way of doing what you want to do:

Private Sub btnUpdate_Click()

    Worksheets("Main").Range("C12").Resize(listTo.ListCount).Value = listTo.List

End Sub