"Sub or Function not defined" vba - vba

VBA novice here
When I run my code I receive the error "Sub or Function not defined". This occurs in one of my subroutines. The first line is highlighted in yellow by the debugger.
Sub Description(Row As Integer, SheetName As String)
With Worksheet(SheetName)
.Cells(Row, 1) = ActiveCell.Offset(0, -3)
.Cells(Row, 2) = ActiveCell.Offset(0, -2)
.Cells(Row, 3) = ActiveCell.Offset(0, -1)
End With
End Sub
This subroutine takes in a number and a string. I want to copy specific descriptions into into another worksheet "SheetName". Here is a piece of the code prior to the subroutine call. The value of "x" is declared outside the loops scope.
string_temp = "Sheet2"
ActiveSheet.Cells(i, 5).Select
Call Description(x, string_temp)
x= x + 1
Appreciate the help!

With Worksheet(SheetName) should be With Worksheets(SheetName)
Also change Row As Integer to Row as Long, Integer is actually marginally slower in VBA as the compiler converts it to a Long for processing.

Related

Getting Error of "Invalid procedure call or argument" at VBA

I am new to vba and i have encountered into some problems.
I want to look up something from another worksheet and copy the match value to the other cell
VBA said it was the last code having problem.
Private Sub ReferenceOk_Click()
Dim nextRefRec As Integer
Dim i As Integer
Dim ListNo As Integer
ListNo = ListBoxBook.ListIndex
If ListNo < 0 Then
MsgBox "Please select any book"
Exit Sub
End If
Sheets("Rental History").Activate
nextRefRec = Cells(Rows.Count, 2).End(xlUp).Row + 1
For i = 0 To 1
Cells(nextRefRec, i + 3).Value = ListBoxBook.List(ListNo, i)
Next i
Cells(nextRefRec, 3).NumberFormat = "0000"
Cells(nextRefRec, 2).NumberFormat = "00000"
Cells(nextRefRec, 2).Value = TxtMemberNo.Value
Cells(nextRefRec, 5).Value = Date
Cells(nextRefRec, 6).Value = Date + TxtRentalDays.Value
Cells(nextRefRec, 7).Value = Application.WorksheetFunction.VLookup(Worksheets("Rental History").Cells(nextRefRec, 4), Worksheets("Book List").Cells("B4:C24"), 6, False)
End Sub
Your VLOOKUP formula:
VLookup(Worksheets("Rental History").Cells(nextRefRec, 4), Worksheets("Book List").Cells("B4:C24"), 6, False)
seems invalid.
The range Worksheets("Book List").Cells("B4:C24") contains 2 columns, but your third argument is 6. In other words, you are attempting to get the 6th column of a 2-column range (which obviously doesn't exist). So at the moment it's probably returning a #REF error.
Further info on VLOOKUP if needed: https://support.office.com/en-us/article/vlookup-function-0bbc8083-26fe-4963-8ab8-93a18ad188a1
Fix your VLOOKUP formula so that you're passing a column that at least exists within the range you pass as the second argument.
Also, I recommend declaring these variables as type Long (to prevent type overflow errors):
Dim nextRefRec As Integer
Dim i As Integer
Dim ListNo As Integer
and changing this line:
Cells(nextRefRec, 7).Value = Application.WorksheetFunction.VLookup(Worksheets("Rental History").Cells(nextRefRec, 4), Worksheets("Book List").Cells("B4:C24"), 6, False)
to this (except also fix your VLOOKUP arguments as mentioned above):
Cells(nextRefRec, 7).Value = Application.VLookup(Worksheets("Rental History").Cells(nextRefRec, 4), Worksheets("Book List").Cells("B4:C24"), 6, False)
so that if VLOOKUP returns #N/A or some other error, the error value can be written to the cell instead of interrupting your macro.
Your code implicitly refers to whatever sheet happens to be active whilst the code is running. Try to refer to the parent workbook and worksheet (in case the sheet that's active isn't the one that you think it is).

VBA - Index SumProduct with mis

I am new to VBA and am working on a userform at the moment that has 3 ComboBoxes that a user picks 1 item from each. The intent is for the code to look up the corresponding 4th value from the spreadsheet and return it to a textbox on the userform. Right now I'm getting a "Type Mismatch (Error 13):"
I have been fiddling with this for going on 3 days now. Some websites say the SumProduct function doesn't work in VBA, some say it does as long as you specify "WorkSheetFunction", and still some say you should instead use 'evaluate'.
Like in this post:
SUMPRODUCT Formula in VBA
(I didn't have much luck using 'evaluate' but my syntax might have been off)
Anyway, I created a quick example to show what I am trying to do. If anyone can help it would be tremendously appreciated.
worksheetdata
this is the code I have been trying:
Private Sub TestButton_Click()
textboxTesting.Text = Application.WorkSheetFucntion.Index(Range("Thickness"), _
Application.WorkSheetFucntion.SumProduct((Range("Wood") = "Oak") _
* (Range("Metal") = "Copper") * (Range("Box") = "Red")), 0)
End Sub
A better approach is to write your own function, I believe the below is much more understandable:
Sub Test()
Dim thickness
thickness = FindThickness("oak", "copper", "red")
MsgBox (thickness)
End Sub
Function FindThickness(wood As String, metal As String, box As String) As String
Dim rng As Range
Set rng = Range("Wood")
For Each cell In rng
If cell.Value = wood _
And cell.Offset(0, 1).Value = metal _
And cell.Offset(0, 3).Value = box Then
FindThickness = cell.Offset(0, 2).Value
Exit Function
End If
Next cell
End Function

Populating an excel file from word vba

I'm writing a macro that will populate an excel file with user inputs from active x controls in word. I've got almost everything working except that I keep getting an error message when I try and select cell A1 in the sheet that I want to use in the workbook. Here is the code:
Workbooks.Open ("mypath\myfile.xlsm")
Workbooks("myfile.xlsm").Activate
Worksheets("sheet1").Select
Range("A1").Select
Do Until (IsEmpty(ActiveCell.Value))
ActiveCell.Offset(1, 0).Select
Loop
ActiveCell.Value = n
ActiveCell.Offset(0, 1).Value = a
ActiveCell.Offset(0, 2).Value = b
ActiveCell.Offset(0, 3).Value = c
Columns("D:D").EntireColumn.AutoFit
Columns("A:A").EntireColumn.AutoFit
Columns("B:B").EntireColumn.AutoFit
Columns("C:C").EntireColumn.AutoFit
Workbooks("myfile.xlsm").Save
Workbooks("myfile.xlsm").Close
The variables in this block of code are the values of the active x controls and are located much further up in the sub. This block of code is a small part of an if statement within the sub. Anyhow, when I take Range("A2").Select out of the code, it works just fine except for the fact that the information that I want to input does not go to the right spot (since it didn't select range A1 to begin with).
The error I get is type mismatch 4218.
Referencing the Excel object model gives you access to some global objects defined in that object model.
VBA resolves identifiers in this order:
Current procedure
Current module
Current project
VBA standard library
Host application object model
Any other referenced library, in the order they appear in the references dialog
So when you invoke Range meaning to be a call to the Excel object model, you actually invoke the same-name Range global member that's defined in the Word object model.
Note I say member and mean it: these are unqualified member calls to Global.Range. This is important, because a member implies an object, and since everything in the Excel object model (Word's too) has an Application property, then if you're not explicit about exactly what you're referring to, you might be implicitly creating an Excel.Application object, that you can't quite clean up properly. This usually translates into a "ghost" EXCEL.EXE process lingering in Task Manager well after your macro finishes running.
The trick is to make that reference explicit, and explicitly constrain its lifetime - a With block is perfect for this:
With New Excel.Application
With .Workbooks.Open(path)
With .Worksheets("Sheet1")
lRow = .Cells(.Rows.Count, 1).End(xlUp).Row + 1
.Cells(lRow, 1) = n
.Cells(lRow, 2) = a
.Cells(lRow, 3) = b
.Cells(lRow, 4) = c
.Columns("A:D").EntireColumn.AutoFit
End With
.Save
.Close
End With
.Close
End With
I'm guessing as I don't usually run Excel from Word, but I think the problem might be related to everything being unqualified from Word.
If Workbooks.Open is working, then we can just hang everything related to that workbook on that..
Try the following code instead:
Dim myWkBk As Workbook, lRow As Long
Set myWkBk = Excel.Application.Workbooks.Open("mypath\myfile.xlsm")
With myWkBk.Sheets("sheet1")
lRow = .Cells(.Rows.Count, 1).End(xlUp).Row + 1
.Cells(lRow, 1) = n
.Cells(lRow, 2) = a
.Cells(lRow, 3) = b
.Cells(lRow, 4) = c
.Columns("A:D").EntireColumn.AutoFit
End With
myWkBk.Save
myWkBk.Close
I've got it figured out. #Cindy Meister I just needed to add an ActiveSheet. qualifier on the troubled line:
Workbooks.Open ("H:\Second Rotation\OBI project\answersUsers.xlsm")
Workbooks("answersUsers.xlsm").Activate
Sheets("Answers Users").Select
ActiveSheet.Range("A1").Select
Do Until (IsEmpty(ActiveCell.Value))
ActiveCell.Offset(1, 0).Select
Loop
ActiveCell.Value = n
ActiveCell.Offset(0, 1).Value = cwid
ActiveCell.Offset(0, 2).Value = mann
ActiveCell.Offset(0, 3).Value = dept
Columns("A:D").EntireColumn.AutoFit
Workbooks("answersUsers.xlsm").Save
Workbooks("answersUsers.xlsm").Close
Dim myWkBk As Workbook, lRow As Long

Run time error 1004 on using Vlookup function

Im trying to highlight range of cells when a date is present in the list of holidays. But on running the below code, the Run time error 1004 is displayed. I have even tried handling it in error handler; but it is not working. Can somebody please help me why this error is occuring and resolve the same?
Sub highlight_cells()
Dim myrange As Range
On Error GoTo myerr:
For i = 1 To 10
Set myrange = Range(Cells(1, i), Cells(10, i))
temp = Application.WorksheetFunction.VLookup(Range(Cells(1, i)), [holidays], 2, False)
If (Application.WorksheetFunction.IsNA(temp)) Then
myrange.Interior.Color = 3
End If
Next i
myerr:
If Err.Number = 1004 Then
MsgBox "vlookup error"
End If
End Sub
Range(Cells(1, i)) isn't a valid range reference
maybe you wanted to reference Cells(1, i)
furthermore you can exploit the Application VLookup() method that wraps the possible error in the returned variant variable that you can check with IsError() function like follows:
Dim temp As Variant
For i = 1 To 10
Set myrange = Range(Cells(1, i), Cells(10, i))
temp = Application.VLookup(Cells(1, i), [holidays], 2, False)
If Not IsError(temp) Then Cells(1, i).Interior.Color = 3
Next i
Here is a conditional formatting method, without using VBA.
Select your range > Conditional Formating > New Rule > Use a formula ...
Enter this formula
=VLOOKUP($A2,$J$2:$K$6,1,FALSE)
Take care of the "$" in the formula. This should highlight all cells that were found in the holidays list.
Your code is okay , It worked in Excel 2010 , Your problem is with VBA Error handling method.
Go to Tools --> Options -->General --> Error Trapping
And check "Break on unhanded Errors"
sorry all these times I was referring to column 2 in vlookup. That was causing the problem. The list of holiday is a single column list. Hence vlookup was throwing error. ANd one more thing the named ranges work as I have entered and even the actual range also gives the same result.
Sub highlight_cells()
Dim myrange As Range
For i = 1 To 10
Set myrange = Range(Cells(1, i), Cells(10, i))
MsgBox Cells(1, i)
temp = Application.VLookup(Cells(1, i), [holidays], 1, False)
If Not IsError(temp) Then
myrange.Interior.ColorIndex = 3
End If
Next i
End Sub

How to match strings in cells on Excel, with if/or operators, and delete the rows

Disclaimer: I've never used Visual Basic and I've never made a macro.
I am trying to create a macro in Microsoft Excel 2010 that will delete all rows where neither column G nor column I contain the string "Ohio", "Indiana", or "Kentucky". To clarify, the row should be deleted if the cell does not contain either of those three state names. I want the macro to start at row 6, as rows 1-5 contain vital information. The whole sheet contains over 14000 rows and only ~1.5% of those are actually helpful.
Also, I am looking to be able to reuse this macro, but for other terms (besides Ohio, Indiana, and Kentucky) in other columns (besides G and I).
It may also help me if you can, besides correcting this, explain what exactly I am saying in these lines. Perhaps in Java terms, or Python is okay too. Not necessary, but may help.
Sub DeleteIfNotKYINOH()
Dim i, LastRow
LastRow = Range("G" & Rows.Count).End(xlUp).Row
For i = LastRow To 6 Step -1
I get a type mismatch error on the next line.
If Cells(i, "G").Value = "Ohio" Or "Indiana" Or "Kentucky" Then
Cells(i, "G").Value = True
End If
If Cells(i, "I").Value = "Ohio" Or "Indiana" Or "Kentucky" Then
Cells(i, "I").Value = True
End If
If Cells(i, "G").Value Or Cells(i, "I").Value = False Then
Cells(i, "G").EntireRow.Delete
End If
Next
' DeleteIfNotKYINOH Macro
' Delete all rows that do not contain Ohio, Indiana, or Kentucky, as a state.
'
'
End Sub
There are a few things to consider, it looks like you are on the right track, though, you even got the backwards iteration over the collection (this stumps a lot of people!).
Make sure to declare your variables properly (i and LastRow should probably be Long integer, not unspecified Variant type).
If statements can include Or joins, but have to be like this:
If Cells(i, "G").Value = "Ohio" Or Cells(i, "G").Value = "Indiana" Or Cells(i, "G").Value = "Kentucky"
Since you want to be able to re-use the macro for other strings, of course you could go in and edit each instance of "Ohio" or "Indiana", etc., but that can be tedious and error-prone.
You could do something like this instead to re-use it for a list of any number of states, just change the assignment to the states variable.
Const states as String = "Ohio,Indiana,Kentucky"
Sub TestDeleteIfNot()
Dim i as Long, LastRow as Long
Dim cl as Range
LastRow = Range("G" & Rows.Count).End(xlUp).Row
For i = LastRow To 6 Step -1
With Cells(i, "G")
If Not(InList(.Value, states)) And Not(InList(.Offset(0,2).Value, states))
.EntireRow.Delete
End If
End With
Next
End Sub
This routine calls on a function InList which accepts as its arguments two strings. The first string should be the value being compared, and the second is a comma-delimited "list" of allowable values.
Function InList(strVal as String, list as String) As Boolean
Dim a as Variant
For each a in Split(list, ",")
If strVal = a Then
InList = True
Exit For
End If
Next
End Function
The function converts the list to an array and iterates that against the compare value. It should return False if the value is not found. So then the logic in the calling sub runs this on cells in COlumn G and also Column I, only deleting the row if BOTH tests return False.