To be clear, I assume that you have a list of countries in Range C1, and then the associated roe and iCap values ββin the ranges AP1 and BM1. The problem is that some of the roe deer and iCap are missing and they were entered as "NA". You would like to create arrays that contain only countries that have iBap and ..
First, using Redim Preserve is an βexpensiveβ operation and will affect code performance.
Secondly, as on the sidelines, using syntax like in your code (below) will only result in setting the final variable to String. The first two will be created as variables. Option:
Dim country(), roe(), iCap() As String
This code should be written as:
Dim country() as String, roe() as String, iCap() As String
From the point of view of your problem, my approach would be as follows:
Sub FillArrays() 'Define arrays Dim countryArray() As String, roeArray() As Variant, iCapArray() As Variant 'Get total number of countries Dim totalRows As Long totalRows = Workbooks("restcompfirm.xls").Worksheets("Sheet1").Range("C1").End(xlDown).Row 'Define array size based on totalRows ReDim countryArray(totalRows - 1) ReDim roeArray(totalRows - 1) ReDim iCapArray(totalRows - 1) 'Define missing data text Dim missingData As String missingData = "NA" Dim iArray As Long iArray = 0 With Workbooks("restcompfirm.xls").Worksheets("Sheet1") 'Loop through each row and check if either roe or iCap are set to 'NA' For cl = 1 To totalRows If Trim(.Range("AP" & cl)) <> missingData Then If Trim(.Range("BM" & cl)) <> missingData Then countryArray(iArray) = .Range("C" & cl) roeArray(iArray) = .Range("AP" & cl) iCapArray(iArray) = .Range("BM" & cl) iArray = iArray + 1 End If End If Next cl End With End Sub
Hope this helps.
Alex p
source share