0

我在星号线上得到了错误。我浏览了很多我发现很多我复制了相同的答案,但仍然收到此错误。我不确定我做错了什么。

请帮忙。提前致谢。

Sub splitIntoCsv()
    Dim wbIn
    Dim wbIn1 As Workbook
    Dim header, ranges, range_lower, range_upper, rangeCopy As Variant
    Dim rangeVariable As String
    Dim commacheck, rows, columns As Integer

    Set wbIn = CreateObject("Excel.Application")
    wbIn.Workbooks.Add
    wbIn.Worksheets(1).Name = "TestData"
    Set wbIn1 = Workbooks.Open(Sheet1.Range("B1").Value, True, True)

    rows = wbIn1.Sheets(1).UsedRange.rows.Count
    columns = wbIn1.Sheets(1).UsedRange.columns.Count
    header = Split(ThisWorkbook.Sheets(1).Range("B2").Value, ",")
    rangeVariable = ThisWorkbook.Sheets(1).Range("B3").Value
    commacheck = InStr(rangeVariable, ",")

    If commacheck = 0 Then
        rangeVariable = rangeVariable & ","
    End If

    ranges = Split(rangeVariable, ",")
    For i = LBound(ranges) To UBound(ranges) - 1
        For j = LBound(header) To UBound(header)
            wbIn.Worksheets(1).Cells(1, j + 1).Value = header(j)
        Next j

        range_lower = Split(ranges(i), "-")(0)
        range_upper = Split(ranges(i), "-")(1)

        With wbIn1.Sheets(1)
           rangeCopy = .Range(.Cells(1 + range_lower, 1), .Cells(1 + range_upper, columns)).Value
        End With

        With wbIn.Worksheets(1)  
          *********        
            .Range(.Cells(1 + range_lower, 1), .Cells(1 + range_upper, columns)).Value = rangeCopy
          *********
        End With

    Next i

    wbIn1.Close SaveChanges:=True

    wbIn.DisplayAlerts = False
    wbIn.Worksheets(1).SaveAs Filename:="D:\RaghuDev\raghu.csv", FileFormat:=xlCSV, CreateBackup:=False
    wbIn.Quit        
End Sub
4

1 回答 1

0

我没有收到同样的错误,但您可以在新的 Excel 报告中尝试以下测试吗?我试图重新创建您问题的关键区域。

只需确保放入一些示例数据并选择 1 x 3 的范围。

Sub test()

    Dim wbIn
    Set wbIn = CreateObject("Excel.Application")
    wbIn.Workbooks.Add
    wbIn.Worksheets(1).Name = "TestData"

    Dim r As Variant, columns As Integer

    r = Selection.Value
    columns = 3

    With wbIn.Worksheets(1)
        .Range(.Cells(1, 1), .Cells(1, columns)).Value = r
    End With

End Sub

还尝试定义变体:

ReDim rangeCopy(1 to (range_upper - range_lower + 1), 1 to columns) as Variant

With wbIn1.Sheets(1)
   rangeCopy = .Range(.Cells(1 + range_lower, 1), .Cells(1 + range_upper, columns)).Value
End With

With wbIn.Worksheets(1)  
    .Range(.Cells(1 + range_lower, 1), .Cells(1 + range_upper, columns)).Value = rangeCopy
End With
于 2013-04-18T13:33:52.370 回答