Excel VBA Error on my Msgbox line - vba

I'm getting an Application-defined or object-defined error on my Msgbox line. I have both InputBox variables declared as String. I tried changing Sheets(sheetname1) to an actual sheet name but same error. I tried everything I know, I'm puzzled with this error. Any help is appreciated.
sheetname1 = Application.InputBox("Enter the name of your first sheet.")
sheetname2 = Application.InputBox("Enter the name of your second sheet.")
For i = 1 To 100
For j = 1 To 100
If Not Sheets(sheetname1).Cells(i, j).Value = Sheets(sheetname2).Cells(i, j).Value Then
Sheets(sheetname1).Select
Cells(i, j).Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 65535
.TintAndShade = 0
.PatternTintAndShade = 0
End With
ans = MsgBox("Cells " & i & "," & j & " do not match." & vbNewLine & "The value on " & sheetname1 & " is " & Sheets(sheetname1).Cells(i, j).Value & " and the value on " & sheetname2 & " is " & Sheets("sheetname2").Cells(i, j).Value, vbOKCancel + vbQuestion)
If ans = vbCancel Then Exit Sub
Else
GoTo skip1
End If
skip1:
Next j
Next i

As #BruceWayne correctly identified, you're using undeclared variables. Use Option Explicit.
Let me rephrase that...
ALWAYS use Option Explicit
Simple: stick the words Option Explicit at the top of every single module you ever see, and VBA will refuse to compile code where you're using an undeclared variable.
In this instance, you're referring to i1 and j1, which both contain the non-value Empty, since they're undeclared and uninitialized variants.
Here are the variables you need to declare:
Dim i As Long
Dim j As Long
Dim ans As vbMsgBoxResult
Dim sheetName1 As String
Dim sheetName2 As String
Without Option Explicit, VBA happily compiles a typo and takes it as "oh, I've never seen that guy, must be a new variable!"

Related

SUM a range where the range is a variable

I have:
Set QuanityRange = Sheets("Raw_Data").Range("F2:F" & lastDataRow)
MsgBox "This is the Data Range: " & vbNewLine & "Quanities: " & QuanityRange.Address()
This displays $F$2:$F$1838 in the message box.
Then if I have:
Total = WorksheetFunction.Sum(QuanityRange)
MsgBox "Total is: " & Total
This displays 15170 in the message box.
My problem is that I currently have:
Range("B2", Cells(lastMatrixRow, lastMatrixCol)).Formula = "=Sum(Raw_Data!$F$2:$F$10000)"
This works, it puts 15170 in each cell from B2 to BO114 which happens to be the location of (lastMatrixRow, lastMatrixCol) in this case.
However, what I want to do is:
Range("B2", Cells(lastMatrixRow, lastMatrixCol)).Formula = "=Sum(QuanityRange)"
instead of:
Range("B2", Cells(lastMatrixRow, lastMatrixCol)).Formula = "=Sum(Raw_Data!$F$2:$F$10000)"
but it doesn't work. How can I do this?
Can you try with:
Range("B2", Cells(lastMatrixRow, lastMatrixCol)).Formula = "=Sum(" & QuanityRange.Address() & ")"
If you are trying to put the address of a range variable in a formula - this is one solution:
Public Sub TestMe()
Dim varRange As Range
Set varRange = Range("F1:F5")
Range("A1", Cells(3, 3)).Formula = "=Sum(" & varRange.Address & ")"
End Sub
The idea is that the formula to the right should be "translatable" to a string.

Excel Application Crash due to Macro

During launching my macro the Excel application is crashed. If I test the macro with an integer the program runs properly (partnumber = 123). If I check with a string the application is crashed. Thus, no error code is visible for me. I assume that there is a type mismatch (but I set Variant for partnumber)
Sub SbIsInCOPexport()
Dim lastRow As Long
Dim i As Long
Dim found As Boolean
Dim partnumber As Variant
i = 1
found = False
partnumber = ActiveCell.Value
Windows("COPexport.xlsx").Activate
lastRow = Sheets(1).Cells.SpecialCells(xlLastCell).Row
Do While i < lastRow + 1
If Cells(i, 6).Value = partnumber Then
found = True
Exit Do
End If
i = i + 1
Loop
If found = True Then
Cells(i, 6).Select
MsgBox ("Searched part number: " & Str(partnumber) & vbNewLine & "Found part number: " _
& ActiveCell.Value & vbNewLine & "Address: " & Cells(i, 6).Address & vbNewLine & vbNewLine & "Test Order: " & _
Cells(i, 2).Value)
Windows("COPexport.xlsx").Activate
Else
MsgBox "Part number is not found in the COP samples!"
Windows("COPexport.xlsx").Activate
End If
End Sub
What can be the root cause?
I don't see any obvious issues, but consider using the .Find method of range object, like so:
Sub SbIsInCOPexport()
Dim partnumber as Variant
Dim rng as Range
Windows("COPexport.xlsx").Activate
partnumber = ActiveCell.Value
Set rng = Columns(6).Find(partnumber) '## Search in column 6 for partnumber
If rng Is Nothing Then
MsgBox "Part number is not found in the COP samples!"
Windows("COPexport.xlsx").Activate
Else
With rng
MsgBox ("Searched part number: " & Str(partnumber) & vbNewLine & _
"Found part number: " & .Value & vbNewLine & _
"Address: " & .Address & vbNewLine & vbNewLine & _
"Test Order: " & .Offset(0,-4).Value) '## Get the value from column 2
End With
End If
End Sub

Do while loop code wont play

I am trying to loop through a list of data then and copy the rows that contain "WIP" into a second tab. yet the code will nor do anything when I hit execute. Can someone explain why?
Thank you.
Sub Update_LvL1_WIP()
Dim BrowFi As Integer
Dim BrowWIP1 As Integer
Dim dblSKU As Double
Dim strDescription As String
Dim strType As String
BrowFi = (ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row) + 1
Do While BrowFi > 4
If Range("G" & BrowFi).Value = "WIP" Then
strType = Range("G" & BrowFi).Value
strDescription = Range("F" & BrowFi).Value
dblSKU = Range("E" & BrowFi).Value
Worksheets("WIP 1").Activate
BrowWIP1 = (ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row) +1
Range("A" & BrowWIP1).Value = dblSKU
Range("B" & BrowWIP1).Value = strDescription
Range("B" & BrowWIP1).Value = strType
Else
BrowFi = BrowiF - 1
End If
Loop
End Sub
You have a typo in your decrement variable
BrowFi = BrowiF - 1
should be
BrowFi = BrowFi - 1
Since BrowiF was not defined it was treated as 0 and so you were setting BrowFi to -1 on the first run through the loop.
It may be worth using Option Explicit to help catch these errors.

.Formula is producing runtime error 1004 - works for all other strings

Everything works except for the last .Formula for kcFormula. It returns a 1004 Runtime Application error. I am thinking maybe I declared the kcFormula as a string and it is too big or I have no idea what I am really doing :D
this is the problem statement:
Sheets("AML").Range(myRNG).Formula = kcFormula
Sub SharePoint_FormatADDcolS()
Dim myRNG As String
Dim lastROW As Integer
Dim matchFormula As String
Dim kcFormula As String
matchFormula = "=IF(ISNA(Table3[[#This Row],[Status]]=Table3[[#This Row],[Last Status]]),"
matchFormula = matchFormula & """ Status Changed"""
matchFormula = matchFormula & "," & "IF(Table3[[#This Row],[Status]]<>Table3[[#This Row],[Last Status]],"
matchFormula = matchFormula & """ Status Changed""" & "," & """Status Unchanged""" & "))"
kcFormula = "=IF(OR(Table3[[#This Row],[Status]]=" & """Rejected""" & "," & "Table3[[#This Row],[Status]] =" & """Completed""" & "," & "Table3[[#This Row],[Status]]=" & """Not Implemented""" & ")," & """ """ & ",INDEX('AMLLAST'!$1:$1048576,MATCH(Table3[[#This Row],[ID]],'AMLLAST'!$E:$E,0),11)"
lastROW = ActiveSheet.UsedRange.Rows.Count
With Sheets("AML")
.ListObjects(1).Name = "Table3"
End With
Sheets("AML (2)").Name = "AMLLAST"
With Sheets("AML")
Range("J1").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Range("J1").Select
ActiveSheet.PasteSpecial Format:=3, Link:=1, DisplayAsIcon:=False, _
IconFileName:=False
Sheets("AML").Range("C1").EntireColumn.Insert
Sheets("AML").Range("C1").EntireColumn.Insert
Sheets("AML").Range("C1") = "Last Status"
Sheets("AML").Range("D1") = "Match"
Sheets("AML").Range("K1").EntireColumn.Insert
Sheets("AML").Range("K1") = "BITOOL - Rating"
myRNG = "C2:" & "C" & lastROW
Sheets("AML").Range(myRNG).NumberFormat = "General"
Sheets("AML").Range(myRNG).Formula = "=INDEX('AMLLAST'!$1:$1048576,MATCH(Table3[[#This Row],[ID]],'AMLLAST'!$E:$E,0),2)"
myRNG = "D2:" & "D" & lastROW
Sheets("AML").Range(myRNG).NumberFormat = "General"
Sheets("AML").Range(myRNG).Formula = matchFormula
myRNG = "K2:" & "K" & lastROW
Sheets("AML").Range(myRNG).NumberFormat = "General"
Sheets("AML").Range(myRNG).Formula = kcFormula
End With
End Sub
You're missing a parenthesis at the end.....I haven't tested it in excel, but I'm guessing that's the problem.
Note that for issues like this, the easiest way to resolve them is to debug.print the formula and then paste it into an actual cell. That way, excel will give you clearer error message that you can resolve.
You can improve the readability of that last statement by getting rid of all the concatenation:
kcFormula = "=IF(
OR(Table3[[#This Row],[Status]] = ""Rejected"",
Table3[[#This Row],[Status]] = ""Completed"",
Table3[[#This Row],[Status]] = ""Not Implemented""
),
"" "",
INDEX('AMLLAST'!$1:$1048576,
MATCH(Table3[[#This Row],[ID]],'AMLLAST'!$E:$E,0
)
,11
)"
) <---- this guy is missing! (put him inside the closing " mark
Also, when typing up those deeply embedded, complex IF statements, I usually paste them into Notepad and break them across multiple lines.
When you do that, you'll notice that you are missing the final closing ), as OpiesDad pointed out.
Give him the credit for the solution, I just wanted to point out couple of tips for resolving the issue that wouldn't have fit in a comment.

Compile error. Argument not optional

I'm sure you will find the problem that I'm uncapable to do.
Below you can see a resume of the code in which I have the problem.
After changing the value in the combobox1 it shows an error "Compile error. Argument not optional", highlighting in yellow "Sub ComboBox1_Change()" and in blue "Call TextBox4_Exit".
I think I'm doing something wrong with the arguments needed but don't know how to handle.
Thank you for your help.
Sub ComboBox1_Change()
If TextBox4.Visible = True And TextBox4.Value <> "" Then
Call TextBox4_Exit
End If
Sub TextBox4_Exit(ByVal cancel As MSForms.ReturnBoolean)
Dim placas As String
placas = TextBox4.Value
I = 3
While Range("E" & I).Value <> ""
If Range("E" & I).Value = mensaje Then
If Range("L" & I).Value = mensaje2 Then
If sheet1 = "SIC" Then
Range("X" & I).Value = placas
TextBox11.Value = Range("Y" & I).Value
TextBox10.Value = Range("Z" & I).Value
Else
Range("U" & I).Value = placas
TextBox11.Value = Range("AN" & I).Value
End If
End If
End If
I = I + 1
Wend
End Sub
First, the Sub TextBox4_Exit(ByVal cancel As MSForms.ReturnBoolean) has one argument and it's not marked as Optional,so you need to pass a parameter to Call TextBox4_Exit.
Second, did you miss your End Sub of ComboBox1_Change()?