Wrong code

fraufreda

Board Regular
Joined
Oct 14, 2010
Messages
190
Hello
I am using a user form to list entries in an excel sheet. the user form has 3 rows and in each one there are 4 text boxes.
I need a code that will list all rows only if the first text box in each row has a value.
Code:
if me.acc1.value <>"" then
    ws.Cells(iRow, 1).Value = Me.acc1.Value
    ws.Cells(iRow, 2).Value = Me.txtRef.Value
    ws.Cells(iRow, 4).Value = Me.desc1.Value
   end if
if me.acc1.value <>"" then
    ws.Cells(iRow, 1).Value = Me.acc2.Value
    ws.Cells(iRow, 2).Value = Me.txtRef.Value
    ws.Cells(iRow, 4).Value = Me.desc2.Value
  end if
if me.acc1.value <>"" then
    ws.Cells(iRow, 1).Value = Me.acc3.Value
    ws.Cells(iRow, 2).Value = Me.txtRef.Value
    ws.Cells(iRow, 4).Value = Me.desc3.Value
end if

this code actually didn't work for me. it only lists the last row. I need your help on this please.
thank you
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
If iRow is the same in the 3 blocks of code, obviously you will overwrite cells.

In addition, I do not understand why you use 3 times the same IF condition:

Code:
if me.acc1.value <>"" then

I guess you first need to check your own code on typos (resulting from copy/paste) before it will work as expected.

If the IF condition is 3 times the same, then you should get rid of the 2 last IF's as they are clearly redundant.
 
Upvote 0
my mistake. I forgot to change them when pasting them here but in the actual code they're not the same. anyway here's the code again.
Code:
if me.ac1.value <>"" then
ws.Cells(iRow, 1).Value = Me.acc1.Value
ws.Cells(iRow, 2).Value = Me.txtRef.Value
ws.Cells(iRow, 4).Value = Me.desc1.Value
end if
if me.acc2.value <>"" then
ws.Cells(iRow, 1).Value = Me.acc2.Value
ws.Cells(iRow, 2).Value = Me.txtRef.Value
ws.Cells(iRow, 4).Value = Me.desc2.Value
end if
if me.acc3.value <>"" then
ws.Cells(iRow, 1).Value = Me.acc3.Value
ws.Cells(iRow, 2).Value = Me.txtRef.Value
ws.Cells(iRow, 4).Value = Me.desc3.Value
end if

but as i said this code didn't work for me.
 
Upvote 0
Please re-read my post above, especially the first line.
 
Upvote 0

Forum statistics

Threads
1,224,592
Messages
6,179,787
Members
452,942
Latest member
VijayNewtoExcel

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