-2

I have a code that he selected data that I want ,but I want to show this data in a table and not ina MsgBox.

This is my code and I hope that someone can find a solution for me

Private Sub CommandButton1_Click()

Dim ws1 As Worksheet
Dim ws2 As Worksheet

Dim table1 As Range
Dim table2 As Range

Dim table1Rows As Integer
Dim table1Cols As Integer

Set ws1 = Worksheets("Feuil1")
Set ws2 = Worksheets("Feuil2")
Set table1 = ws1.Cells
Set table2 = ws2.Cells

table1Rows = ws1.UsedRange.Rows.Count
table1Cols = ws1.UsedRange.Columns.Count

For i = 1 To table1Rows
    For j = 1 To table1Cols
        If table1(i, 1).Value <> table2(i, 1).Value Then
            MsgBox "Libellé : " & table1(i, 1) & ", du montant : " & table1(i, 3) & " est ajouté !"
        End If
    Next
Next
End Sub
4
  • You mean in UserForm, or in some range in spreadsheet, or what? Commented Nov 2, 2016 at 9:07
  • Yes I mean in UserForm . coz I can't see all data in a MsgBox Commented Nov 2, 2016 at 9:46
  • You are not using j looping variable: either you get rid of its loop or you use it! Commented Nov 2, 2016 at 9:51
  • I use j looping ,and I puted it equal to 1 coz I want data of column 1 and 3. The probleme is that when I click on the button , I get data that I want in a MsgBox , but I can't see all my data , and I have to click on OK several times to show data one by one and that disturb me . So I need to see all data in a Table or something like that . Thanks Commented Nov 2, 2016 at 9:57

1 Answer 1

0

What you need is ListBox. Create UserForm (e.g. Userform1) and listbox inside of it (e.g. Listbox1). Then you can put the data inside by this formula .AddItem function. Your loop will look like:

For i = 1 To table1Rows
    For j = 1 To table1Cols
        If table1(i, 1).Value <> table2(i, 1).Value Then
            UserForm1.ListBox1.AddItem "Libellé : " & table1(i, 1) & ", du montant : " & table1(i, 3) & " est ajouté !"
        End If
    Next
Next
End Sub
UserForm1.Show
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for your advise , I know how to create a UserForm and put a ListBox in, but I don't know how to do to get data when I click on my button
If you mean assigning macro to button, than see: stackoverflow.com/questions/10623704/… . If not, thats another question for another topic. Please create one, describe your concerns, somebody will help.
It works know. but there's another problem haha, I get my data but they repeted 4 times and I don't know why :/
I have no idea. Create another thread with screens, code and step-by-step description of a problem, please.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.