Excel VBA - Code breaks when Excel opens - vba

I just discover problem with my code, which not appears yesterday.
This is code from "Sheet1 (Calculator)":
Option Explicit
Private Sub Worksheet_Activate()
Sheets("Calculator").ComboBox1.ListFillRange = "Materials!B4:B7"
Sheets("Calculator").ComboBox1.ListIndex = 0
End Sub
Private Sub ComboBox1_Change()
Sheets("Calculator").Range("T18") = ComboBox1.ListIndex + 1
Select Case Sheets("Calculator").ComboBox1.ListIndex
Case 0
Sheets("Calculator").ComboBox2.ListFillRange = "Materials!G4:G5"
Sheets("Calculator").ComboBox2.ListIndex = 0
Case 1
Sheets("Calculator").ComboBox2.ListFillRange = "Materials!G6"
Sheets("Calculator").ComboBox2.ListIndex = 0
Case 2
Sheets("Calculator").ComboBox2.ListFillRange = "Materials!G7:G10"
Sheets("Calculator").ComboBox2.ListIndex = 0
Case 3
Sheets("Calculator").ComboBox2.ListFillRange = "Materials!G11:G12"
Sheets("Calculator").ComboBox2.ListIndex = 0
End Select
End Sub
Everything working fine, while Excel is opened. But if I save and close Excel and then reopen it, code breaks at first line under "Case 0" with error message:
Run-time error '438'
Object doesn't support this property or method
Then when I stop debuging and change item in ComboBox1, code works fine again and ComboBox2 is filled with correct data. Do you have any ideas where can be problem?
Here is the file.

Dont know if you initialized your ComboBox object properly.....but this should work:
Set ComboBox1 = Sheets("Calculator").Shapes(1)
ComboBox1.ControlFormat.ListFillRange = "Materials!B4:B7"
so mayb its because mainly you are missing the ControlFormat thing!?

Private Sub ComboBox1_Change()
with Sheets("Calculator")
.Range("T18") = ComboBox1.ListIndex + 1
if .ComboBox1.ListIndex > -1 Then .ComboBox2.List=sheets("Materials").range(G4:G5").offset(choose(.ComboBox1.ListIndex+1).value
end with
End Sub

Related

vba Checkbox if else condition in Subform, access

I have a main form with several tab controls with subforms in them. On the first tab or subform I have a "checkbox 1", which shall be:
checked if "textbox 1" is not empty
unchecked if "textbox 1" is empty
The code is directly put into the Class Object of "subform 1", that's why I thought I could use Me.
Here is my code, but I always get error messages :(
Private Sub Ctltextbox_1_AfterUpdate()
If Len(Ctltextbox_1.Value) = 0 Then
Me.checkbox_1.Value = 0
Else
Me.checkbox_1.Value = -1
End If
End Sub
That way I am getting the
Run-time error '2448': You can't assign a value to this object.
On the line that attempts to assign -1 to Me.checkbox_1.Value.
Try this. It is working for me. I moved it to the Ctltextbox_1_Change action. This will call the function whenever it is changed and will catch when you have nothing in the box as well. I also changed your values to True and False.
Please try this and let me know.
Private Sub Ctltextbox_1_Change()
If Len(frmSub.Ctltextbox_1.Value) = 0 Then
frmSub.CheckBox_1.Value = False
Else
frmSub.CheckBox_1.Value = True
End If
End Sub
you can also solve this in one line as mentioned below.
Private Sub Ctltextbox_1_Change()
frmSub.CheckBox_1.Value = IIf((Len(frmSub.Ctltextbox_1.Value) = 0), False, True)
End Sub
Ok, the simple answer is: you can not use the expression Len()= 0 for a not numeric field! So my working code for the subform is now:
Private Sub Textbox1_AfterUpdate()
If IsNull(Textbox1.Value) = False Then
Me.checkbox1.Value = True
Else
Me.checkbox1.Value = False
End If
End Sub
Thank you all for your help! :)

move to next page on userform multipage excel VBA

i have 5 pages in multipage userform.
if the next button enabled, which it can be clicked by user then it should move to next hidden page, i always got an error "Object Required" it drives me crazy.
Private Sub btnGenerate_Click()
iPageNo = MultiPage1.Value + 1
MultiPage1.Pages(iPageNo).Visible = True
MultiPage1.Value = iPageNo
End Sub
that code seems doesnt work for me, any help would be appreciate.
Thanks
Which line is causing the error when you step thru?
Ensure there are enough existing pages. Also, has the name of the MultiPage object changed?
This code below tested working (2 Pages in MultiPage1, Page2 set hidden):
Option Explicit
Private Sub CommandButton1_Click()
Dim iNextPage As Long
With Me.MultiPage1
iNextPage = .Value + 1
If iNextPage < .Pages.Count Then
.Pages(iNextPage).Visible = True
.Value = iNextPage
End If
End With
End Sub

Listbox Item Clearing is Not Working in VBA Excel

I have a two list boxes with one button when the user can click the button move all the list item from listbox1 to listbox2. when the listbox1 is becomes empty app is getting restarted IN EXCEL 2016.
My Code is
For i = 1 To ThisWorkbook.Sheets("MultiSheet").ListBoxes(strFromlb).listCount
ThisWorkbook.Sheets("MultiSheet").ListBoxes(strTolb).AddItem ThisWorkbook.Sheets("MultiSheet").ListBoxes(strFromlb).List(1)
ThisWorkbook.Sheets("MultiSheet").ListBoxes(strFromlb).RemoveItem (1)
Next i
Here strFromLb is clearing the values but when it clearing last value my VBA App is excel has been restarted.
Then I have tried code to clear the listbox
ThisWorkbook.Sheets("MultiSheet").ListBoxes(strFromlb).ControlFormat.RemoveAllItems
ThisWorkbook.Sheets("MultiSheet").ListBoxes(strFromlb).Items.Clear
The error is
"Object doesnt supported to property or method"
Then
ThisWorkbook.Sheets("MultiSheet").ListBoxes(strFromlb).Clear
This code I got the 400 error. so kindly help me.
Worksheets("MultiSheet").ListBoxes(strFromlb).ControlFormat.‌​RemoveAllItems
Reference: The Complete Guide to Excel VBA Form Control ListBoxes
There are two type listbox controls for the worksheet Forms control and MsForms ActiveX control. getListBox will return get either one.
You code has a couple of syntax errors in it
Listbox.List returns a 0 based array
You don't use parentheses when using RemoveItem because it is a not a function
Dim lBoxFrom As Object, lBoxTo As Object
Set lBoxFrom = getListBox("MultiSheet", strFromlb)
Set lBoxTo = getListBox("MultiSheet", strTolb)
For i = 0 To lBoxFrom.ListCount - 1
lBoxTo.AddItem lBoxFrom.List(1)
lBoxFrom.RemoveItem 1
Next
or
lBoxTo.List = lBoxFrom.List
lBoxFrom.Clear
Sub Test()
Const WORKSHEET_NAME = "Sheet1"
Const strFromlb = "BoxFrom"
Const strTolb = "BoxTo"
Dim lBoxFrom As Object, lBoxTo As Object
Dim i As Integer
Set lBoxFrom = getListBox(WORKSHEET_NAME, strFromlb)
Set lBoxTo = getListBox(WORKSHEET_NAME, strTolb)
lBoxFrom.AddItem "A"
lBoxFrom.AddItem "B"
lBoxFrom.AddItem "C"
For i = 0 To lBoxFrom.ListCount - 1
lBoxTo.AddItem lBoxFrom.List(0)
lBoxFrom.RemoveItem 0
Next
End Sub
Function getListBox(WorkSheetName As String, ListBoxName As String) As Object
Dim lBox As Object
On Error Resume Next
Set lBox = Worksheets(WorkSheetName).ListBoxes(ListBoxName)
On Error GoTo 0
If lBox Is Nothing Then
On Error Resume Next
Set lBox = Worksheets(WorkSheetName).Shapes(ListBoxName).OLEFormat.Object.Object
On Error GoTo 0
End If
Set getListBox = lBox
End Function

Merge excel cells issue using VB.NET

Hello everyone and thank you!
I have some wierd exception that I cannot solve.
It is not my first program though I cannot figure it out!
I'm writing an application wich through it I want to fill an excel workbook.
every time i'm trying to merge cells, I'm recieving an error.
I have checked over and over and I just can't figure it out.
everything in my code supposed to work.
the xl application,workbook,worksheet and ranges all declared.
the wierdest is that if during the code running, i'm entering the excel worksheet manualy (after pausing the program with a msgbox or something), and
selecting some cell (no matter wich), the code is running just fine :(((
Im not aloud to add in images so here is the code - copy paste.
the btn click code:
Private Sub btnCreatPriceQuote_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnCreatPriceQuote.Click
oXL.Visible = False
Dim indexForCopy As Integer
'few commands for faster run in the xl WB
XlFasterRun()
'Replace pricequote sheet with its format to avoid overwrite
PriceQuoteRenew()
'Decalre rows for add details on the BAMAY
DecalreFirstRow()
FillPriceQuoteTable()
TillIndex = 39
EmptyCellIndex = 13
the declaration from a module:
`Public Sub DecalreFirstRow()
rng1thcol1thLinePQ = shPriceQuote.Range("A75:D75")
rng2thcol1thLinePQ = shPriceQuote.Range("E75:F75")
rng3thcol1thLinePQ = shPriceQuote.Range("G75:H75")
rng4thcol1thLinePQ = shPriceQuote.Range("I75:J75")
rng5thcol1thLinePQ = shPriceQuote.Range("K75:L75")
rng6thcol1thLinePQ = shPriceQuote.Range("M75:N75")
rng7thcol1thLinePQ = shPriceQuote.Range("O75:T75")
rng8thcol1thLinePQ = shPriceQuote.Range("U75:X75")
rng1thcol1thLineCF = shCusFile.Range("A12:G12")
rng2thcol1thLineCF = shCusFile.Range("H12:K12")
rng3thcol1thLineCF = shCusFile.Range("L12:O12")
rng4thcol1thLineCF = shCusFile.Range("P12:S12")
rng5thcol1thLineCF = shCusFile.Range("T12:W12")
rng6thcol1thLineCF = shCusFile.Range("X12:AA12")
rng7thcol1thLineCF = shCusFile.Range("AD12:AI12")
rng8thcol1thLineCF = shCusFile.Range("AJ12:AN12")
End Sub
`
public sub where the exception occured:
Public Sub FillPriceQuoteTable()
With rng1thcol1thLinePQ
.Select()
.Value = rng1thcol1thLineCF.Value
**.Merge()**
End With
With rng2thcol1thLinePQ
.Select()
**.Merge()**
.Value = rng2thcol1thLineCF.Value
End With
With rng3thcol1thLinePQ
.Select()
**.Merge()**
.Value = rng3thcol1thLineCF.Value
End With
With rng4thcol1thLinePQ
.Select()
**.Merge()**
.Value = rng4thcol1thLineCF.Value
End With
Replace:
rng.Merge()
With:
rng.mergecells=true.

MS Word VBA for formfields

I am trying to assign a numeric value in VBA for a dropdown formfield. I have the Msgbox just to test functionality:
Sub ScreenB()
Dim a As Double
If ActiveDocument.FormFields("Dropdown9").DropDown.Value = No Then
a = 1
Else
a = 2
End If
MsgBox a
End Sub
With this code, my Msgbox does not change (it reads "2"), even when I change the the dropdown from Yes to No, or vice-versa. I also tried putting quotes around yes ("Yes") in the VBA code and got a Type Mismatch error.
You should use FormFields.Result
Sub ScreenB()
Dim a As Double
If ActiveDocument.FormFields("Dropdown9").Result = "No" Then
a = 1
Else
a = 2
End If
MsgBox a
End Sub