简体   繁体   English

引用其他单元格并在VBA中提取子字符串

[英]Referring to a different cell and extracting Substrings in VBA

I have 2 sheets, "Report" and "Data". 我有2张纸,“报告”和“数据”。 In the "Data" sheet are values in column A as follows T-Shirt.Adidas.25.110 I need to take the raw data and input them into the "Report" Sheet as separate entities. 在“数据”表中,A列中的值如下所示:T-Shirt.Adidas.25.110我需要获取原始数据,并将其作为单独的实体输入到“报告”表中。

ex. 恩。 Cell A1 will read "T-Shirt" Cell B1 will read "Adidas" 单元格A1将显示为“ T恤”单元格B1将显示为“阿迪达斯”

Here is what I have so far. 这是我到目前为止所拥有的。 Its a with statement but that will only work for one line. 它的with语句,但仅适用于一行。 Im not sure how to loop it. 我不确定如何循环播放。

Dim Cell As Object
Dim Data As Range
Dim Report As Range
Set Report = Worksheets("Report").Range("A2", Range("A2").End(xlDown))
Set Data = Worksheets("Data").Range("A2", Range("A2").End(xlDown))

With Report
    .Resize(1, 4) = Split(Worksheets("Data").Range("A2"), ".")
End With

The macro needs to work for any number of objects in the data sheet. 该宏需要为数据表中的任意数量的对象工作。 Thanks in advance! 提前致谢!

Here is one way to loop: 这是循环的一种方法:

Sub test()

Dim Cell As Object
Dim Data As Range
Dim Report As Range
Dim i As Long, TempArray As Variant

Set Data = Worksheets("Data").Range("A2", Worksheets("Data").Range("A2").End(xlDown))
Set Report = Worksheets("Report").Range("A2")

For i = 1 To Data.Count
    TempArray = Split(Data.Range("A" & i), ".")
    Report.Offset(i - 1).Resize(1, UBound(TempArray) + 1) = TempArray
Next i
Set TempArray = Nothing

End Sub

This way you use the Data as the source and count of iterations, and you can generalize the columns based on the number of periods in a given piece of data. 这样,您可以将数据用作迭代的来源和计数,并且可以基于给定数据段中的周期数来概括列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM