My code returns with an error. The result is as wished, but the error message is rather annoying. How can I get rid of this?
Sub SELECTIE_VERZUIM()
Dim Cval As Variant
Dim Rng1 As Range
Cval = Sheet4.Range("A16").Value
Set Rng1 = Sheet4.Range("D1:T" & Cval).Select
End Sub
Run-time error '424':
Object required
Try:
Sub SELECTIE_VERZUIM()
Dim Cval As Variant
Cval = Sheet4.Range("A16").Value
Sheet4.Range("D1:T" & Cval).Select
End Sub
Also, you should avoid using Select in VBA
Related
I have the following code
Dim Shop1 As String
With Worksheets("Data Page")
Shop1 = Application.WorksheetFunction.Index(.Range("B2:B7"), Application.Worksheet.Match(profitable1, .Range("E2:E7"), 0))
End With
This is how my excel looks like
The code works at first, but after I change some value,it suddenly does not and the above error pops out.
Can someone tell me why? Thank you so much for helping
Application.Match vs WorksheetFunction.Match (WorksheetFunction.Index)
You have misspelled WorksheetFunction in your code as mentioned by
Raymond Wu in the comments: Worksheet.Match should be WorksheetFunction.Match.
The early-bound WorksheetFunction version of Match will raise an error if a value is not found so you will have to implement some kind of error handling. The late-bound Application version is preferred because it can be tested with IsNumeric or IsError.
I can't recall using Index/Match in VBA. It is usually handled as illustrated in the following code.
Option Explicit
Sub Test()
Dim sIndex As Variant ' a number or an error value, hence 'As Variant'
Dim profitable1 ' ?
Dim Shop1 As String ' this actually means Shop1 = ""
With ThisWorkbook.Worksheets("Data Page")
With .Range("E2:E7")
sIndex = Application.Match(profitable1, .Cells, 0)
If IsNumeric(sIndex) Then
Shop1 = CStr(.Cells(sIndex).EntireRow.Columns("B").Value)
' Or:
'Shop1 = CStr(.Cells(sIndex).Offset(, -3).Value)
'Else ' if the code is in a loop
' Shop1 = ""
End If
End With
End With
End Sub
It becomes simpler (more readable) when using range variables.
Sub Test2()
Dim sIndex As Variant ' a number or an error value, hence 'As Variant'
Dim profitable1 ' ?
Dim Shop1 As String ' this actually means Shop1 = ""
Dim lrg As Range ' Lookup
Dim vrg As Range ' Value
With ThisWorkbook.Worksheets("Data Page")
Set lrg = .Range("E2:E7")
Set vrg = .Range("B2:B7")
End With
sIndex = Application.Match(profitable1, lrg, 0)
If IsNumeric(sIndex) Then
Shop1 = CStr(vrg.Cells(sIndex).Value)
'Else ' if the code is in a loop
' Shop1 = ""
End If
End Sub
I am making a vlookup code that shows the user description when they scan item barcode but I am getting subscript out of range error
Dim ws As Worksheet
Set ws = Sheets("CONVERSION")
Dim itemcode As String
Dim description As String
Dim myrange As Range
ws.Activate
Set myrange = Range("A:B")
description = ws.Application.WorksheetFunction.VLookup(TextBox1.Value, Worksheets("CONVERSION").Range("myrange"), 2, False)
Label5 = description
Then it is supposed to assign the value of the vlookup (description) to the label
This should help:
Option Explicit
Sub Test()
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("CONVERSION") 'if you don't define the workbook it will be the activeworkbook
Dim itemcode As String
Dim description As Variant 'Defining it as variant won't rise an Error if the item is not found
Dim myrange As Range
Set myrange = ws.Range("A:B") 'If you define a worksheet, you can refer to it and you won't need .Select or .Activate
description = Application.VLookup(TextBox1.Value, myrange, 2, False) 'as before, once you defined your range you can simply refer to it
If Not IsError(description) Then
Label5 = description
Else 'if nothing is found description will be an error
Label5 = "Item not found"
End If
End Sub
Application.VLookUp instead using the WorksheetFunction will prevent VBA to rise an error in case nothing is found when it's applied to a Variant so you can use that little trick to later do the if I've posted you in case your BarCode isn't yet on your database.
Ok Finaly Fixed the Problem , not the way i originaly thought but it does the job , i stopped using vlookup and did this
Dim Found As Range
Dim str As String
str = Me.TextBox1.Text
Set Found = Sheet2.Range("A2", Range("A" & Rows.Count).End(xlUp)).Find(str)
If Found Is Nothing Then
Label5 = "Not Found"
Else
Label5 = Cells(Found.Row, 2).Value
End If
i found the solution here
Can't figure out what's wrong with the code.
I have a code where I need to check if in certain sheet can be found certain products' ID before making an order.
Private Sub Pardot_Click()
Dim xlRange As Range
Dim xlCell As Range
Dim xlSheet As Worksheet
Dim valueToFind As String
valueToFind = pardID
Set xlSheet = ActiveWorkbook.Worksheets("Noliktava")
Set xlRange = xlSheet.Range("A1:A500")
For Each xlCell In xlRange
If xlCell.Value <> valueToFind Then
MsgBox ("This product wasn't found in the database - ID: " & pardID.Text)
Exit Sub
End If
Next xlCell
End Sub
Basically, I launch the userform and type in the ID (i.e. 1) in the box and click "Okay" or whatever, if the ID can't be found in the range (ID:1) I want it to show the error Msg.
Code works if I change <> to =, but that's not the needed result.
Without changing your code too much, and probably saving some time, instead of checking each cell in the range, just use CountIf()?
Private Sub Pardot_Click()
Dim xlRange As Range
Dim xlCell As Range
Dim xlSheet As Worksheet
Dim valueToFind As String
valueToFind = pardID
Set xlSheet = ActiveWorkbook.Worksheets("Noliktava")
Set xlRange = xlSheet.Range("A1:A500")
If WorksheetFunction.CountIf(xlRange,valuetoFind) = 0 then
msgbox "This product wasn't found in the database - ID: " & parId.textEnd
End If
End Sub
Note: this will look for exactly the text the user inserted. Use wildcards if it can be somewhere in a string (i.e. search for "dog" in "doggone", "dog food","dog")
I keep getting the RunTime Error '1004', which says that my pastespecial method of a range class failed. All I'm trying to do is cut out 2 ranges and paste them into a cell right next to it so i can create an open, empty range. For some reason, the pastespecial method of the range class failed, and i can't figure out why. Here is the code below:
Sub ProductivityMacro()
Dim rDelete As Range
Dim rCut As Range
Dim rPaste As Object
Set rDelete = Range("A:A,C:D,F:J,M:BD")
rDelete.Delete (xlShiftToLeft)
Set rCut = Range("C:D")
rCut.Cut
Set rPaste = Range("E1")
rPaste.PasteSpecial xlPasteValues
End Sub
Sub ProductivityMacro()
Dim rDelete As Range
Dim rCut As Range
Dim rPaste As Object
Set rDelete = Range("A:A,C:D,F:J,M:BD")
rDelete.Delete (xlShiftToLeft)
Set rCut = Range("C:D")
rCut.Cut Range("E1")
End Sub
I am new in VBA coding. Lets say I am retrieving value from Sheet3.Cell(23, 4) for a value, is there any way in the VBA code which let me set this as a variable?
For example, I have changed the interface and let the value stay at Sheet4.Cell(20,1), everywhere in my code which refer to Sheet3.Cell(23, 4) need to be changed to Sheet4.Cell(20, 1). I am thinking is there any best practice for coding VBA for situation like this?
Yes. For that ensure that you declare the worksheet
For example
Previous Code
Sub Sample()
Dim ws As Worksheet
Set ws = Sheets("Sheet3")
Debug.Print ws.Cells(23, 4).Value
End Sub
New Code
Sub Sample()
Dim ws As Worksheet
Set ws = Sheets("Sheet4")
Debug.Print ws.Cells(23, 4).Value
End Sub
Yes, set the cell as a RANGE object one time and then use that RANGE object in your code:
Sub RangeExample()
Dim MyRNG As Range
Set MyRNG = Sheets("Sheet1").Cells(23, 4)
Debug.Print MyRNG.Value
End Sub
Alternately you can simply store the value of that cell in memory and reference the actual value, if that's all you really need. That variable can be Long or Double or Single if numeric, or String:
Sub ValueExample()
Dim MyVal As String
MyVal = Sheets("Sheet1").Cells(23, 4).Value
Debug.Print MyVal
End Sub