Home » excel » excel – Store text in variant after using .resize()

excel – Store text in variant after using .resize()

Posted by: admin May 14, 2020 Leave a comment

Questions:

I am new to variant function. I create myself a hypothetical exercise and then create a code for it. The exercise is to ask to search for the word “Price” in each worksheets, and then use resize to store the word “Price” and the value right to the “Price” word, it means to store the texts of 2 cells in the same row. Finally, return to whole stored texts in Sheet1 in cell C1. I am stuck in how to store the text in variant, the below code return the value TRUE, any idea on how to do it? Please critique severely on anything wrong to the below code.

1 more question, is that i am wrong in using variant instead of string in this case for array? Many thanks.

 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
How to&Answers:

Please critique severely on anything wrong to the below code.

Ok, you asked for it :p

I am new to variant function.

Variant is not a function. It is a datatype. You might want to read THIS

Use of .Find

Your code assumes that it will find Price on every sheet. What if there is no Price in a particular sheet? Your code will fail. You need to trap the .Find. For example

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

You can read more on .Find HERE

Storing the values from two cells from multiple sheets in the array

Here is a very basic example to help you understand on how to store those values in the array

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

So when you are using .Find then the below

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

becomes

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

If you wish, you can also omit storing the value of the first cell in the array as it will always be Price. In such a case, the above code simply becomes

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

Writing to a cell from the above array

Taking the above example, We will write the result array to say C1 in 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)

1 more question, is that i am wrong in using variant instead of string in this case for array?

No there is nothing wrong with it. However, I would use a String array instead as you are storing strings from different sheets into the array. Here is an example when you would use a Variant Array.

Dim MyArray As Variant

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

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

For further reading (Highly Recommended)

VBA Arrays And Worksheet Ranges