Tunning a VBA macro

Guinaba

Board Regular
Joined
Sep 19, 2018
Messages
233
Office Version
  1. 2016
Platform
  1. Windows
Hi guys,

Is there a way to make this code running faster? This macro is running on 700,000 rows and converting the range into a table.

VBA Code:
Sub Data_Array_Set_IBPData_1(vDtaHdr() As Variant, vDtaBdy() As Variant)
   Dim wrksht As Worksheet
   Dim objListObj As ListObject
   Dim vArray As Variant
   
   Dim LRow As Long
   Dim i As Long

  'Find the last non-blank cell in column A(1)
   LRow = ThisWorkbook.Worksheets("IBP Data").Cells(Rows.Count, 2).End(xlUp).Row
  
   With ThisWorkbook.Worksheets("IBP Data").Range(ThisWorkbook.Worksheets("IBP Data").Cells(2, 1), ThisWorkbook.Worksheets("IBP Data").Cells(LRow, 9))
        vArray = .Rows(1)
        vDtaHdr = vArray
        vArray = .Offset(1, 0).Resize(-1 + .Rows.Count)
        For i = 1 To UBound(vArray)
            If IsDate(vArray(i, 1)) Then
                vArray(i, 1) = CDate(vArray(i, 1))
            End If
        Next i
        vDtaBdy = vArray
  End With
End Sub
  
Sub IBPData_1()

    Dim MyTable As ListObject
    Dim vDtaHdr() As Variant, vDtaBdy() As Variant
    Dim lRowsAdj As Long
        
    Application.Calculation = xlCalculationManual
    Application.ScreenUpdating = False

    Set MyTable = ThisWorkbook.Worksheets("IBP Data").ListObjects("tFcst_1") 'Change as required

    Call Data_Array_Set_IBPData_1(vDtaHdr, vDtaBdy)

    With MyTable.DataBodyRange
        Rem Get Number of Rows to Adjust
        lRowsAdj = 1 + UBound(vDtaBdy, 1) - LBound(vDtaBdy, 1) - .Rows.Count

        Rem Resize ListObject
        If lRowsAdj < 0 Then
            Rem Delete Rows
            .Rows(1).Resize(Abs(lRowsAdj)).Delete xlShiftUp

        ElseIf lRowsAdj > 0 Then
            Rem Insert Rows
            .Rows(1).Resize(lRowsAdj).Insert Shift:=xlDown

    End If: End With

    Rem Overwrite Table with New Data
    MyTable.HeaderRowRange.Value = vDtaHdr
    MyTable.DataBodyRange.Value = vDtaBdy
      
   Application.Calculation = xlCalculationAutomatic
   Application.ScreenUpdating = True
   
   MsgBox "Table has been refreshed", vbInformation
   
End Sub
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Test this on a COPY of your Workbook and see if it produces the expected results.
VBA Code:
Option Explicit

Sub ConvertToTable()
Dim wb As Workbook, sht As Worksheet, rng As Range, lRow As Long
Set wb = ThisWorkbook
Set sht = wb.Sheets("IPB Data")
lRow = wb.sht.Cells(Rows.Count, 2).End(xlUp).Row
Set rng = Range(sht.Cells(1, 1), sht.Cells(lRow, 9))
sht.ListObjects.Add xlSrcRange, rng, XlListObjectHasHeaders:=xlYes, Destination:=sht.Cells(1, 1)
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,813
Messages
6,181,116
Members
453,021
Latest member
Justyna P

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