VBA菜鸟在这里。奋战了几个小时才弄清楚我的错误。经过大量论坛搜索后,我仍然不明白我的错误。
因此,我要执行的操作是复制并粘贴一定范围的单元格,但是行号将根据先前的代码进行更改(查找指定范围内的最小值,该值也会随每组新数据而改变)。
我有一组数据,其中包含多行和多列。我正在1300左右的行中寻找8或9个“好的”布景。我已经构建了“查找”这些行的代码。
现在,我需要复制所需范围(已知列“ FoundRow”)中的行,并将其粘贴到Sheet2所需范围(行1,行2等)中。
所以我得到:
“编译错误:预期:列表分隔符或””
这是我的测试块,因此我不必遍历其他代码部分。该问题已在代码中注释到代码结尾。
Sub test()
'block of code to state array value 0-1, 1-2, 2-3, etc to signify the row range
'and then search for the minimum sum of COV within that.
Dim i As Integer
Dim iVal As Integer
Dim covBlk(1) As Single
Dim MinVal As Double
Dim lIndex As Long
Dim FoundRow As Variant
Dim sFoundRow As String
Dim inRange As Range
covBlk(0) = 62
covBlk(1) = 109
iVal = 8
i = 0
With Sheet1
' .Range(.Cells(1, 1), .Cells(10, 1)).Value2 = 5
' End With
'Set rng = Range("C3:C" & Rows.Count)
Set rng = .Range(.Cells(covBlk(i), 106), .Cells(covBlk(i + 1), 106))
MinVal = Application.WorksheetFunction.Min(rng)
lIndex = Application.WorksheetFunction.Match(MinVal, rng, 0)
End With
GetAddr = rng.Cells(lIndex).Address
'Find a way to return the Row # only so it can be used to copy/paste
FoundRow = Range(GetAddr).Row
sFoundRow = LTrim(Str(FoundRow))
'THIS IS THE PROBLEM
Worksheets("Sheet1").Range("CB" & sFoundRow : "CY" & sFoundRow).Copy _
'Destination:=Worksheets("Sheet2").Range("E5")
End Sub
答案 0 :(得分:1)
您没有很好地串联该字符串。如您所见,:
不在字符串中。因此,要使用您的语法,正确的字符串应为:
Worksheets("Sheet1").Range("CB" & sFoundRow & ":CY" & sFoundRow).Copy