2

我想看看为什么我会遇到这个问题。

Private Sub test()

Dim Row As Integer

For Row = 1 To 100

   'Loop through SummaryRange and ignore blanks but get document type'
    Dim documentTypes As String

    Dim myDocument As String, Column As Integer

    Column = 2

   'First get range from Summary'
    Sheets("Sheet1").Active

    If ActiveSheet.Cells(Row, Column).Value <> "" Then documentTypes = documentTypes + "," + ActiveSheet.Cells(Row, Column).Value

    Sheets("Sheet12").Active

    ActiveSheet.Range("B17").Value = documentTypes

Next Row

End Sub

我试图遍历不同工作表中的范围,然后获取值,然后将它们连接成一个字符串并输出该字符串。

编辑:

删除了 SummaryRange,摆脱了超出范围的问题,但带来了一个Object doesn't support this property or method

4

3 回答 3

1

尝试改变

SummaryRange.Range("Row:Column").Value

SummaryRange.Range(Row:Column).Value

更新:尝试以下

Dim range As Range
Dim row As Range
Dim cell As Range

Set range = Range("B1:B100")

For Each row In range.Rows
   For Each cell in row.Cells

      'processing code
      'documentTypes = documentTypes + "," + cell.Value

      'etc...

   Next cell
Next row
于 2012-04-03T23:04:26.663 回答
1

尝试改变:

 Sheets("Sheet1").Active

到:

 Sheets("Sheet1").Activate

这同样适用于:

 Sheets("Sheet12").Active
于 2012-04-04T00:33:33.537 回答
1

虽然 bouvierr 已经回答了您现有的问题,但我注意到您实际上可以避免循环并更有效地做到这一点

此行
strOut = Join(Application.Transpose(ws.Range("B1:B100")), ",")
生成一个由 B1:B100 中的所有值组成的字符串,以逗号分隔

由于某些值可能为空,因此生成的字符串可能看起来像这样 test,1,2,3,,,3,,afaff,,,,,,,,,,,

所以我使用正则表达式将多个清理,为一个'

Sub QuickGrab()
Dim ws As Worksheet
Dim objRegex
Dim strOut As String

Set ws = Sheets("Sheet1")
strOut = Join(Application.Transpose(ws.Range("B1:B100")), ",")

Set objRegex = CreateObject("vbscript.regexp")
With objRegex
    .Pattern = ",{2,}"
    .Global = True
    strOut = .Replace(strOut, ",")
End With

MsgBox strOut
End Sub
于 2012-04-04T01:09:42.577 回答