Unable to run the copy code in Excel VBA - vba

I am not able to run the below copy code, getting error Application -defined or Object-Defined error. This code was running earlier but is now giving the error. Please help me as I am not able to understand what went wrong.
For i = Start To last
'MsgBox ActiveSheet.Cells(i, 17).Value, vbInformation
If (ActiveSheet.Cells(i, 17).Value >= ActiveSheet.Cells(1, 34).Value) Then
'MsgBox ActiveSheet.Cells(i, 17).Value, vbInformation
Worksheets("Sample Data").Range(Cells(i, 1), Cells(i, 2)).Copy
Worksheets("Output").Cells(j, 2).PasteSpecial xlValues
j = j + 1
End If
Next i
I am able to run it line by line but getting the error when pressingf5.

The code runs fine now on restart. This can be ignored as the code is correct.

Related

Inconsistent Runtime error '1004' - Delete method of range class failed

I have written some VBA in excel to iterate a relatively large set of data in order to perform some basic formatting and tidying up. The code previously worked and still works when I use a break point and step through it. However, when i don't step through it and just let the code run by itself, the spreadsheet becomes unresponsive. Once I've pressed escape a few times I get the runtime 1004 error (see title) and the relevant line is hightlighted.
I'd like to reiterate that when i set a break point on the offending line and step through it, it works fine and the row is deleted as appropriate. Also when I test the syntax in a much smaller data set it works perfectly so this is not syntax related.
Any help will be greatly appreciated because I'm stumped. Here is the code:
Private Sub CommandButton21_Click()
Dim index As Integer, lapsedSchemes As New Collection, lapseDates As New
Collection
Dim iRow As Long
Dim iCol As Integer
Sheet1.Columns(5).NumberFormat = "dd-mmm-yy"
Sheet1.Columns(14).NumberFormat = "dd-mmm-yy"
iRow = 2
Do While Sheet3.Cells(iRow, 1).Value <> ""
lapsedSchemes.Add Sheet3.Cells(iRow, 1).Value
lapseDates.Add Sheet3.Cells(iRow, 2).Value
iRow = iRow + 1
Loop
iRow = 2
Do While Cells(iRow, 7).Value <> ""
index = 1
If Cells(iRow, 9).Value = "" Then
If Cells(iRow, 7).Value = Cells(iRow, 8).Value Then
Cells(iRow, 9).Value = "Policy Holder"
Else
Cells(iRow, 9).Value = "Dependant"
End If
End If
For Each scheme In lapsedSchemes
If Cells(iRow, 4).Value = scheme And Cells(iRow, 5) = lapseDates(index) Then
Cells(iRow, 4).EntireRow.Delete
iRow = iRow - 1
Exit For
End If
index = index + 1
Next scheme
iRow = iRow + 1
Loop
appendLegacyData (iRow)
Range("A1:AZ10000").Sort Key1:=Range("A1"), Order1:=xlAscending, Key2:=Range("D1"), Order2:=xlAscending, Key3:=Range("E1"), Order3:=xlAscending, Header:=xlYes
End Sub
The offending line is:
Cells(iRow, 4).EntireRow.Delete
The sheet becomes unresponsive and only when I press escape a few times does the runtime error show.
Unresponsive doesn't mean that Excel crashed. Deleting rows one by one is just painfully slow.
You can use
application.statusbar=cstr(iRow)
DoEvents
to see your progress and keep Excel responsive.
If it will work to slow, I recommend you to set some value in rows to delete, sort by that value and then delete them all together.

VB Error - Object doesnt support this property or method

I am at a very basic part of VB and I am stuck with this relentless issue. I am trying to copy data from a form to a bunch of cells in excel and I am facing this error.
Object doesn't support this property or method: Runtime Error - 438
Although the first row gets inserted. The issue is with the second row. I don't see the problem. Kindly help
Sub sbInsertingRows()
Range("A3").EntireRow.Insert
End Sub
Sub EditAdd()
sbInsertingRows
Cells(3, 19).Value = Order_Details_Form.Controls(TextBox_orderid).Value
Cells(3, 20).Value = Order_Details_Form.Controls(TextBox_name).Value
Cells(3, 24).Value = Order_Details_Form.Controls(TextBox2).Value
ActiveWorkbook.Save
End Sub
EditAdd gets called on click
I think TextBox_orderid is a name of your textbox control not a variable so you should enclose it with " like:
Cells(3, 19).Value = Order_Details_Form.Controls("TextBox_orderid").Value
Or you can simply use:
Cells(3, 19).Value = Order_Details_Form.TextBox_orderid.Value
Apply this at your other two lines.

Delete All Rows With Errors in a column

I am working on a VBA code and part of the code is needed to delete all the rows in the sheet where the text in the column "J" is "#N/A". I have written a code but but I get a Type mismatch error when debugging it.
Here is the code
Dim i As Long
For i = Cells(Rows.Count, 10).End(xlUp).Row To 1 Step -1
If Cells(i, 10) = "#N/A" Then Cells(i, 1).EntireRow.Delete
Next i
Use the Range.SpecialCells method with the xlSpecialCellsValue constant as xlErrors to quickly identify all cells in column J with errors. It wasn't disclosed whether the cells were formulas or typed constants so I've added code to examine both through the xlCellType Enumeration types.
Sub del_error_rows()
With Worksheets("Sheet3")
On Error Resume Next
With .Columns(10).SpecialCells(xlCellTypeFormulas, xlErrors)
.EntireRow.Delete
End With
With .Columns(10).SpecialCells(xlCellTypeConstants, xlErrors)
.EntireRow.Delete
End With
On Error GoTo 0
End With
End Sub
The On Error Resume Next is necessary in case there are no cells with that particular error configuration. In that case, the SpecialCells would be Nothing and you must bypass any error thrown from attempting to handle nothing.
Try this code:
Dim i As Long
For i = Cells(Rows.Count, 10).End(xlUp).Row To 1 Step -1
If Cells(i, 10).Text = "#N/A" Then Cells(i, 1).EntireRow.Delete
Next i

loop copy paste randomly leaving out data

has anyone had an issue in excel vba where a loop to copy and paste rows and check for certain criteria sometimes leaves out information and after i try to debug and run through it works fine
I have a loop
For rnum = 3 To LastRow
'if all cells are equal to the Comboboxes then copy and paste the row
ThisWorkbook.Activate
If Sheets("Revised Budget").Cells(rnum, 1).Value = BUval And _
Sheets("Revised Budget").Cells(rnum, 2).Value = TeamLocVal And _
Sheets("Revised Budget").Cells(rnum, 3).Value = YrVal And _
Sheets("Revised Budget").Cells(rnum, 9).Value = InstVal Then
Sheets("Revised Budget").Range(Cells(rnum, 1), Cells(rnum, 12)).Copy
NewWkbk.Sheets("Actual").Activate
If NewWkbk.Sheets("Actual").Range("A1").Offset(1, 0).Value = "" Then
NewWkbk.Sheets("Actual").Range("A1").Offset(1, 0).PasteSpecial
Else
NewWkbk.Sheets("Actual").Range("A1").End(xlDown).Offset(1, 0).PasteSpecial
End If
Application.CutCopyMode = False
End If
Next rnum
Are your variables strings or integers?
If any of them are strings you can try making sure it's not an issue with capitalization on your string matches.
ucase(Sheets("Revised Budget").Cells(rnum, 3).Value) = ucase(YrVal)
Consider updating all of your .value to .value2. I was running into a (potentially) similar issue, and that fixed it for me.
I think I may have figured it out through some more research. The page in which the macro was running through had data that was filtered. I put in a code that would ShowAllData and I believe that should fix the issue.

Using multiple if/then statements without getting a runtime error

New to vba and trying to write a simple if/then statement in sequential order to return a value from different sheets.
If Cells(4, 4).Value = "water stress" Then
Cells(15, 4).Value = Application.WorksheetFunction.VLookup(region, ws2.Range("a3:b12"), 2)
End If
If Cells(4, 4).Value = "fire" Then
Cells(15, 4).Value = Application.WorksheetFunction.VLookup(region, ws3.Range("a3:b12"), 2)
End If
When i run the code with only the first if statement, it runs fine. When I add in the second if statement, I get a run-time error '1004' and am unable to get the vlookup property of the worksheetfunction class. I am new, and not sure how to trouble shoot.