VBA: fix code to delete rows and alpha characters

wizardofsloth

New Member
Joined
Mar 5, 2021
Messages
2
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I'm having issues while importing and deleting data and unimportant cells into excel using ActiveCell. Small data imports work just fine, but large amounts of data gives me Method "Range" of object '_Global' failed

Below is the VBA code used. I would like to avoid ActiveCell, what would be the best way to go about doing this?



VBA Code:
 IdxMsg1.TextBox1.Value = "Checking Data"
        IdxMsg1.Repaint
        Range("startImport").Offset(0, 2).Select
        i = 0
        j = 0
        Do While i < intC
            i = i + 1
            work = ActiveCell.Value
            check = IsNumeric(work)
            If check = True Then
                ActiveCell.Offset(1, 0).Select
            Else
                ActiveCell.EntireRow.Delete
                j = j + 1
                Range("StartImport").Offset(i - 2, 2).Select
            End If
Loop
intC = intC - j
Range("TotalEntries").Value = intC
Range("StartImport", "EndImport").Select
Selection.Font.ColorIndex = 5
Selection.Interior.ColorIndex = 19
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college

Alex Blakenburg

Well-known Member
Joined
Feb 23, 2021
Messages
615
Office Version
  1. 365
Platform
  1. Windows
I am not a 100% clear on what the code is doing but it does seem to be a delete rows loop.
Typically for a delete rows loop you want to start at the bottom and work you way up. Each delete row requires Excel to rearrange all the rows under the delete so to minimise this start at the bottom.

In your case it might look something like this:-
(Untested)
I a bit unclear as to on what row your data starts. You may need to add or subtract 1 from the StartRow in the For statement.

VBA Code:
Sub DeleteRows()

    Dim i As Long
    Dim intC As Long
    Dim StartRow As Long
    Dim StartCol As Long
    Dim criteriaCol As Long
    
    StartRow = Range("startImport").Row
    StartCol = Range("startImport").Column
    criteriaCol = StartCol + 2
        
    For i = intC To StartRow Step -1
    
        If Not IsNumeric(Cells(i, criteriaCol).Value) Then
            Cells(i, criteriaCol).EntireRow.Delete
        End If    
    Next i

End Sub
 
Solution

Watch MrExcel Video

Forum statistics

Threads
1,130,174
Messages
5,640,593
Members
417,152
Latest member
DayTimeSeby

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top