Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.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 使用数组将文本从一个工作表传递到另一个工作表_Arrays_Vba_Excel_Transpose - Fatal编程技术网

Arrays 使用数组将文本从一个工作表传递到另一个工作表

Arrays 使用数组将文本从一个工作表传递到另一个工作表,arrays,vba,excel,transpose,Arrays,Vba,Excel,Transpose,我试图根据标准(*)将数据从表3传递到表4。对于数字结果,但对于文本,程序失败。 如何克服这种情况时,而不是一个数字我有文字 Public Sub TestArray3() 'Array to copy data from Sheet3 to Sheet4 Based on criterion "in this case*" Dim tempVar As Integer, anotherIteration As Boolean, i As Integer Dim J As

我试图根据标准(*)将数据从表3传递到表4。对于数字结果,但对于文本,程序失败。 如何克服这种情况时,而不是一个数字我有文字

Public Sub TestArray3()
    'Array to copy data from Sheet3 to Sheet4 Based on criterion "in this case*"
    Dim tempVar As Integer, anotherIteration As Boolean, i As Integer
    Dim J As Integer, ArraySize As Integer, myArray() As Integer
    Dim newArray() As Integer, FinalRow As Integer, linha As Integer
    Dim counter As Integer, cel1 As Range

    Sheets("Folha3").Select
    FinalRow = Cells(Rows.Count, 1).End(xlUp).Row ' Find the last row of data
    ArraySize = FinalRow 'Get Array Size
    ReDim myArray(ArraySize - 1)
    For linha = 1 To FinalRow
        Set cel1 = Cells(linha, 1)
        If cel1 = "*" Then
            myArray(linha - 1) = Val(Cells(linha, "B").Value) 'Populate de Array
        End If
    Next linha

    ReDim newArray(LBound(myArray) To UBound(myArray)) 'Avoid zeros in Array
    For i = LBound(myArray) To UBound(myArray)
        If myArray(i) <> "0" Then
            J = J + 1
            newArray(J) = myArray(i)
        End If
    Next i

    ReDim Preserve newArray(LBound(myArray) To J)
    ArraySize = J
    Sheets("Folha4").Select 'Write data to Sheet 4 column A
    Range("A1").Resize(J - LBound(newArray) + 1)=Application.Transpose(newArray)
End Sub
Public Sub-TestArray3()
'根据标准将数据从Sheet3复制到Sheet4的数组“在这种情况下*”
Dim tempVar为整数,另一个迭代为布尔值,i为整数
Dim J为整数,ArraySize为整数,myArray()为整数
Dim newArray()为整数,FinalRow为整数,linha为整数
Dim计数器为整数,cel1为量程
表格(“折叠3”)。选择
FinalRow=单元格(Rows.Count,1)。End(xlUp)。Row'查找最后一行数据
ArraySize=FinalRow'获取数组大小
ReDim myArray(阵列大小-1)
对于linha=1到FinalRow
设置cel1=单元格(linha,1)
如果cel1=“*”则
myArray(linha-1)=Val(单元格(linha,“B”).Value)”填充数组
如果结束
下一个林哈
ReDim newArray(LBound(myArray)到UBound(myArray))'避免数组中的零
对于i=LBound(myArray)到UBound(myArray)
如果myArray(i)“0”,则
J=J+1
新数组(J)=myArray(i)
如果结束
接下来我
重拨保留新数组(LBound(myArray)到J)
ArraySize=J
表格(“文件夹4”)。选择“将数据写入表格4 A列
Range(“A1”).Resize(J-LBound(newArray)+1)=Application.Transpose(newArray)
端接头

我不清楚您实际要从哪里粘贴到哪里,但这里有几种在工作表之间移动数据的方法,包括使用和不使用转置

希望此示例能够澄清以下步骤:

Sub copyRangeToOtherSheet()

    Dim lastRow As Long, lastCol As Long, rgSrc As Range, rgDest As Range, arr() As Variant

    With ThisWorkbook.Sheets("Sheet1")                               'set source worksheet

        lastRow = .Cells(Rows.Count, "A").End(xlUp).Row              'find last row of Col A
        lastCol = .Cells(1, Columns.Count).End(xlToLeft).Column      'find last col of Row 1
        Set rgSrc = Range(.Range("A1"), .Cells(lastRow, lastCol))    'create range (from A1)

    End With

    arr = rgSrc                                                      'dump range into array

    With ThisWorkbook.Sheets("Sheet2")                               'set destination sheet

      'OPTION #1: Populate destination in "original" orientation
        Set rgDest = .Range("A1")                           'set destination top-left corner
        Set rgDest = rgDest.Resize(UBound(arr, 1), UBound(arr, 2))  'fit to array rows/col's
        rgDest = arr                                          'dump array to worksheet range

      'OPTION #2: Populate destination in "transposed" orientation
        Set rgDest = .Range("D1")                           'set destination top-left corner
        Set rgDest = rgDest.Resize(UBound(arr, 2), UBound(arr, 1))  'fit to array col's/rows
        rgDest = WorksheetFunction.Transpose(arr)  'dump transposed array to worksheet range

    End With

End Sub
请注意,如果不事先设置数组的大小,这是最简单的方法-只要数组尚未标注尺寸,Excel就会自动调整数组的大小(这就是为什么它仅声明为
arr()作为Variant

在目标端,我们可以选择一个单元格作为范围的左上角,然后根据数组的上界()选择范围

如果要转到单元格,必须交换目标区域中的行数/列数


更多信息:
  • 我发现一个非常有用的资源是Chip Pearson的
字符串与整数 这里发生的事情有点不清楚,但我注意到您已将所有数组声明为整数,因此无法将字符串传递给它们。尝试找出要将字符串传递到的数组,并将其声明为变量,或者实现一些“条件”代码,如:

If Not IsNumeric(Cells("A1").Value) then
  Variable = 0
End If  
阅读ashleedawg的指南

您不必选择工作表来对其执行操作(请参阅选择)。你可以写

FinalRow = Sheets("Folha3").Cells(Rows.Count, 1).End(xlUp).Row

并保留一条线路,但更重要的是,不要在工作区里到处乱跳。更好的方法是将一起使用:

With Sheets("Folha3")
    FinalRow = .Cells(Rows.Count, 1).End(xlUp).Row ' Find the last row of data
    ArraySize = FinalRow 'Get Array Size
    ReDim myArray(ArraySize - 1)
    For linha = 1 To FinalRow
        Set cel1 = .Cells(linha, 1)
        If cel1 = "*" Then
            myArray(linha - 1) = Val(.Cells(linha, "B").Value) 'Populate de Array
        End If
    Next linha
End With
请注意每个单元格(.cells)前面的“”,它指的是图纸对象

尝试对对象使用变量。当你写作时

Sheets("folha3"). 
什么都不会发生你必须记住它能做什么。但是,如果您将其分配给变量,intelliSense将被激活,您可以看到对象的属性和方法,例如

Dim oWb as Workbook
Dim oWs as Worksheet
Set oWb = Activeworkbook
Set oWs = oWb.Sheets("Folha3")
现在当你写:

oWs.
IntelliSense向您显示工作表对象的属性和方法,例如激活、单元格、复制、删除、粘贴等


通过几行代码,您将学到更多。

如果您需要文本,为什么要使用非限定Val(…)转换?您的意思是使用Val使文本为零吗?您要复制哪个范围,以及复制到哪里?感谢您的帮助,但是,我在第1页的a列有*我有*和B列有字符串。每次找到*时,我都要将字符串从表1复制到表2。当我执行宏时,系统会看到带有0的字符串。感谢您的帮助和建议。我使用了您的代码(使用with),在开头声明了一个数组,但我无法让宏读取字符串,值始终为0。如何求解?我的答案的第一句话说,如果要传递字符串,您将数组声明为整数,这是错误的。如果要将字符串传递给数组,应将其声明为string:Dim SomeArray()为string。更好的方法是将Dim SomeArray()作为变量转换为Dim SomeArray(),然后您可以传递所需的任何值。
oWs.