VBA - If statement, Clear contents, add data

Evapar18

Board Regular
Joined
Aug 3, 2018
Messages
86
Office Version
  1. 2019
Platform
  1. Windows
I am having multiple problems:

I created an if statement that first clears out any pre-existing content and then adds new data to the cells from a Userform. The Userform has 5 tabs within a Multipage and when I set the first page as active I want the data to be exported into a BOM. I have a specific sheet called BOM.

Problem 1:
When I test out this code it will clear the contents, but it stops there and skips over the rest.
Problem 2: If i a rem out the clear contents it will write the 3 that I told it too, but If I re run it it will only write to the "B2" cell value and everything else is skipped over.
Problem 3: I have to manually clear out the contents to get all the 3 items to work again.

I don't know if this matters or not, but I noticed that when the code has failed the userform closes out automatically. When it writes all 3 items the userform stays up and works fine until I close it.

What am I doing that would cause this sort of behavior?

Code:
Private Sub CommandButton2_Click()

If MultiPage1.Value = 0 Then


Worksheets("BOM").Range("B2:E77").ClearContents


ThisWorkbook.Worksheets("BOM").Range("B2").Value = ComboBox14
ThisWorkbook.Worksheets("BOM").Range("C2").Value = TextBox111
ThisWorkbook.Worksheets("BOM").Range("D2").Value = TextBox115


End If


End Sub
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Found the solution - simple remove the "ThisWorkbook." works completely fine now
 
Upvote 0

Forum statistics

Threads
1,224,746
Messages
6,180,704
Members
452,994
Latest member
Janick

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