Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 在VBA中重新命名数组_Arrays_Vba_Excel - Fatal编程技术网

Arrays 在VBA中重新命名数组

Arrays 在VBA中重新命名数组,arrays,vba,excel,Arrays,Vba,Excel,我有3个数据数组,通过读取excel工作表来填充,一些数据点缺失,因此刚刚以“NA”的形式输入excel,因此我想查看我的数组,找到这些NA的每个实例,并将其从数组中删除,因为信息是无用的。我需要同时更新所有三个阵列 Sub group_data() Dim country(), roe(), iCap() As String Dim i As Integer For i = 1 To 3357 country(i) = Workbooks("restco

我有3个数据数组,通过读取excel工作表来填充,一些数据点缺失,因此刚刚以“NA”的形式输入excel,因此我想查看我的数组,找到这些NA的每个实例,并将其从数组中删除,因为信息是无用的。我需要同时更新所有三个阵列

Sub group_data()
    Dim country(), roe(), iCap() As String
    Dim i As Integer
    For i = 1 To 3357
        country(i) = Workbooks("restcompfirm.xls").Worksheets("Sheet1").Range("C1").Offset(i, 0)
        roe(i) = Workbooks("restcompfirm.xls").Worksheets("Sheet1").Range("AP1").Offset(i, 0)
        iCap(i) = Workbooks("restcompfirm.xls").Worksheets("Sheet1").Range("BM1").Offset(i, 0)
    Next i 
End Sub

因此,如果我在roe或iCap中找到一个“NA”作为值之一,我希望在所有这些数组中去掉这段数据

注意:我已在记事本中编写了此代码。
如果你对此有任何问题,请告诉我

Sub group_data()
dim totalRows as integer
dim rowNum as integer
dim rowsWithoutNA as integer

dim c1Range as Range
dim ap1Range as Range
dim bm1Range as Range

set c1Range = Workbooks("restcompfirm.xls").Worksheets("Sheet1").Range("C1")
set ap1Range = Workbooks("restcompfirm.xls").Worksheets("Sheet1").Range("AP1")
set bm1Range = Workbooks("restcompfirm.xls").Worksheets("Sheet1").Range("BM1")


Dim country(), roe(), iCap() As String
Dim i As Integer

totalRows = 3357

redim country(totalRows)
redim roe(totalRows)
redim iCap(totalRows)

For i = 0 To (totalRows - 1)
   rowNum = rowNum + 1

   roe(rowsWithoutNA) = ap1Range.Offset(rowNum, 0).Text
   iCap(rowsWithoutNA) = bm1Range.Offset(rowNum, 0).Text

   if (WorksheetFunction.IsNA(roe(rowNum)) _
      OR WorksheetFunction.IsNA(iCap(rowNum))) = False Then
   ' use the following condition, if NA is written in text 
   'if (trim(roe(rowNum)) = "NA" OR trim(iCap(rowNum)) = "NA") Then
      country(rowsWithoutNA) = c1Range.Offset(rowNum, 0)
      rowsWithoutNA = rowsWithoutNA + 1
   end if
Next i 

redim preserve country(rowsWithoutNA )
redim preserve roe(rowsWithoutNA )
redim preserve iCap(rowsWithoutNA )

end sub

在构建阵列时,我甚至不会将“NA”放在第一位。这是您的代码,但更改为不包含“NA”


为了清楚起见,我假设您有一个C1范围内的国家列表,然后是AP1和BM1范围内的相关roe和iCap值。一些roe和iCap实体缺失且已输入为“NA”的问题。您希望创建仅包含roe和iCap值同时存在的国家/地区的数组

首先,使用
Redim Preserve
是一种“昂贵”的操作,会影响代码的效率

其次,顺便说一句,在代码(如下)中使用语法只会将最后一个变量设置为String。前两个变量将创建为变量类型变量:

Dim country(), roe(), iCap() As String 
此代码应编写为:

Dim country() as String, roe() as String, iCap() As String 
关于你的问题,我的做法如下:

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
子填充数组()
'定义数组
Dim countryArray()作为字符串,roeArray()作为变量,iCapArray()作为变量
'获取国家总数
所有行的长度相同
totalRows=工作簿(“restcomfirm.xls”)。工作表(“Sheet1”)。范围(“C1”)。结束(xlDown)。行
'根据totalRows定义数组大小
ReDim countryArray(totalRows-1)
ReDim ROE阵列(总计行数-1)
ReDim iCapArray(总计行数-1)
'定义缺少的数据文本
Dim丢失数据作为字符串
缺失数据=“NA”
暗淡的光线
iArray=0
带有工作手册(“restcompfirm.xls”)。工作表(“表1”)
'循环每行,检查roe或iCap是否设置为'NA'
对于cl=1到totalRows
如果微调(.Range(“AP”&cl))丢失数据,则
如果微调(.Range(“BM”&cl))丢失数据,则
countryArray(iArray)=.范围(“C”和cl)
roeArray(iArray)=.范围(“AP”和cl)
iCapArray(iArray)=.范围(“BM”和cl)
iArray=iArray+1
如果结束
如果结束
下一个cl
以
端接头

希望这有帮助。

请考虑将整个范围分配给数组,而不是迭代。细节将取决于您的工作表,但速度会快得多。
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