Populating worksheet with dynamic # of rows from userform listbox

cherrydee

New Member
Joined
Jan 1, 2017
Messages
13
Hi, I've been frustrated with this part. I'm trying to population the worksheet with data from a userform. column B is auto-incremented, column D & E are 'most used' debit codes (i only listed 2) and column F is for other debit codes aside from D and E.
Column H and I (only listed 2) are most used Credit codes, column J is for other credit codes.
The most used codes are encoded using textboxes while the 'others' and 'others' amount' are populated thru listboxes
The problem is, there are times when the debit and credit are not equal in # of rows as shown below:

[TABLE="width: 500"]
<tbody>[TR]
[TD]A
Date[/TD]
[TD]B
Serial[/TD]
[TD]C
Transaction No.[/TD]
[TD]D
10104040[/TD]
[TD]E
20201010[/TD]
[TD]F
Others[/TD]
[TD]G
Amount[/TD]
[TD]H
199010[/TD]
[TD]I
199011[/TD]
[TD]J
Others[/TD]
[TD]K
Amount[/TD]
[/TR]
[TR]
[TD]8-7-18[/TD]
[TD]1[/TD]
[TD]2018-08-001[/TD]
[TD]500[/TD]
[TD]500[/TD]
[TD]50201010[/TD]
[TD]1,000[/TD]
[TD]500[/TD]
[TD]500[/TD]
[TD]50215030[/TD]
[TD]500[/TD]
[/TR]
[TR]
[TD][/TD]
[TD]2[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]50214020[/TD]
[TD]500[/TD]
[/TR]
</tbody>[/TABLE]

some of my codes:
Code:
lrow = ws.Range("B" & Rows.Count).End(xlUp).Row + 1 'gets the lastrow +1
lrow2 = ws.Range("B" & Rows.Count).End(xlUp).Row - 6 'Since the start of my database is on row 7 (1-6 are headers and stuffs)
ws.Range("A" & lrow).value = Format(CDate(Me.txt_Date.value), "mm/dd/yyyy")
ws.Range("B" & lrow).value = lrow2 + 1
ws.Range("C" & lrow).value = txt_transac_ID.value
ws.Range("D" & lrow).value = Format(Me.TextBox1.value, "#,##0.00")
ws.Range("E" & lrow).value = Format(Me.TextBox2.value, "#,##0.00")

dim i as integer
For i=0 to listbox1.listcount -1 'listbox for debit
If ws.Range("B" & lrow).value = "" Then 
   ws.Range("B" & lrow).value = lrow2 + 1
   ws.Range("C" & lrow).value = txt_transac_ID.value
   ws.Range("F" & lrow).value = ListBox1.List(i, 0)
   ws.Range("G" & lrow).value = ListBox1.List(i, 1)
   Else
    ws.Range("F" & lrow).value = ListBox1.List(i, 0)
   ws.Range("G" & lrow).value = ListBox1.List(i, 1)
End If
Next
 ws.Range("H" & lrow).value = Format(Me.TextBox3.value, "#,##0.00")
 ws.Range("I" & lrow).value = Format(Me.TextBox4.value, "#,##0.00")
dim j as integer
for j = 0 to listbox2.listcount - 1 'listbox for credit
If ws.Range("B" & lrow).value = "" Then 
   ws.Range("B" & lrow).value = lrow2 + 1
   ws.Range("C" & lrow).value = txt_transac_ID.value
   ws.Range("J" & lrow).value = ListBox1.List(j, 0)
   ws.Range("K" & lrow).value = ListBox1.List(j, 1)
Else
    ws.Range("J" & lrow).value = ListBox1.List(j, 0)
    ws.Range("K" & lrow).value = Listbox1.List(j, 1)
End If
 Next
I'm not actually good in programming but our accounting department requested this from me.
I think the problem is the referencing to the lastrow.
 
Last edited:

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Sorry, these are the codes that will result to the table i posted. I can't find the edit button in this thread.
I need to think of a way to somehow populate the serial column inside the for-next of the listbox population. I've tried it but it keeps messing the data placement
Code:
lrow = = ws.Range("B" & Rows.Count).End(xlUp).Row + 1 'gets the lastrow +1
[FONT=Verdana]lrow2 = ws.Range("B" & Rows.Count).End(xlUp).Row - 6 'Since the start of my database is on row 7 (1-6 are headers and stuffs)[/FONT]
ws.Range("A" & lrow).value = Format(CDate(Me.txt_Date.value), "mm/dd/yyyy")
ws.Range("B" & lrow).value = lrow2 + 1
ws.Range("C" & lrow).value = txt_transac_ID.value
ws.Range("D" & lrow).value = Format(Me.TextBox1.value, "#,##0.00")
ws.Range("E" & lrow).value = Format(Me.TextBox2.value, "#,##0.00")

dim i as integer
dim lrow_cr_multi as long
For i=0 to listbox1.listcount -1 'listbox for debit
lrow_cr_multi  = ws.Range("B" & Rows.Count).End(xlUp).Row + i
    ws.Range("F" & lrow_cr_multi).value = ListBox1.List(i, 0)
    ws.Range("G" & lrow_cr_multi).value = ListBox1.List(i, 1)
Next
 ws.Range("H" & lrow).value = Format(Me.TextBox3.value, "#,##0.00")
 ws.Range("I" & lrow).value = Format(Me.TextBox4.value, "#,##0.00")
dim j as integer
dim lrow_db_multi as long
for j = 0 to listbox2.listcount - 1 'listbox for credit
lrow_db_multi = ws.range("B" & Rows.Count).End(xlUp).Row + j
   ws.Range("J" & lrow_db_multi ).value = ListBox1.List(j, 0)
   ws.Range("K" & lrow_db_multi ).value = ListBox1.List(j, 1)
Next
[COLOR=#222222][FONT=Verdana]
[/FONT][/COLOR]
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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
Back
Top