我需要一个宏来查找一行中的字符串(该字符串来自一个数组),然后找到该字符串所在的单元格后,将其旁边的值存储到另一个数组中。有没有办法做到这一点,还是我在想一些不可能的事情?这是我到目前为止所得到的
Sub ListWorkSheetNames()
Dim Cellnames(1000) As String
Dim Shrinkage(1000) As Double
For i = 1 To Application.Sheets.Count
Cellnames(i) = Application.Sheets(i).Name
strName = Cellnames(i)
Shrinkage(i) = Workbooks(strProjectedRevenue).Worksheet("Month").Rows(2).Find(What:=strName, LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False)
Workbooks(strB.xlsm).Worksheets("Sheet1").Range("E" & i) = Shrinkage
Workbooks(strB.xlsm).Worksheets("Sheet1").Range("D" & i) = strName
Next i
End Sub
答案 0 :(得分:0)
你不需要我能看到的两个阵列。
这是你正在尝试的( UNTESTED )?我评论了代码。如果您有疑问,请告诉我。
Sub Sample()
Dim ws As Worksheet, wsI As Worksheet
Dim MyAr As Variant
Dim SearchString As String, tmpString As String, sDelim As String
Dim aCell As Range
'~~> Worksheet where you need to search
Set wsI = Workbooks(strProjectedRevenue).Worksheet("Month")
'~~> I am assuming that none of the sheets have this name. Else Change it
'~~> to something unique
sDelim = "lbochitt"
'~~> Loop through the worksheets
For Each ws In ThisWorkbook.Sheets
'~~> Search String
SearchString = ws.Name
'~~> Find the string
Set aCell = wsI.Rows(2).Find(What:=SearchString, LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
'~~> Check if the result returned anything
If Not aCell Is Nothing Then
'~~> Store the offset value in a string separated with a delimiter
If tmpString = "" Then
tmpString = aCell.Offset(, 1).Value
Else
tmpString = tmpString & sDelim & aCell.Offset(, 1).Value
End If
Set aCell = Nothing
Next
Next
If tmpString <> "" Then
'~~> here is your final array
MyAr = Split(tmpString, sDelim)
End If
End Sub