ListBox executing incorrect transpose in the Worksheet.

Jom

New Member
Joined
Feb 19, 2021
Messages
36
Office Version
  1. 2016
Platform
  1. Windows
Could you help me? ListBox executing incorrect transpose in the Worksheet3
My Command Button sends ListBox data to Sheet 2 and Sheet 3.

The ListBox has 2 columns.
Column 1 = numbers
Column 2 = Colors (Ex. "green","blue","black")

Worksheet 2 receives the data from the 2 columns of the ListBox (Column A= numbers / Column B = colors).
Worksheet 3 needs to receive ONLY the data from column 1 of the ListBox, using transpose for the columns of row 2.

It needs to happen in the following way:
Worksheet2
223 Green
224 Green
225 Green
567 Blue
568 Blue

Worksheet3
Line 2: A2=223/B2=224/C2=225/D2=567/E2=568 ...etc (regardless of how many records have in the ListBox.)
(Here, runing incorrectly.)

Error: The next records replace the previous records. They are sent on over of the previous ones. It should be sent to the next empty columns.
Below is my code:


VBA Code:
Private Sub CommandButton2_Click()

'Worksheet2 ---------------(occurs correctly)
Worksheet2.Select
Worksheet2.Range("A3").Select

For i = 0 To UserForm1.ListBox1.ListCount - 1
Do
If Not (IsEmpty(ActiveCell)) Then
ActiveCell.Offset(1, 0).Select
End If

Loop Until IsEmpty(ActiveCell) = True
ActiveCell.Value = ListBox1.List(i, 0)
ActiveCell.Offset(0, 1).Value = ListBox1.List(i, 1)

Next

'(Worksheet3) ---------(occurs incorrectly)

Dim Lin As Long
Dim Col As Long

Lin = 2
'Worksheet2.Range("A3").Select

For Col = 0 To ThisWorkbook.Sheets("Worksheet3").Cells(Lin, Columns.Count).End(xlToRight).Value
For n = 0 To ListBox1.ListCount - 1

    Col = Col + 1
    ThisWorkbook.Sheets("Worksheet3").Cells(Lin, Col).Value = Application.Transpose(ListBox1.List(n))

Next
Next
End Sub


I don't know where the error is. Help me please.
 
Last edited by a moderator:

Jom

New Member
Joined
Feb 19, 2021
Messages
36
Office Version
  1. 2016
Platform
  1. Windows
If you save for the first time, it will be saved correctly. But the second time onwards, it saves in the columns ahead.
Another problem:
If you try to save a 10-digit number, for example, it shows an error in the line of code below:
ThisWorkbook.Sheets("Planilha3").Cells(Linha, Coluna).Value = ListBox1.List(n)
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.

Jom

New Member
Joined
Feb 19, 2021
Messages
36
Office Version
  1. 2016
Platform
  1. Windows
Sorry, translating from Portuguese to English ....

ThisWorkbook.Sheets("Worksheet3").Cells(Lin, Col).Value = ListBox1.List(n)
 

Jom

New Member
Joined
Feb 19, 2021
Messages
36
Office Version
  1. 2016
Platform
  1. Windows
If you can help me, I'll be very grateful.
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
56,883
Office Version
  1. 365
Platform
  1. Windows
This works for me without any problems
VBA Code:
Dim Lin As Long
Dim Col As Long

Lin = 2
Col = ThisWorkbook.Sheets("worksheet3").Cells(Lin, Columns.Count).End(xlToLeft).Column
For n = 0 To ListBox1.ListCount - 1

Col = Col + 1
ThisWorkbook.Sheets("worksheet3").Cells(Lin, Col).Value = ListBox1.List(n)

Next
If it is putting the data into columns further right than it should I can only think that some of the cells are not blank.
 

Jom

New Member
Joined
Feb 19, 2021
Messages
36
Office Version
  1. 2016
Platform
  1. Windows

ADVERTISEMENT

Did you read what I wrote on post # 31?
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
56,883
Office Version
  1. 365
Platform
  1. Windows
Yes I did & I replied in post#34.
Is your code exactly the same as post#34?
 

Jom

New Member
Joined
Feb 19, 2021
Messages
36
Office Version
  1. 2016
Platform
  1. Windows

ADVERTISEMENT

It is exactly the same. But I can't convince you, if you don't believe. I am very grateful for your attempts and efforts.
Perhaps this question is beyond your knowledge, just as it is beyond mine. I hope someone can help me. A big hug.
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
56,883
Office Version
  1. 365
Platform
  1. Windows
Can you please post the code you are using.
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
56,883
Office Version
  1. 365
Platform
  1. Windows
For your reference this is not "Beyond my knowledge" & it is not very nice to insult people who are trying to help you.

I suspect that you have either gone back to your code without saying so (again) or you have changed the part in red
Rich (BB code):
Col = ThisWorkbook.Sheets("worksheet3").Cells(Lin, Columns.Count).End(xlToLeft).Column
 

Jom

New Member
Joined
Feb 19, 2021
Messages
36
Office Version
  1. 2016
Platform
  1. Windows
Dear friend, When checking the posts again, I realized that you are RIGHT since the post 5.
I've been wrong all this time, because I've been focused on (xlRigt) and (xlLeft) haven't paid any attention to .Column

My knowledge is very limited and this causes misunderstandings. I never meant to insult you. I assume my mistake and thank you very much for your availability and patience with me.
I hope to be more attentive to details in the near future.
If you come to Brazil and want to accept a humble invitation to a meal, let me know. Thank you very much. Sorry about my English..
Sincerely...
 

Watch MrExcel Video

Forum statistics

Threads
1,130,167
Messages
5,640,535
Members
417,151
Latest member
ChickenTenderer

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top