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

时间:2018-10-01 22:13:03

标签: arrays excel vba excel-vba 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 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

2 个答案:

答案 0 :(得分:1)

我不清楚您实际在何处进行粘贴,但这是[几种]方法中的一种,可在工作表之间移动数据,包括有和无转移

希望此示例可以清除步骤:

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() As Variant的原因) 。

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

如果要访问Transpose单元格,则必须交换目标范围内的行数/列数

img


更多信息:

答案 1 :(得分:0)

字符串vs整数

目前尚不清楚发生了什么,但是我注意到您已将所有数组声明为 integer ,因此您无法将字符串传递给它们。尝试找出您要传递字符串的数组,并将其声明为 variant 或实施一些“条件”代码,例如:

If Not IsNumeric(Cells("A1").Value) then
  Variable = 0
End If  

阅读 ashleedawg 的指南。

您无需选择工作表即可对其进行处理(请参考选择)。你可以写

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

Sheets("Folha4").Range("A1").Resize(J - LBound(newArray) + 1) _
= Application.Transpose(newArray)

并保存一行,但更重要的是,不要在工作簿中跳来跳去。更好的是使用使用

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向您显示工作表对象的属性和方法,例如激活,单元格,复制,删除,粘贴等。

使用几行代码,您会学到更多。