运行时错误'1004'无法设置范围的文本属性

时间:2015-09-25 15:28:56

标签: excel excel-vba runtime-error vba

我试图将“详细信息”表中的数据以线性方式存储到不同列到不同字符串的字符串中,然后在名为“output”的其他工作表的单元格中将字符串分配相同的值51次。 / p>

Option Explicit

Sub Arrange()
Dim FinalRow, FinalRow1 As Long
Dim ws, wr As Worksheet
Dim strCN, strAdd, strCity, strState, strZip, strPhone, strWeb As String
Application.ScreenUpdating = False
Dim i, j As Long


Set ws = Sheets("details")
FinalRow = ws.Range("A900000").End(xlUp).Row

For j = 2 To FinalRow

    strCN = Cells(j, "A")
    strAdd = Cells(j, "H")
    strCity = Cells(j, "I")
    strState = Cells(j, "J")
    strZip = Cells(j, "K")
    strPhone = Cells(j, "R")
    strWeb = Cells(j, "U")

    Set wr = Sheets("output")
    FinalRow1 = wr.Range("A900000").End(xlUp).Row
    For i = FinalRow1 To FinalRow1 + 51
        With Sheets("output")
            Cells(i, "A").Text = strCN       'Error Line
            Cells(i, "B").Text = strAdd
            Cells(i, "C").Text = strCity
            Cells(i, "D").Text = strState
            Cells(i, "E").Text = strZip
            Cells(i, "F").Text = strPhone
            Cells(i, "G").Text = strWeb
        End With
    Next i
Next j

End Sub

1 个答案:

答案 0 :(得分:1)

根据我们上面的谈话。我做了我建议的修改。

最后一个问题是没有调用详细信息表,如果其他工作表在查看空单元格时处于活动状态。

Dim FinalRow, FinalRow1 As Long
Dim ws, wr As Worksheet
Dim strCN, strAdd, strCity, strState, strZip, strPhone, strWeb As String
Application.ScreenUpdating = False
Dim i, j As Long


Set ws = Sheets("details")
FinalRow = ws.Range("A900000").End(xlUp).Row

For j = 2 To FinalRow
    With ws
        strCN = .Cells(j, "A")
        strAdd = .Cells(j, "H")
        strCity = .Cells(j, "I")
        strState = .Cells(j, "J")
        strZip = .Cells(j, "K")
        strPhone = .Cells(j, "R")
        strWeb = .Cells(j, "U")
    End With
    Set wr = Sheets("output")
    FinalRow1 = wr.Range("A900000").End(xlUp).Row
    For i = FinalRow1 To FinalRow1 + 51
        With Sheets("output")
            .Cells(i, "A").Value = strCN       'Error Line
            .Cells(i, "B").Value = strAdd
            .Cells(i, "C").Value = strCity
            .Cells(i, "D").Value = strState
            .Cells(i, "E").Value = strZip
            .Cells(i, "F").Value = strPhone
            .Cells(i, "G").Value = strWeb
        End With
    Next i
Next j
Application.ScreenUpdating = True