Identifying the name of the clicked shape from another sheet - excel

I work on sheet 1 and sheet 2 and in both sheets there are shapes that contain code. The Shape ID in sheet 1 is "RUN 1" and in sheet 2 is "EQ-1". I already have a code that can identify the shape ID I've clicked on sheet1 / sheet2. But the code is debug with the explanation "the item with the specified name wasn't found". Thanks. Please help :)
This code must be located in the sheet 2
sub x ()
'the first trial
If Sheet1.Shapes(Application.Caller).Name = "RUN 1" Then Sheet2.Cells(1, 2) = "x"
If activesheet.Shapes(Application.Caller).Name = "EQ-1" Then Sheet2.Cells(1, 2) = "x"
'the second trial
If Sheet1.Shapes(Application.Caller).Name = "RUN 1" Or _
activesheet.Shapes(Application.Caller).Name = "EQ-1" Then Sheet2.Cells(1, 2) = "x"
end sub

Try...
Sub x()
If Application.Caller = "RUN 1" Or Application.Caller = "EQ-1" Then
Sheet2.Cells(1, 2) = "x"
End If
End Sub
Hope this helps!

Related

ComboBox Situation in Excel VBA

I have an Excel sheet that's a process checklist. The background is there are 3 Combo Boxes (form control) and within each Combo Box there's multiple options the user can select. Based on what the user selects as a combination between the 3 Combo Boxes it will hide/unhide specific rows in a section and their corresponding checkboxes. I'm trying to code this in VBA.
Between setting everything up these are the problems I'm running into with VBA:
There are 60 different ways the user can select options between the 3 Combo Boxes. Including the "Select Option" options (which will be used), the first ComboBox has 5 options, the second 4 options, and the last 3 options. The obvious I've been trying is If/Else logic, but copying and pasting 60 times over seems very redundant and leaves the sheet prone to coding errors that will take time to fix. Is there a better way to code this? Or in the way I'm trying to do this for each change, the If/Else logic is the best way?
When coding (I've pasted a short example of part of my code below this section with a few of the cases), I run into "Method and data member not found" or "Invalid use of Me" errors when debugging. How do I fix this?
How do I hide and unhide the checkboxes along with the rows be hid and unhidden so the formatting doesn't change and there are not stray checkboxes everywhere?
Sub ComboBox1_Change()
'Combo Box 1 is Asset Type, Combo Box 2 is AUS, Combo Box 3 is Transaction Type
'Select, Select, Select OR Refinance
If Me.ComboBox1.Value = "Select Asset Type" And Me.ComboBox2.Value = "Select AUS" And Me.ComboBox3.Value = "Select Transaction Type" Or Me.ComboBox3.Value = "Refinance" Then
Worksheets("Assets Checklist").Rows("19:37" And "39").EntireRow.Hidden = True
'Select, Select, Purchase
ElseIf Me.ComboBox1.Value = "Select Asset Type" And Me.ComboBox2.Value = "Select AUS" And Me.ComboBox3.Value = "Purchase" Then
Worksheets("Assets Checklist").Rows("39").EntireRow.Hidden = False
Worksheets("Assets Checklist").Rows("19:37").EntireRow.Hidden = True
'Liquid, Select, Select
ElseIf Me.ComboBox1.Value = "Liquid" And Me.ComboBox2.Value = "Select AUS" And Me.ComboBox3.Value = "Select Transaction Type" Then
Worksheets("Assets Checklist").Rows("31:34").EntireRow.Hidden = False
Worksheets("Assets Checklist").Rows("19:30" And "35:37" And "39").EntireRow.Hidden = True
And the above code continues for each case.
If you have 60 distinct cases then the most maintainable method might be to use a worksheet with columns for CB1/2/3 values and a column to hold visible and hidden rows (as range address like "A1:A10,A12"). Create a method which reads the values and looks up the matching row on the sheet, then sets the row visibility accordingly.
'this is called from each of the 3 combos
Sub ShowHideRows()
Dim cb1, cb2, cb3, arr, r As Long, rngHide As String, rngShow As String
cb1 = comboValue("combo1")
cb2 = comboValue("combo2")
cb3 = comboValue("combo3")
Debug.Print cb1, cb2, cb3
arr = Me.Range("choices").Value 'lookup table is a named range
'better on a different sheet....
For r = 1 To UBound(arr, 1)
If arr(r, 1) = cb1 Then
If arr(r, 2) = cb2 Then
If arr(r, 3) = cb3 Then
rngShow = arr(r, 4)
rngHide = arr(r, 5)
Exit For 'stop checking
End If
End If
End If
Next r
If rngHide <> "" Then Me.Range(rngHide).EntireRow.Hidden = True
If rngShow <> "" Then Me.Range(rngShow).EntireRow.Hidden = False
End Sub
'read a Forms combo control value
Function comboValue(cbName As String)
With Me.Shapes(cbName)
comboValue = .ControlFormat.List(.ControlFormat.ListIndex)
End With
End Function
Setup:
'Each of the three Combo Boxes have al Linked Cell
'In an another cell concatenate the numeric values of the three Linced Cells
'e.g. in cell A1 have this: =A2 & A3 & A4
'where A2, A3, and A4 are the Linked cells of CBox1, CBox2 and CBox3
'In every ComboBox value change, call the Sub
'SixtyCases with parameter the value of cell A1
Private Sub SixtyCases(combination As String)
Select Case combination
Case "111" 'first combination
'YOUR CODE HERE
Case "112", "132" ' OR
'YOUR CODE HERE
Case "113"
'YOUR CODE HERE
'...
'...
Case "543" 'last combination
'YOUR CODE HERE
End Select
End Sub

How would I use an If Statement from a userform where if the cell matches another cell it would add the results to a specific field / row?

The majority of my code is working correctly however, when I move onto validation form the userform to excel it isn't working correctly. I am using a form that allows the user to enter in a job number in column AA and Quantity in Column AC. I am keeping a list of jobs in column A. When the user uses the button it will auto fill columns AA thru AD I am looking for after this is done it will check to see if the job number exists in column A and if it does then in the same row in Column D it would add the quantity that was just entered. In the background I have a piece of code that will auto calculate and increase in column D
Private Sub CommandButton1_Click()
whichSheet = PartNoTxtBox.Value
If whichSheet = "" Then
MsgBox "You didn't specify a Part Number"
Exit Sub
End If
Worksheets(whichSheet).Activate
If Me.PartNoTxtBox = "" Then GoTo MoreInfo
If Me.AddJobNoTxtBox = "" Then GoTo MoreInfo
If Me.AddShipperNoTxtBox = "" Then GoTo MoreInfo
If Me.AddQtyTxtBox = "" Then GoTo MoreInfo
If Me.AddDate = "" Then GoTo MoreInfo
''LastRow
eRow = Cells(Rows.Count, "AA").End(xlUp).Offset(1, 27).Row
Cells(eRow, 27) = AddJobNoTxtBox.Text
Cells(eRow, 28) = AddShipperNoTxtBox.Text
Cells(eRow, 29) = AddQtyTxtBox.Text
Cells(eRow, 30) = AddDate.Text
If AddJobNoTxtBox.Value = Cells(eRow, 1) Then
Cells(eRow, 4) = AddQtyTxtBox.Text
End If
Unload UserForm1
Exit Sub
MoreInfo:
MsgBox "More information required."
End Sub
The issue is located at the If AddJobNoTxtBox.Value=Cells...
any direction would be helpful.

return a message box if cell in column C contains the word "FALSE" or "ERROR"

I have a VBA macro which send a message through a message box if any cell of my column C contain the word “ERROR” or “FALSE”… I would like to extend this macro in order that it returns this message box if any cell of my column C contains the word “ERROR” or “FALSE” in a string (I want to do that because sometimes I can have some spaces like for example "ERROR " or " FALSE") … Please find the code of my macro below.
Many Thanks in advance for your help.
Xavi
Sub reporterroroncolumnB ()
Dim LastRow As Integer
Dim X As Integer
LastRow = ActiveSheet.Range("C" & Rows.Count).End(xlUp).Row
For X = 1 To LastRow
If Cells(X, 3) = "ERROR" Or Cells(X, 3) = "FALSE" Then
MsgBox "Cell in column C should be reviewed"
End If
Next X
End Sub
If Instr(Ucase(Cells(X, 3)),"ERROR") + instr(Ucase(Cells(X, 3)),"FALSE") > 0 then

Insert comment based on existing data

I have a little problem, with my VBA code. I have made a userform, which has following:
Combobox1 is Sku number
Combobox2 is Test number
Combobox3 is Test result
Textbox is Comment_To_Result.
'Inserts comments og test result
Dim iRow, iCol
With ThisWorkbook.Worksheets("Data sheet")
iCol = Application.Match(CLng(ComboBox2.Value), .Columns("Q"), 0)
iCol = Application.Match(CLng(ComboBox1.Value), .Columns("A"), 0)
' If IsError(iRow) Then MsgBox "SKU not found": Exit Sub
' If IsError(iCol) Then MsgBox "Test number not found": Exit Sub
'Add test result/next step and comment
.Cells(iCol, 30).Value = Me.ComboBox3.Value
.Cells(iCol, 30 + 1).Value = Me.Comments_To_Result.Value
End With
I want the code to find sku number and test number and based on that, insert test result and comment i the same row. (SKU and test number is already in the sheet)
The code below is working fine, when the test number is 1, but when i try to change the test number to for example 2 or 3, the column code is debuggen. Does anybody know, what could be wrong?
Thanks in advance!
You are writing results in the same columns (30 and 31) regardless of the result of the search on the Test number column. You probably want this:
Dim iRow, iCol
With ThisWorkbook.Worksheets("Data sheet")
iRow = Application.Match(CLng(ComboBox1.Value), .Columns("A"), 0)
iCol = Application.Match(CLng(ComboBox2.Value), .Rows(17), 0)
If IsError(iRow) Then MsgBox "SKU not found": Exit Sub
If IsError(iCol) Then MsgBox "Test number not found": Exit Sub
'Add test result/next step and comment
.Cells(iRow, iCol).Value = Me.ComboBox3.Value
.Cells(iRow, iCol+1).Value = Me.Comments_To_Result.Value
End With
p.s.: Sincde the items searched for are numbers (integers), I converted the combo's values to numbers before searching them in the worksheet:
CLng(ComboBox1.Value) and CLng(ComboBox2.Value)
' ^^^^^ ^^^^^

Excel VBA Line of Questioning

I have an excel document that has 5 successive cells on 1 column that need to be filled in with data. I am looking to create a VBA userForm that will go through a line of questioning to help the user input the correct data.
Lets say that the 5 cells are A1 through A5. Once the userform is called it should show a question(label maybe?), A text box for the input of data, and a command button to move on the the next question. all the while moving from A1 to A2 and so on until the line of questioning is done.
Does anybody have any idea how to accomplish this? My VB knowledge is basic but i have tried and tried to no avail.
Thanks in advance!
Given a form like this:
You could set your questions up in an array and iterate through them each button press while setting the answers to your sheet.
Dim i As Integer
Dim str(1 To 3) As String
Private Sub UserForm_Initialize()
i = 1
str(1) = "Question 1"
str(2) = "Question 2"
str(3) = "Question 3"
btnNext.Default = True
lblQuestion.Caption = str(i)
txtAnswer.SetFocus
End Sub
Private Sub btnNext_Click()
Sheets("Sheet1").Cells(i, 1).Value = txtAnswer.Text
i = i + 1
If i = UBound(str) + 1 Then
UserForm1.Hide
Exit Sub
End If
lblQuestion.Caption = str(i)
txtAnswer.Text = ""
txtAnswer.SetFocus
End Sub
Example of Result:

Resources