VBA Array not working - cannot write to array and read is too slow

ouvay

Board Regular
Joined
Jun 9, 2022
Messages
131
Office Version
  1. 2019
Platform
  1. Windows
Hello

I am writing a code to write this code to populate an array based on an If statement and then unload that array into a range.

Here is a sample of my code

VBA Code:
Sub UpdateFromPCT()

Dim wkb As Workbook
Dim ws As Worksheet
Dim pct As Workbook
Dim pctST As Worksheet
Dim i As Long
Dim holdAry() As Variant
Dim Count As Integer

Count = 0

Set wkb = ThisWorkbook
Set ws = wkb.Worksheets(1)
Set pct = Workbooks.Open("C:\Users\user\OneDrive - Enagic USA Inc\General\Payment Check Tool.xlsm")
Set pctST = pct.Worksheets("Batch Processing & Hold List")


With pctST

lastRow = .Range("C6").End(xlDown).Row
For i = 6 To lastRow
ReDim holdAry(1 To lastRow + 5) As Variant
If .Range("K" & i).Value = "Tokurei" Or .Range("K" & i).Value = "OK" Then
Else
Count = Count + 1
holdAry(Count) = .Range("C" & i).Value
End If
Next i
End With


With ws.Range("N2:N700")
For i = LBound(holdAry) To UBound(holdAry)
.Range("N" & i).Value = holdAry(i)
Next i
End With

End Sub


For some reason this code returns blanks... it appears to be blank when I debug.print to immediate window and I'm not sure what I'm doing wrong.

I did however get it to work once by fluke, but the code took forever to work.. if you could help factor in a faster version of this for me to work off of, I would appreciate it!!
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Hi
Try
VBA Code:
'Option Explicit

Sub UpdateFromPCT()

    Dim wkb As Workbook
    Dim ws As Worksheet
    Dim pct As Workbook
    Dim pctST As Worksheet
    Dim i As Long
    Dim holdAry() As Variant
    Dim Count As Integer
    Count = 0

    Set wkb = ThisWorkbook
    Set ws = wkb.Worksheets(1)
    Set pct = Workbooks.Open("C:\Users\user\OneDrive - Enagic USA Inc\General\Payment Check Tool.xlsm")
    Set pctST = pct.Worksheets("Batch Processing & Hold List")

    With pctST
        lastrow = Cells(Rows.Count, 3).End(xlUp).Row
        ReDim holdAry(1 To lastrow + 5)

        For i = 6 To lastrow
            If .Range("K" & i).Value = "Tokurei" Or .Range("K" & i).Value = "OK" Then
            Else
                Count = Count + 1
                holdAry(Count) = .Range("C" & i).Value
            End If
        Next i
    End With


    With ws.Range("N2:N700")
        For i = LBound(holdAry) To UBound(holdAry)
            .Range("N" & i).Value = holdAry(i)
        Next i
    End With

End Sub
 
Upvote 0
Solution
Hi
Hi
Try
VBA Code:
'Option Explicit

Sub UpdateFromPCT()

    Dim wkb As Workbook
    Dim ws As Worksheet
    Dim pct As Workbook
    Dim pctST As Worksheet
    Dim i As Long
    Dim holdAry() As Variant
    Dim Count As Integer
    Count = 0

    Set wkb = ThisWorkbook
    Set ws = wkb.Worksheets(1)
    Set pct = Workbooks.Open("C:\Users\user\OneDrive - Enagic USA Inc\General\Payment Check Tool.xlsm")
    Set pctST = pct.Worksheets("Batch Processing & Hold List")

    With pctST
        lastrow = Cells(Rows.Count, 3).End(xlUp).Row
        ReDim holdAry(1 To lastrow + 5)

        For i = 6 To lastrow
            If .Range("K" & i).Value = "Tokurei" Or .Range("K" & i).Value = "OK" Then
            Else
                Count = Count + 1
                holdAry(Count) = .Range("C" & i).Value
            End If
        Next i
    End With


    With ws.Range("N2:N700")
        For i = LBound(holdAry) To UBound(holdAry)
            .Range("N" & i).Value = holdAry(i)
        Next i
    End With

End Sub
Hi Mohadin,

I your code and it worked perfectly! :)

I'm not sure entirely sure what I was doing differently, so if you could explain, I'd be grateful, but I'm happy it works none the less.

Thanks for your help!
 
Upvote 0
Well,
lastrow should be like
VBA Code:
 lastrow = Cells(Rows.Count, 3).End(xlUp).Row

And
VBA Code:
 ReDim holdAry(1 To lastrow + 5)
Should be out the for loop, you do Redim only one time right?
 
Upvote 0
Well,
lastrow should be like
VBA Code:
 lastrow = Cells(Rows.Count, 3).End(xlUp).Row

And
VBA Code:
 ReDim holdAry(1 To lastrow + 5)
Should be out the for loop, you do Redim only one time right?
ah! that makes sense!!! thank you so much for explaining where I went wrong! :)
 
Upvote 0
You are very welcome
And thank you for the feedback
Be happy and safe
 
Upvote 0
In both codes in #1 and #2:
VBA Code:
For i = LBound(holdAry) To UBound(holdAry)
            .Range("N" & i).Value = holdAry(i)
        Next i
it loop through each row in sheet then unload array then paste

Why not, in bulk:

VBA Code:
.Range("N1").Resize(Ubound(holdAry),1).value = holdAry

???
 
Upvote 0
In both codes in #1 and #2:
VBA Code:
For i = LBound(holdAry) To UBound(holdAry)
            .Range("N" & i).Value = holdAry(i)
        Next i
it loop through each row in sheet then unload array then paste

Why not, in bulk:

VBA Code:
.Range("N1").Resize(Ubound(holdAry),1).value = holdAry

???
I tried this! seems like it should work, but the output is just the value holdAry(1)

this is how I tried it


VBA Code:
...
With ws
.Range("N2").Resize(UBound(holdAry), 1).Value = holdAry
.Range("N1").Value = Format(Date, "yyyy/mm/dd")
...
 
Upvote 0

Forum statistics

Threads
1,214,551
Messages
6,120,159
Members
448,948
Latest member
spamiki

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