我试图从一个工作表的特定范围中抓取字符串,然后将该数组输出到另一个工作表范围内。不幸的是,生成的代码没有给我任何输出。
提前感谢您的帮助。
Dim strFunds As String
Dim varItemName As Variant
Dim strItemName() As String
Dim iRow As Long
Dim rngMyRange As Range
Sub DataGrab()
ActiveWorkbook.Sheets("Returns").Activate
Set rngMyRange = Range("A5:A100")
varItemName = rngMyRange
ReDim strItemName(LBound(varItemName, 1) To UBound(varItemName, 1))
ActiveWorkbook.Sheets("Data").Activate
Range("A3:A" & UBound(strItemName) + 1) = WorksheetFunction.Transpose(strItemName)
End Sub
答案 0 :(得分:2)
Sub Main()
Dim rngArr
rngArr = Sheets("Returns").Range("A5:B100").Value
Sheets("Data").Range("A3").Resize(UBound(rngArr) + 1, 2) = rngArr
End Sub
答案 1 :(得分:1)
试试这个。
首先,将varItemName
更改为数组:
Dim varItemName() As Variant
然后:
Sub DataGrab()
ActiveWorkbook.Sheets("Returns").Activate
Set rngMyRange = Range("A5:A100")
varItemName = rngMyRange.Value
'ReDim strItemName(LBound(varItemName, 1) To UBound(varItemName, 1))
ActiveWorkbook.Sheets("Data").Activate
Range("A3").Resize(1, UBound(varItemName) + 1) = WorksheetFunction.Transpose(varItemName)
End Sub
假设您要将列式数据转换为单行。如果没有,请在最后一行执行此操作:
Range("A3").Resize(UBound(varItemName) + 1, 1) = varItemName
答案 2 :(得分:0)
您没有为strItemName
分配任何值。这是一个只保留varItemName
中所有内容的版本。
ActiveWorkbook.Sheets("Returns").Activate
Set rngMyRange = Range("A5:A100")
varItemName = rngMyRange.Value
ActiveWorkbook.Sheets("Data").Activate
Range(Cells(3, 1), Cells(3, UBound(varItemName))) = WorksheetFunction.Transpose(varItemName)
更新:如果您不需要保存varItemName
,则可以使用此较短版本:
Set rngMyRange = Range("A5:A100")
ActiveWorkbook.Sheets("Data").Activate
Range(Cells(3, 1), Cells(3, 100)) = WorksheetFunction.Transpose(rngMyRange)