Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

fixing problem copy data from userform to sheet

0

hi

 I    have  many  comboboxes(describe, manfacture,type)   and  I   have  combobox (month) .so  I  have  to  fill   all of  comboboxes(describe, manfacture,type)   and  fill combobox (month) .then  it  will copy  to  sheet1 . this  step  works  well  ,but  if  I  don't filling  all  the  comboboxes   and  fill  combobox (month)  for  instance just combobox(c1,c2,c3 ) and  fill combobox(month)   and  leave  the  rest of  combobox are empty  .then  the  values  in sheet  shows  zero  when copy from userform .

any  suggestion to  fix  it ?

Answer
Discuss

Answers

0
Selected Answer

Kalil

In the revised workbook attached, I modified the sub cmdSAVE_Click() by changing the test:

If x > f Then
to read:
If x > f And Me.Controls("CA1").Text <> "" Then
(and likewise for CB and CC). That adds a test to see it the first ComboBox (was "DESCRIBE", now "Description") is complete- if not, values are NOT written to the sheet. That way, you can write any or all rows and only those (or all) will be written to the (common) Month column in the worksheet.

In those If sections, you might want to add a Else statement to either report that the columns were not all complete (or force the user back to the UserForm to complete them).

Regarding the problem of repeat month entries setting the price to 0 (in the Discussion below), if there's data in the first row of ComboBoxes, the lines:

If InStr(1, chk4, chk1) Then

Sheet1.Cells(x, z) = p1
write the new value to column D. Further down the macro, the line:
If InStr(1, chk4, chk2) Then

Sheet1.Cells(x, z) = p2
then overwrites the cell with P2 (=0) since (null string) chk2 (="") is "found" in ch4 by Instr (at position 1). Same happens for similar lines for chk3.

I got around that by making each Instr test like this: 

If chk1 <> "" And InStr(1, chk4, chk1) Then
Sheet1.Cells(x, z) = p1
Now the price is replaced by the new row, whichever row of ComboBoxes is populated.

Hope this helps.

Discuss

Discussion

thanks  John  , but  it  shows a problem  if  I  write  the  price  in specific  month  and  I  return to  write  a new  price  for  the  same  month . it  shows  zero 
Kalil (rep: 36) Sep 21, '21 at 6:31 am
Kalil- please see revised Answer/ file.
John_Ru (rep: 6142) Sep 21, '21 at 7:56 am
that's  better !
thanks  so  much  for  your  cooperation .
Kalil (rep: 36) Sep 21, '21 at 8:43 am
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login