Help on making this code Run faster, Takes like a minute

DarrenBurke

New Member
Joined
May 6, 2022
Messages
29
Office Version
  1. 2016
  2. 2007
Platform
  1. Windows
Hi, Guys and gals,
Can i tweak this code to run faster.
What are you trying to do ? I want the code to check colum "A" for empty cells and delete the entire row. (Basically the telesales did not capture the correct info, therefore the row of info is useless and needs to be deleted. ) It works as is but takes a long time to finish. I have 10 other sheets to apply it to with a macro on each sheet. Thank you to the Hive Minds :)

VBA Code:
Sub Delete_Rows_with_Blank_Cells_in_Single_Column()

Worksheets("MichaelF").Activate

Set Rng = ActiveSheet.UsedRange

Blank_Cells_Column = 1

For i = Rng.Rows.Count To 1 Step -1
    If Rng.Cells(i, Blank_Cells_Column) = "" Then
        Rng.Cells(i, Blank_Cells_Column).EntireRow.Delete
    End If
Next i
   
End Sub

I hope i did the code correctly

Cheers
 
Thank you so much Peter it works in milliseconds.... WOW!!
Thank you Fluff, Both Champions
Cheers from a chilly South Africa
Hi Peter, Sorry , The code runs perfect, when clicked on, removes all empty cells and the associated row. If you click it a second time without closing and opening the workbook it gives an error.... a bit like saying " i have done my bit what now lol" I will send a snapshot , Thank you for your help. :)
 

Attachments

  • errorOnSecondClicka.JPG
    errorOnSecondClicka.JPG
    97.5 KB · Views: 11
  • errorOnSecondClick.JPG
    errorOnSecondClick.JPG
    98.1 KB · Views: 11
Upvote 0

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
I don't think it is related to closing or opening the workbook.
From your image, it looks like the table is empty. I'm wondering why you are running the code again, particularly if the table is already empty?
 
Upvote 0
I don't think it is related to closing or opening the workbook.
From your image, it looks like the table is empty. I'm wondering why you are running the code again, particularly if the table is already empty?
100 % correct, it had empty lines when first run. and it cleared them. I will diasable the button after click and set "msgbox The Blank Lines Have been Cleared" ...... when workbook reopens the button click will be active again for one click again. Humans are in curios by design LOL. Thank you. Ps,, Louise's sheet had 3 records captured 2 blank rows (these were deleted and one record captured) she has only one row of information at the moment, see snapshot.

how it works.
When the workbook opens, the data connections run(Automatically) and fetch any new captured info. this is then placed in the individual green sheet tabs at the bottom.
The manager then clicks the top left Button in red. This clears the sheet and and also clears any blank rows in the green sheet tabs. Then She clicks on "Fetch that new info"

I think i will upload the mini worksheet, then you can see it in action, just ignore the data connecion errors.
It only fails when the sheet has 1 record in it( Louise is a newbie and finding her feet)

GetdatafromFundRaisers.xlsm
BCDEFGHIJKL
1Jacqui's DataMariana's DataTrevor's DataSuzette's DataKasia's DataSean
2Kevin's DataRuth's DataKerry-Lee's DataTheresa's DataLouise's Data
3CapturedREPCODEP/U DATECOMPANYADDRESSX-ROAD/SUBURBAREAAMOUNTDrivers
4
52022-06-16MF1SUPA QUICK EDENVALEC/R 10 TH STREDENVALEEDENVALE
62022-06-16MF1TALLISMAN HIRE BENONI92 GOUSBLOM STRNORTHMEADBENONI
72022-06-161
82022-06-14MF30002022-06-15FAME FENCE(PTY)LTD180 BROADWALK STRMIDRAND
92022-06-14MF3182022-06-15NORTH LAWNMOWERS709 PRESIDENT STEYN STRPRETORIA
102022-06-13MF3162022-06-14ROSE LIEBENBERGC/R 10 TH STREDENVALEEDENVALER200,00
112022-06-13MF3142022-06-14SCOTSMAN ICE SYSTEMS18 YALDWYN ROADJETPARKBOKSBURG
122022-06-08MF3012022-06-09LUV MOR MUSIC1 5TH AVEMELVILLEJOHANNESBURGR200,00
132022-06-08MF3032022-06-09IRENE SAUNDERS87 RADIO STR0ALBERTONR200,00
142022-06-08MF3042022-06-09LYNS PRE-SCHOOLCNR PITTS + KORHAAN STREETSUNNYRIDGEPRIMROSER100,00
152022-06-07MF2992022-06-09GRAYTEX METALS375 KRUGER STREETSTRIJOMPARKRANDBURGR400,00
162022-06-07MF2962022-06-09JMR TRAILERPARTS GAUTENG(PTY)LTD4 BORAX STREETALRODEALBERTONR400,00
172022-06-07MF2972022-06-09ISA VAN SCHALKWYK1 BRUTON AVENUE0BRYANSTONR400,00
182022-06-07MF2982022-06-09ALMECH PTY LTD23 GOLDEN DRIVEMOREHILLBENONIR4 800,00
192022-06-02MF2872022-06-03SCOTSMAN ICE SYSTEMS18 YALDWYN ROADJETPARKBOKSBURGR500,00
202022-06-01MF2922022-06-02CAR CLINIC49 SHAFT ROADKNIGHTSGERMISTONR100,00
212022-05-31MF2882022-06-01MARK MELTZER57 LAKE RDCARTER AGENCIES(PTY)LTDEDENVALER550,00
222022-05-31MF2892022-06-01AUTOBAR688 SECCILL AVEGEZINAPTAR500,00
232022-05-31MF2902022-06-01M & M HYDRAULICS CC27A GOLDEN DRIVE.MOREHILLBENONIR100,00
242022-05-30MF12822022-05-31MUGG AND BEANH/V NELMAPIUS&0PRETORIAR500,00
252022-05-30MF12812022-05-31CARLOS DA COSTA28 LOWTHER STREETWITFIELDBOKSBURGR100,00
262022-05-30MF2782022-05-31TOTAL CAMPBELLS333 HARRY GALAUN DRIVEVORNA VALLEYMIDRANDR200,00
272022-05-30MF2802022-05-31LANDYTECH1 TILE RDANDERBOLTBOKSBURGR200,00
282022-05-24MF2752022-05-25GRAYTEX METALS375 KRUGER STREETSTRIJOMPARKRANDBURGR200,00
292022-05-24MF2762022-05-25EASYLIFE KITCHEN9 COMMERCE CRESENTEASTGATE EXTSANDTONR300,00
302022-05-24MF2772022-05-25ISA VAN SCHALKWYK1 BRUTON AVENUE0BRYANSTONR600,00
312022-05-23MF2742022-05-25NTT ISUZU TZANEEN77 OLD GRAVELOTTE RDTZANEENR600,00
322022-05-23MF2732022-05-24UPPER DECK SPORTS CAFE&BISTROCNR PIET RETIEF ROAD NELSPRUITR300,00
CodeT
Cells with Data Validation
CellAllowCriteria
K3List=#REF!
 
Upvote 0
Sorry did not copy the buttons this is what it looks like
 

Attachments

  • buttons.JPG
    buttons.JPG
    94.7 KB · Views: 6
Upvote 0
If you wanted the code to deal with already cleared tables, or tables with only one data row (that is what caused the error you reported previously) yo could try this version.

VBA Code:
Sub Del_Rws_v2()
  Dim a As Variant, b As Variant
  Dim nc As Long, i As Long, k As Long, Blank_Cells_Column As Long
  
  Blank_Cells_Column = 1
  
  With Sheets("MichaelF").ListObjects(1)
    If .DataBodyRange.Rows.Count > 1 Then
      a = .DataBodyRange.Columns(Blank_Cells_Column).Value
      ReDim b(1 To UBound(a), 1 To 1)
      For i = 1 To UBound(a)
        If Len(a(i, 1)) = 0 Then
          b(i, 1) = 1
          k = k + 1
        End If
      Next i
      If k > 0 Then
        Application.ScreenUpdating = False
        .ListColumns.Add
        With .DataBodyRange
          nc = .Columns.Count
          .Columns(nc).Value = b
          .Sort Key1:=.Columns(nc), Order1:=xlAscending, Header:=xlYes
          .Resize(k).EntireRow.Delete
        End With
        .ListColumns(nc).Delete
        Application.ScreenUpdating = True
      End If
    Else
      If Len(.DataBodyRange.Cells(1, Blank_Cells_Column).Value) = 0 Then .DataBodyRange.ClearContents
    End If
  End With
End Sub
 
Upvote 0
Solution
In the version I was playing around with I had the additional If statement below, to stop it erroring out when the Table had been totally cleared.

VBA Code:
    With Sheets("MichaelF").ListObjects(1)
        ' Check for an empty table
        If .DataBodyRange Is Nothing Then Exit Sub
 
Upvote 0
If you wanted the code to deal with already cleared tables, or tables with only one data row (that is what caused the error you reported previously) yo could try this version.

VBA Code:
Sub Del_Rws_v2()
  Dim a As Variant, b As Variant
  Dim nc As Long, i As Long, k As Long, Blank_Cells_Column As Long
 
  Blank_Cells_Column = 1
 
  With Sheets("MichaelF").ListObjects(1)
    If .DataBodyRange.Rows.Count > 1 Then
      a = .DataBodyRange.Columns(Blank_Cells_Column).Value
      ReDim b(1 To UBound(a), 1 To 1)
      For i = 1 To UBound(a)
        If Len(a(i, 1)) = 0 Then
          b(i, 1) = 1
          k = k + 1
        End If
      Next i
      If k > 0 Then
        Application.ScreenUpdating = False
        .ListColumns.Add
        With .DataBodyRange
          nc = .Columns.Count
          .Columns(nc).Value = b
          .Sort Key1:=.Columns(nc), Order1:=xlAscending, Header:=xlYes
          .Resize(k).EntireRow.Delete
        End With
        .ListColumns(nc).Delete
        Application.ScreenUpdating = True
      End If
    Else
      If Len(.DataBodyRange.Cells(1, Blank_Cells_Column).Value) = 0 Then .DataBodyRange.ClearContents
    End If
  End With
End Sub
Thank you Peter, i will give it a spin, let you know. :)
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

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
Back
Top