Argument not optional, str = - excel

Why when I want to debug, it will error at
If Emp8.Value = True Then
str = "Whatsapp, "
End If
and it will appear;
Argument not optional
Basically what I want to do is, I want to edit data in the user form.
Private Sub cmdEdit_Click()
If Me.Emp1 = "" Then
Call MsgBox("The fields are not complete", vbInformation, "Edit Contact")
Exit Sub
End If
Set findvalue = Sheet1.Range("b8:b10000").Find(What:=Me.Emp1, LookIn:=xlValues)
findvalue.Offset(0, 1).Value = Me.Emp1.Value
If Emp8.Value = True Then
str = "Whatsapp, "
End If
If Emp12.Value = True Then
str = str & "SMS, "
End If
If Emp11.Value = True Then
str = str & "Email, "
End If
If Emp10.Value = True Then
str = str & "Facebook, "
End If
If Emp9.Value = True Then
str = str & "Phone Call, "
End If
str = Left(str, Len(str) - 2)
findvalue.Offset(0, 2) = str
If Emp2.Value = True Then
findvalue.Offset(0, 3) = "Yes"
ElseIf Emp3.Value = True Then
findvalue.Offset(0, 3) = "No"
End If
findvalue.Offset(0, 4).Value = Me.Emp4.Value
findvalue.Offset(0, 5).Value = Me.Emp5.Value
findvalue.Offset(0, 6).Value = Me.Emp6.Value
findvalue.Offset(0, 7).Value = Me.Emp7.Value
Call MsgBox("The contact has been updated", vbInformation, "Edit Contact")
On Error GoTo 0
Exit Sub
End Sub

Related

Error 1004 when trying to load UserForm with non-blank sheet

I have created a UserForm named blocksForm inside a workbook. Buttons inside the form will populate a sheet BBG with data from form. I want the form to be able to show even if my BBG sheet is not blank and fill in the next blank cell. My form will only load from the command button click when the BBG sheet is blank.
When the sheet is not blank and I click the command button to load the form, I get the 1004 error and the debugger highlights Load blocksForm from my code
Private Sub blocksSorter_Click()
Load blocksForm
blocksForm.Show
End Sub
I'm assuming the issue might be from my initialize form code below, but I can't pin point it
Public Sub UserForm_Initialize()
With Worksheets("DATA")
lRow = .Range("A" & .Rows.Count).End(xlUp).Row
End With
With Worksheets("BBG")
lastRow = .Range("A" & .Rows.Count).End(xlUp).Row
End With
MsgBox (lastRow)
If Worksheets("BBG").Range("A1") = "" And i < 3 Then
i = 2
End If
activeCheck.Value = False
itwCheck.Value = False
yestCheck.Value = False
recentCheck.Value = False
TextBox2.Value = ""
ric = Worksheets("DATA").Range("H" & i)
name = Worksheets("DATA").Range("B" & i)
valueUSD = Worksheets("DATA").Range("C" & i)
adV = Worksheets("DATA").Range("D" & i)
sitchStr = ""
dstr = ""
timeStr = ""
pStr = ric & " " & name & " " & valueUSD & " " & adV
Label1.Caption = pStr
If i > lRow Then
Unload Me
Application.Worksheets("BBG").Activate
End If
End Sub
My public variables
Public valueUSD, name, ric, adV, dstr, sitchStr, timeStr, pStr As String
Public i, lRow, lastRow, j, k As Long
The rest of my code
Private Sub activeCheck_Change()
If activeCheck.Value = True Then
sitchStr = activeCheck.Caption
dstr = dstr + sitchStr
Else
sitchStr = ""
End If
End Sub
Private Sub itwCheck_Change()
If activeCheck.Value = False And itwCheck.Value = True Then
sitchStr = sitchStr + itwCheck.Caption
dstr = dstr + sitchStr
ElseIf activeCheck.Value = True And itwCheck.Value = True Then
MsgBox ("You can only be active OR ITW")
End If
End Sub
Private Sub yestCheck_Change()
If yestCheck.Value = True Then
timeStr = timeStr & " " & yestCheck.Caption
dstr = dstr + timeStr
End If
End Sub
Private Sub recentCheck_Change()
If yestCheck.Value = False And recentCheck.Value = True Then
timeStr = timeStr & " " & recentCheck.Caption
dstr = dstr + timeStr
ElseIf yestCheck.Value = True And recentCheck.Value = True Then
MsgBox ("You cannot select both yesterday and recently")
End If
End Sub
Private Sub TextBox2_Change()
If sitchStr = "" Then
dstr = TextBox2.Value
ElseIf sitchStr <> "" Then
dstr = sitchStr & timeStr & ", " & TextBox2.Value
End If
End Sub
Private Sub addBtn_Click()
Application.ScreenUpdating = False
Dim pasteSheet As Worksheet
Set pasteSheet = Application.Worksheets("SHANE FOR BBG")
If j = 0 Then j = 1
If Worksheets("BBG").Range("A1") = "" Then
k = 1
ElseIf Worksheets("BBG").Range("A1") <> "" Then
With Worksheets("BBG")
lastRow = .Range("A" & .Rows.Count).End(xlUp).Row
End With
j = lastRow + 2
End If
If Not IsError(Application.Match(name, Sheets("BBG").Range("A:A"), 0)) Then
MsgBox ("This is already on your list")
End If
pasteSheet.Cells(j, k) = name
pasteSheet.Cells(j, k + 1) = "(" & ric & ")"
pasteSheet.Cells(j + 1, k) = valueUSD & ","
pasteSheet.Cells(j + 1, k + 1) = " " & adV & " ADV"
If dstr = "" Then
j = j + 3
ElseIf dstr <> "" Then
pasteSheet.Cells(j + 2, k) = dstr
j = j + 4
End If
i = i + 1
UserForm_Initialize
End Sub
Private Sub skipBtn_Click()
If i = 2 Then
i = 3
Else
i = i + 1
End If
UserForm_Initialize
End Sub
Private Sub prevBtn_Click()
i = i - 1
MsgBox (dstr)
UserForm_Initialize
End Sub
Sub exitBtn_Click()
Unload Me
End Sub
You could include an IF statement to prevent the error, but that wont fix the underlying problem of how i got set to 0 in the first place...
If i = 0 Then
Msgbox "Somehow, i got set to zero. Aborting."
Exit Sub
Else
ric = Worksheets("DATA").Range("H" & i)
name = Worksheets("DATA").Range("B" & i)
valueUSD = Worksheets("DATA").Range("C" & i)
adV = Worksheets("DATA").Range("D" & i)
End If

Error-91 VB script error

I keep getting error91 come up when I run my code which says that the object variable is not set.
It highlights the line:
'If OptionButton3.Value Then' when I try the debugger. I don't understand where its going wrong and I don't know what an object variable is? Has anyone else had this issue?
Any help would be much appreciated. Thank you
Sub NewEntry()
Dim cboOpName As Object
Dim TextBox1 As Object
Dim Operator As String
Dim UserForm1 As Object
Dim OptionButton1 As Object
Dim OptionButton3 As Object
Dim OptionButton11 As Object
Sheets("Sheet1").Activate
txtTitle1 = "Input Operator"
firstline1 = "Operator" & Chr(10) & Chr(10)
firstline = (firstline1)
Oper = InputBox(firstline, txtTitle1)
If Oper = "" Then
Exit Sub
Else
End If
If Oper = 0 Then
Exit Sub
Else
End If
txtTitle1 = "Input Project ID Number"
firstline3 = "Project ID No" & Chr(10) & Chr(10)
firstline = (firstline3)
PROno = InputBox(firstline, txtTitle1)
If PROno = "" Then
Exit Sub
Else
End If
If PROno = 0 Then
Exit Sub
Else
End If
txtTitle1 = "Input Date of Manufacture"
firstline3 = "Date of Manufacture" & Chr(10) & Chr(10)
firstline = (firstline3)
val1 = "DD/MM/YY"
DateM = InputBox(firstline, txtTitle1, val1)
If DateM = "" Then
Exit Sub
Else
End If
If DateM = 0 Then
Exit Sub
Else
End If
txtTitle1 = "Input Serial Number"
firstline3 = "Serial Number" & Chr(10) & Chr(10)
firstline = (firstline3)
SerNo = InputBox(firstline, txtTitle1)
If SerNo = "" Then
Exit Sub
Else
End If
If SerNo = 0 Then
Exit Sub
Else
End If
txtTitle1 = "Input Actuator ID"
firstline3 = "Actuator ID" & Chr(10) & Chr(10)
firstline = (firstline3)
ActID = InputBox(firstline, txtTitle1)
If ActID = "" Then
Exit Sub
Else
End If
If ActID = 0 Then
Exit Sub
Else
End If
txtTitle1 = "Input Opening Angle"
firstline3 = "Opening Angle" & Chr(10) & Chr(10)
firstline = (firstline3)
Angle = InputBox(firstline, txtTitle1)
If Angle = "" Then
Exit Sub
Else
End If
If Angle = 0 Then
Exit Sub
Else
End If
txtTitle1 = "Input Date of Test"
firstline3 = "Date of Test" & Chr(10) & Chr(10)
firstline = (firstline3)
DateT = InputBox(firstline, txtTitle1, val1)
val1 = "DD/MM/YY"
If DateT = "" Then
Exit Sub
Else
End If
If DateT = 0 Then
Exit Sub
Else
End If
UserForm2.Show
UserForm4.Show
Sheets("Sheet1").Activate
RowNow = 6
RowNum = 1
Do While RowNow = ""
If RowNow <> "" Then
RowNow = RowNow + 1
RowNum = RowNum + 1
Else
Cells(RowNow, 1).Value = RowNum
Cells(RowNow, 4).Value = PROno
Cells(RowNow, 9).Value = DateM
Cells(RowNow, 7).Value = SerNo
Cells(RowNow, 8).Value = ActID
Cells(RowNo, 2).Value = DateT
Cells(RowNow, 3).Value = Oper
Cells(RowNow, 10).Value = Angle
End If
Loop
Do While Cells(RowNow, 11) = ""
If OptionButton3.Value Then
Cells(RowNow, 11).Value = "Yes"
End If
Cells(RowNow, 11).Value = "No"
RowNow = RowNow + 1
Loop
Do While Cells(RowNow, 6) = ""
If OptionButton11.Value Then
Cells(RowNow, 6).Value = "Yes"
End If
Cells(RowNow, 6).Value = "No"
RowNow = RowNow + 1
Loop
Do While Cells(RowNow, 5) = ""
If OptionButton1.Value Then
Cells(RowNow, 5).Value = "Yes"
End If
Cells(RowNow, 5).Value = "No"
RowNow = RowNow + 1
Loop
End Sub
​

Multiple values in userform listbox - what's wrong with my code

I don't know how to program in this language so I have to rely on Google. Taken bits from here and there and I suspect that I have two puzzles that don't fit at the moment. I am getting runtime error 9 subscript out of range because of this line:
ThisWorkbook.Sheets("Sheet4").Range("c2").End(xlDown).Select = myVar
Here's my code:
Dim iRow As Long
Dim ws As Worksheet
Set ws = Worksheets("Skráningar")
'Taeki gögn í gagnagrunn
myVar = ""
For X = 0 To Me.taeki.ListCount - 1
If Me.taeki.Selected(X) Then
If myVar = "" Then
myVar = Me.taeki.List(X, 0)
Else
myVar = myVar & "," & Me.taeki.List(X, 0)
End If
End If
Next X
ThisWorkbook.Sheets("Sheet4").Range("c2").End(xlDown).Select = myVar
'find first empty row in database
'iRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _ SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
iRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row + 1
'check for a Name number
If Trim(Me.timutbox.Value) = "" Then
Me.dagsbox.SetFocus
MsgBox "Vinsamlega skráðu hversu lengi tækið var í notkun"
Exit Sub
End If
Me.Hide
'copy the data to the database
ws.Cells(iRow, 1).Value = Me.dagsbox.Value
ws.Cells(iRow, 2).Value = Me.timutbox.Value
ws.Cells(iRow, 5).Value = Me.sandbox.Value
ws.Cells(iRow, 6).Value = Me.vedurbox.Value
ws.Cells(iRow, 8).Value = Me.bilunbox.Value
ws.Cells(iRow, 7).Value = Me.athbox.Value
ws.Cells(iRow, 3).Value = Me.taeki.Value
ws.Cells(iRow, 4).Value = Me.svaedi.Value
MsgBox "Data added", vbOKOnly + vbInformation, "Data Added"
'clear the data
Me.dagsbox.Value = ""
Me.timutbox.Value = ""
Me.sandbox.Value = ""
Me.vedurbox.Value = ""
Me.bilunbox.Value = ""
Me.athbox.Value = ""
Me.taeki.Value = ""
Me.svaedi.Value = ""
Me.dagsbox.SetFocus
I think I understand what you are trying to do:
Dim iRow As Long
Dim ws As Worksheet
Set ws = Worksheets("Skráningar")
'Taeki gögn í gagnagrunn
myVar = ""
For X = 0 To Me.taeki.ListCount - 1
If Me.taeki.Selected(X) Then
If myVar = "" Then
myVar = Me.taeki.List(X, 0)
Else
myVar = myVar & "," & Me.taeki.List(X, 0)
End If
End If
Next X
'find first empty row in database
'iRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _ SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
iRow = ws.range("A" & rows.count).End(xlUp).Row + 1
ws.Range("C" & iRow).value = myVar
'check for a Name number
If Trim(Me.timutbox.Value) = "" Then
Me.dagsbox.SetFocus
MsgBox "Vinsamlega skráðu hversu lengi tækið var í notkun"
Exit Sub
End If
Me.Hide
'copy the data to the database
ws.Cells(iRow, 1).Value = Me.dagsbox.Value
ws.Cells(iRow, 2).Value = Me.timutbox.Value
ws.Cells(iRow, 5).Value = Me.sandbox.Value
ws.Cells(iRow, 6).Value = Me.vedurbox.Value
ws.Cells(iRow, 8).Value = Me.bilunbox.Value
ws.Cells(iRow, 7).Value = Me.athbox.Value
ws.Cells(iRow, 3).Value = Me.taeki.Value
ws.Cells(iRow, 4).Value = Me.svaedi.Value
MsgBox "Data added", vbOKOnly + vbInformation, "Data Added"
'clear the data
Me.dagsbox.Value = ""
Me.timutbox.Value = ""
Me.sandbox.Value = ""
Me.vedurbox.Value = ""
Me.bilunbox.Value = ""
Me.athbox.Value = ""
Me.taeki.Value = ""
Me.svaedi.Value = ""
Me.dagsbox.SetFocus
EDIT:
I have changed how iRow is calculated, and removed reference to sheet4
Does that do what you need?
Dim iRow As Long
Dim ws As Worksheet
Set ws = Worksheets("Skráningar")
'Taeki gögn í gagnagrunn
myVar = ""
For X = 0 To Me.taeki.ListCount - 1
If Me.taeki.Selected(X) Then
If myVar = "" Then
myVar = Me.taeki.List(X, 0)
Else
myVar = myVar & "," & Me.taeki.List(X, 0)
End If
End If
Next X
'find first empty row in database
'iRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _ SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
iRow = ws.Range("A" & Rows.Count).End(xlUp).Row + 1
ws.Range("C" & iRow).Value = myVar
'check for a Name number
If Trim(Me.timutbox.Value) = "" Then
Me.dagsbox.SetFocus
MsgBox "Vinsamlega skráðu hversu lengi tækið var í notkun"
Exit Sub
End If
Me.Hide
'copy the data to the database
ws.Cells(iRow, 1).Value = Me.dagsbox.Value
ws.Cells(iRow, 2).Value = Me.timutbox.Value
ws.Cells(iRow, 5).Value = Me.sandbox.Value
ws.Cells(iRow, 6).Value = Me.vedurbox.Value
ws.Cells(iRow, 8).Value = Me.bilunbox.Value
ws.Cells(iRow, 7).Value = Me.athbox.Value
ws.Cells(iRow, 3).Value = Me.taeki.Value
ws.Cells(iRow, 4).Value = Me.svaedi.Value
MsgBox "Data added", vbOKOnly + vbInformation, "Data Added"
'clear the data
Me.dagsbox.Value = ""
Me.timutbox.Value = ""
Me.sandbox.Value = ""
Me.vedurbox.Value = ""
Me.bilunbox.Value = ""
Me.athbox.Value = ""
Me.taeki.Value = ""
Me.svaedi.Value = ""
Me.dagsbox.SetFocus
This is the current code.

ping silently in background

When I execute the following code, a black command window opens and it will flicker until the time all devices pings. How can I run it silently?
Sub PING()
Application.ScreenUpdating = False
Dim strTarget, strPingResult, strInput, wshShell, wshExec
With Sheets(1)
shlastrow = .Cells(Rows.Count, "B").End(xlUp).Row
Set shrange = .Range("B3:B7" & shlastrow)
End With
For Each shCell In shrange
strInput = shCell.Text
If strInput <> "" Then
strTarget = strInput
setwshshell = CreateObject("wscript.shell")
Set wshExec = wshShell.exec("ping -n 2 -w 5 " & strTarget)
strPingResult = LCase(wshExec.stdout.readall)
If InStr(strPingResult, "reply from") Then
shCell.Offset(0, 1).Value = "Reachable"
shCell.Offset(0, 2).Value = "Time"
Else
shCell.Offset(0, 1).Value = "UnReachable"
shCell.Offset(0, 2).Value = "Reachable"
End If
End If
Next shCell
End Sub
Here is the code for that
Sub Do_ping()
With ActiveWorkbook.Worksheets(1)
n = 0
Row = 2
Do
If .Cells(Row, 1) <> "" Then
If IsConnectible(.Cells(Row, 1), 2, 100) = True Then
n = n + 1
Cells(Row, 1).Interior.Color = RGB(0, 255, 0)
Cells(Row, 1).Font.FontStyle = "bold"
Cells(Row, 1).Font.Size = 14
Cells(Row, 2).Interior.Color = RGB(0, 255, 0)
Cells(Row, 2).Value = Time
'Call siren
Else:
n = n + 1
'Cells(Row, 2).Formula = "=NOW()-" & CDbl(Now())
Cells(Row, 1).Interior.Color = RGB(255, 0, 0)
Cells(Row, 3).Value = DateDiff("h:mm:ss", Cells(Row, 2), Now())
End If
End If
Row = Row + 1
Loop Until .Cells(Row, 1) = ""
End With
End Sub
Function IsConnectible(sHost, iPings, iTO)
' Returns True or False based on the output from ping.exe
' Works an "all" WSH versions
' sHost is a hostname or IP
' iPings is number of ping attempts
' iTO is timeout in milliseconds
' if values are set to "", then defaults below used
Dim nRes
If iPings = "" Then iPings = 1 ' default number of pings
If iTO = "" Then iTO = 550 ' default timeout per ping
With CreateObject("WScript.Shell")
nRes = .Run("%comspec% /c ping.exe -n " & iPings & " -w " & iTO _
& " " & sHost & " | find ""TTL="" > nul 2>&1", 0, True)
End With
IsConnectible = (nRes = 0)
End Function

Turn 3 listboxes into 1 3-column listbox?

The following code searches column A(sorted) for an item# and each time it finds it, the corresponding B, C & D column are entered into 3 listboxes. I would like to use a 3-column listbox. Any help?
Private Sub cmdSearch_Click()
Dim Response As Long
Dim NotFound As Integer
Dim arr As Variant
Dim i As Long
Dim str1 As String, str2 As String, str3 As String
NotFound = 0
ActiveWorkbook.Sheets("Items").Activate
Response = Val("0" & Replace(txtItemNumber.Text, "-", ""))
If Response <> False Then
With ActiveSheet
arr = .Range("A2:D" & .Cells(.Rows.Count, "A").End(xlUp).Row)
End With
For i = 1 To UBound(arr)
If arr(i, 1) = Response Then
str1 = IIf(str1 = "", arr(i, 2), str1 & "|" & arr(i, 2))
str2 = IIf(str2 = "", arr(i, 3), str2 & "|" & arr(i, 3))
str3 = IIf(str3 = "", arr(i, 4), str3 & "|" & arr(i, 4))
End If
Next
If str1 = "" Then
MsgBox "Item Number Not Found!", vbExclamation
NotFound = 1
Else
Frame1.Visible = True
ListBox1.List = Split(str1, "|")
ListBox2.List = Split(str2, "|")
ListBox3.List = Split(str3, "|")
End If
End If
End Sub
Thanks for any help...
This should do it:
Change:
If str1 = "" Then
MsgBox "Item Number Not Found!", vbExclamation
NotFound = 1
Else
Frame1.Visible = True
ListBox1.List = Split(str1, "|")
ListBox2.List = Split(str2, "|")
ListBox3.List = Split(str3, "|")
End If
to:
If str1 = "" Then
MsgBox "Item Number Not Found!", vbExclamation
NotFound = 1
Else
Frame1.Visible = True
ListBox1.Clear 'to avoid errors
ListBox1.ColumnCount = 3
For i = 0 To UBound(Split(str1, "|"))
ListBox1.AddItem Split(str1, "|")(i)
ListBox1.List(i, 1) = Split(str2, "|")(i)
ListBox1.List(i, 2) = Split(str3, "|")(i)
Next
End If
hint: you may change ColumnWidths
however... to leave you some work, i suggest to merge it with your arr-part...
just using my solution would be a waste :D

Resources