使用.resize()后在变体中存储文本

时间:2014-01-06 10:50:57

标签: excel vba excel-vba

我是变体功能的新手。我创建了一个假设的练习,然后为它创建一个代码。练习是要求在每个工作表中搜索单词“Price”,然后使用resize将单词“Price”和值右侧存储到“Price”单词,这意味着将2个单元格的文本存储在同一行。最后,返回单元格C1中Sheet1中的整个存储文本。我被困在如何将文本存储在variant中,下面的代码返回值TRUE,任何关于如何做的想法?请严格批评以下代码的任何错误。

另外1个问题,在这种情况下,对于数组使用variant而不是字符串是错误的吗?非常感谢。

 Sub macro1()

 Dim data1 As Integer
 Dim counter1 As Integer
 Dim rng As Range
 Dim variant1() As Variant
 Dim strData As String

 data1 = Worksheets.Count


 ReDim variant1(data1) As Variant

 counter1 = 1

 For counter1 = 1 To data1

 Sheets(counter1).Select
 Cells.Find(What:="Price", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
     :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
     False, SearchFormat:=False).Activate



    variant1(counter1 - 1) = Selection.Resize(1, 2).Select
    'variant1(counter1 - 1) = Selection.Resize(1, 2).Text

 Next



 strData = Join(variant1, vbNewLine)

 strData = Left(strData, Len(strData) - 1)

 Sheets(1).Select
 Range("C1") = strData


 MsgBox strData


 End Sub

1 个答案:

答案 0 :(得分:3)

  

请严格批评以下代码的任何错误。

好的,你要求它:p

  

我是变体功能的新手。

Variant不是一个功能。它是一种数据类型。您可能需要阅读THIS

  

使用.Find

您的代码假定它会在每张表上找到Price。如果特定工作表中没有Price怎么办?您的代码将失败。您需要捕获.Find。例如

Dim aCell As Range

With Sheets(counter1)
    Set aCell = .Cells.Find(What:="Price", LookIn:=xlFormulas, LookAt _
    :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
    False, SearchFormat:=False)
End With

If Not aCell Is Nothing Then
    '
    '~~> Rest of the code
    '
Else
    Debug.Print "`Price` not found in sheet " & Sheets(counter1).Name
End If

您可以在.Find HERE

上阅读更多内容
  

存储数组中多个工作表的两个单元格中的值

这是一个非常基本的示例,可帮助您了解如何在数组中存储这些值

Dim MyArray()
Dim n As Long

n = 1

ReDim MyArray(n)

MyArray(n) = Sheets("Sheet1").Range("A1").Value & _
             "-" & _
            Sheets("Sheet1").Range("B1").Value

n = n + 1

'<~~ We need to use Preserve else the previous data will be lost
ReDim Preserve MyArray(n) 

MyArray(n) = Sheets("Sheet2").Range("A1").Value & _
             "-" & _
             Sheets("Sheet2").Range("B1").Value

n = n + 1

ReDim Preserve MyArray(n)

MyArray(n) = Sheets("Sheet3").Range("A1").Value & _
             "-" & _
             Sheets("Sheet3").Range("B1").Value

因此,当您使用.Find时,请使用下面的

MyArray(n) = Sheets("Sheet3").Range("A1").Value & _
             "-" & _
             Sheets("Sheet3").Range("B1").Value

变为

MyArray(n) = aCell.Value & _
             "-" & _
             aCell.Offset(,1).Value

如果您愿意,您也可以省略存储数组中第一个单元格的值,因为它始终是Price。在这种情况下,上面的代码就变成了

MyArray(n) = aCell.Offset(,1).Value
  

从上面的数组写入单元格

采用上面的例子,我们将结果数组写成C1

中的Sheet3
Dim MyArray()
Dim n As Long

ReDim MyArray(n)

MyArray(n) = Sheets("Sheet1").Range("A1").Value & _
             "-" & _
            Sheets("Sheet1").Range("B1").Value

n = n + 1

ReDim Preserve MyArray(n)

MyArray(n) = Sheets("Sheet2").Range("A1").Value & _
             "-" & _
             Sheets("Sheet2").Range("B1").Value

ThisWorkbook.Sheets("Sheet3").Range("C1").Resize(UBound(MyArray) + 1, 1).Value = _
Application.Transpose(MyArray)
  

另外还有一个问题,就是我在使用变体而不是字符串这个数组时错了吗?

没有任何问题。但是,我会使用String数组,因为您将来自不同工作表的字符串存储到数组中。这是一个使用Variant数组的例子。

Dim MyArray As Variant

MyArray = Sheets("Sheet1").Range("A1:B1").Value

Debug.Print MyArray(1, 1)
Debug.Print MyArray(1, 2)
  

进一步阅读(强烈推荐)

VBA Arrays And Worksheet Ranges