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

我是新变种function。 我创build一个假设的练习,然后为它创build一个代码。 练习是要求在每个工作表中search单词“价格”,然后使用resize来存储单词“价格”和价值右边的“价格”单词,这意味着将2个单元格的文本存储在同一排。 最后,在单元格C1中返回Sheet1中的整个存储的文本。 我卡在如何存储variables的文本,下面的代码返回值TRUE,任何想法如何做到这一点? 请对下面的代码进行严重的批评。

1更多的问题,是我在这种情况下使用variant而不是string的数组? 非常感谢。

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 

请对下面的代码进行严重的批评。

好的,你问了:p

我是新变种function。

Variant不是一个函数。 这是一个数据types。 你可能想读这个

使用.Find

你的代码假定它会在每张纸上findPrice 。 如果特定表格中没有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这里

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

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

 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 

从上面的数组写入一个单元格

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

 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) 

1更多的问题,是我在这种情况下使用variant而不是string的数组?

不,没有错。 但是,我会使用一个string数组,而不是将string从不同的工作表存储到数组中。 这里是一个例子,你会使用一个Variant数组。

 Dim MyArray As Variant MyArray = Sheets("Sheet1").Range("A1:B1").Value Debug.Print MyArray(1, 1) Debug.Print MyArray(1, 2) 

为进一步阅读(强烈推荐)

VBA数组和工作表范围