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:

A
Date
B
Serial
C
Transaction No.
D
10104040
E
20201010
F
Others
G
Amount
H
199010
I
199011
J
Others
K
Amount
8-7-1812018-08-001500500502010101,00050050050215030500
250214020500

<tbody>
</tbody>

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

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
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,215,419
Messages
6,124,798
Members
449,189
Latest member
kristinh

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