I'm trying to use if statement inside a For loop and it is giving Runtime Error 1004 “Application-defined or Object-defined error”. I am using the below code(I have commented where the error occurs). I think it is because of cell referencing that I've used for the for loop.
Please help.
Sub ifcheck()
Dim i As Integer
For i = 1 To 5
Cells(3, 2).Select
ActiveCell.FormulaR1C1 = _
"=IF(AND(Availability!R3C2>=Interviewers!R[i]C[1],Availability!R3C1>=Interviewers!R[i]C[3]),Interviewers!R4C2,"""")"
'>>>The above line shows as error 1004 - “Application-defined or Object-defined error”
Range("B3").Select
Next i
End Sub
Thanks,
Jay.
Try splicing i into the formula with basic string concatenation.
Dim i As long
For i = 1 To 5
Cells(2+i, 2).FormulaR1C1 = _
"=IF(AND(Availability!R3C2>=Interviewers!R[" & i & "]C[1], Availability!R3C1>=Interviewers!R[" & i & "]C[3]), Interviewers!R4C2, text(,))"
Next i
Since cells(3, 2) is B3, I don't know what you are intending to do for successive loops. I've moved down one row for each loop.
Related
I use this code to apply a length formula and then Autofill till the last visible cell but getting an error
Runtime Error '1004'- Method 'Range' of object_Global' failed
Code
Range("C2").Select
ActiveCell.FormulaR1C1 = "=LEN(RC[-1])"
Selection.AutoFill Destination:=Range("C2:C" & Lastrow).SpecialCells(xlCellTypeVisible).Select
Selection.Copy
from your code it seems you want the length of cells in COl B. The below code works for me.
Sub x()
Range("C2:C" & Range("B" & Rows.Count).End(xlUp).Row).SpecialCells(xlCellTypeVisible).FormulaR1C1 = "=LEN(RC[-1])"
End Sub
As always it's better to stay away from Select, ActiveCell and Selection.
Try the code below:
Dim FitRng As Range, Lastrow As Long
Range("C2").FormulaR1C1 = "=LEN(RC[-1])"
Set FitRng = Range("C2:C" & Lastrow).SpecialCells(xlCellTypeVisible)
FitRng.FillDown
If you don't want to use the FillDown method, you can simply use:
FitRng.FormulaR1C1 = "=LEN(RC[-1])"
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
I have an issue with the following lines giving me the run-time error referenced in the title, with the first line of the if statement being highlighted as the culprit:
Sub setupFirstWorksheet()
For i = 0 To Worksheets(1).UsedRange.Rows.Count
If Range("h" & i).Value = 1010 Or Range("j" & i).Value < 0 Or Range("k" & i).Value = False Then
[some code or blank]
End If
Next i
End Sub
However, putting in similar code below, I have no issues:
Sub test()
For i = 0 To Worksheets(1).UsedRange.Rows.Count
Next i
MsgBox Range("h" & i).Value
End Sub
Any help with this would be much appreciated. Thank you.
You are starting your loop at 0, which means you are generating range references H0,J0 & K0. These are invalid because there is no such thing as row 0.
Change your loop to start at 1 and you will be okay.
The reason your test works is because your MsgBox line is only executed after the loop has finished, and i will equal Worksheets(1).UsedRange.Rows.Count. If you move the MsgBox statement into the loop, you will find that it will also fail.
I'm just getting familiar with VBA and my code
xxLastrow = xLastRow+1
For x = 11 To xLastRow
For y = 12 To xxLastrow
If ActiveSheet.Cells(x, 2).Value = Cells(y, 2).Value Then
For z = 4 To xLastColumn
If ActiveSheet.Cells(y, z).Value <> "" Then '(possible If Not IsEmpty(Cells(y, z).Value))
ActiveSheet.Cells(y, z).Select
Selection.Copy
ActiveSheet.Cells(x, z).Select
ActiveSheet.Paste
End If
Next z
End If
Next y
Next x
makes Run-time error 1004: Application-defined or object-defined error when the line "If ActiveSheet.Cells(y, z).Value <> "" Then" goes. All variables are integer (including lastrow/column). What's the problem here?
I promise sea of cats for man who will try to help :)
This i smoe of a comment than an answer, but as code looks ugly in a comment I thought I would post as answer.
You can accomplish your same results with a much more efficient code, the below code uses value = value instead of copy paste, as to avoid using the clipboard (that itself is a buggy slow process). It also does not loop every single column reducing your loops by (Row matches * Column Count) number of times, probably a lot.
Dim rngTest As Range
Dim rngCur As Range
Dim rngCOff As Range
Set rngTest = Range("B11:B" & xLastRow)
For Each rngCur In rngTest
Set rngCOff = rngCur.Offset(1)
If rngCur.Value = rngCOff.Value Then
rngCOff.Offset(, 2).Resize(, xLastColumn - 4).Value = rngCur.Offset(, 2).Resize(, xLastColumn - 4).Value
End If
Next rngCur
I'm pretty new to VBA and having trouble creating a quick macro used to move blocks of numbers around.
What I am trying to create is a button that when pressed:
Moves the contents of (i,5) to E63
The cells from (i, 16) down to F67:F110
Dependant on whether Row 10 contains "Low" or "High" moves three cells from the set
N106:N109 to the cells (i12:i14) [Where i is the column reference).
The Range sections of code are what accomplish this and they are working fine, the problem I am having is with my Do.Until row and with the reference Column(i)
Does anyone know how this could work? Thanks
UPDATE
So thanks to the help of Siddharth I've been able to fix all but one bit, which is the lines where there is a string in the Range function. The reason I am not using .Formula here but Paste instead is that otherwise all of the cells A12:A14 to Z12:Z14 will equal the same thing which isn't correct. On the other parts that doesn't matter. I am getting a type 13 mismatch error on these lines.
Sub Columntest()
Dim i As Integer
i = 5
Do Until Cells(5, i).Value = ""
If Cells(10, i).Value = "Low" Then
Range("E63").Formula = Cells(5, i)
Range("F67:F110").Formula = Cells(16, i)
Range("O106:O108").Copy
Range("=" & Columns(i) & "12").PasteSpecial Paste:=xlPasteValues
End If
If Cells(10, i).Value = "High" Then
Range("E63").Formula = Cells(5, i)
Range("F67:F110").Formula = Cells(16, i)
Range("N106:N108").Copy
Range(Columns(i) & "12").PasteSpecial Paste:=xlPasteValues
End If
i = i + 1
Loop
End Sub
The type mismatch is because you are trying to concatenate a column object and a string in the range reference:
Range("=" & Columns(i) & "12").PasteSpecial Paste:=xlPasteValues
Try using this instead:
Cells(12, i).PasteSpecial Paste:=xlPasteValues