Array returning #N/A

Jotoya

Active Member
Joined
May 25, 2003
Messages
366
Hello,

I receive an automatically generated report that contains a single sheet with around 540,000 used rows. In column J the file contains a date/time format that looks like this: 2023-08-02T16:26:42 I have emphasized the "T". The file is a .csv file. I have used the below code to remove the "T" in another csv file with larger number of rows used (790,000) successfully but with this file, after about 150,000 rows it starts generating a #N/A. I have checked for extra spaces and all good in that front.

VBA Code:
Sub RemoveTFromData ()

    Dim v As Long
    Dim LastRowJ As Long
    Dim arrJ() As Variant
        
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
'   Define variables.
    LastRowJ = Sheets("Report").Range("J" & Rows.Count).End(xlUp).Row
    
'   Define and load Arrays to be used for columns J.
    arrJ = Application.Transpose(Sheets("Report").Range("J3:J" & LastRowJ))

'   Loop through arrays to remove "T" value and re-format column J.
    For v = 1 To UBound(arrJ)
        arrJ(v) = Replace(arrJ(v), "T", " ")
    Next v

'   Write arrays back to column.
    Sheets("Report").Range("J3:J" & LastRowJ) = Application.Transpose(arrJ)
    
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True

End Sub

Any assistance is greatly appreciated.
 
You are making that much harder than it needs to be, try this:
(this should be very fast)

VBA Code:
Sub RemoveTFromDatav02()

    Dim wsRpt As Worksheet
    Dim rngJ As Range
    Dim LastRowJ As Long
    Dim arrJ() As Variant
     
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
'   Define variables.
    Set wsRpt = Worksheets("Report")
    LastRowJ = wsRpt.Range("J" & Rows.Count).End(xlUp).Row
    Set rngJ = wsRpt.Range("J3:J" & LastRowJ)
 
'   Define and load Arrays to be used for columns J.
    arrJ = rngJ.Value2
'   Remove "T" value and re-format column J.
    arrJ = Application.Substitute(arrJ, "T", " ")

'   Write arrays back to column.
    rngJ.Value2 = arrJ
 
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True

End Sub
 
Upvote 1
Solution

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
You could also just do a Find/Replace as below. For my 400,000+ row test data it did take longer than the array method above (10 seconds v about 1.2 seconds) but not too long a wait.

VBA Code:
Sub Replace_T_v1()
  Columns("J").Replace What:="T", Replacement:=" ", LookAt:=xlPart
End Sub
Many, many thanks Peter! I will most certainly give it a shot here in a bit and report back!
 
Upvote 0
Many, many thanks Peter! I will most certainly give it a shot here in a bit and report back!
You're welcome. Note though that although that code is very short, it isn't anywhere near as fast as post 9 or post 11.
 
Upvote 0
You are making that much harder than it needs to be, try this:
(this should be very fast)

VBA Code:
Sub RemoveTFromDatav02()

    Dim wsRpt As Worksheet
    Dim rngJ As Range
    Dim LastRowJ As Long
    Dim arrJ() As Variant
    
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
'   Define variables.
    Set wsRpt = Worksheets("Report")
    LastRowJ = wsRpt.Range("J" & Rows.Count).End(xlUp).Row
    Set rngJ = wsRpt.Range("J3:J" & LastRowJ)
 
'   Define and load Arrays to be used for columns J.
    arrJ = rngJ.Value2
'   Remove "T" value and re-format column J.
    arrJ = Application.Substitute(arrJ, "T", " ")

'   Write arrays back to column.
    rngJ.Value2 = arrJ
 
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True

End Sub
Many thanks Alex! I'll give your method a go as well. I appreciate you posting it. 👍
 
Upvote 0

Forum statistics

Threads
1,224,816
Messages
6,181,143
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