Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 将动态范围格式化为文本_Vba_Excel - Fatal编程技术网

Vba 将动态范围格式化为文本

Vba 将动态范围格式化为文本,vba,excel,Vba,Excel,在加载到数据库之前,我正在编写一个makro,将所有非数字单元格格式化为文本。我有一条线路,我不知道出了什么问题。我的VBA技能很差。我得到运行时错误'424' Sub formatAllCellsAsText() Dim wsTemp As Worksheet Dim LastRow As Long Dim LastColumn As Long Dim StartCell As Range For sht = 3 To Worksheets.Count

在加载到数据库之前,我正在编写一个makro,将所有非数字单元格格式化为文本。我有一条线路,我不知道出了什么问题。我的VBA技能很差。我得到运行时错误'424'

Sub formatAllCellsAsText()

    Dim wsTemp As Worksheet
    Dim LastRow As Long
    Dim LastColumn As Long
    Dim StartCell As Range
    For sht = 3 To Worksheets.Count
        Set wsTemp = Sheets(sht)
        Set StartCell = Range("A4")
        LastRow = wsTemp.Range("A1").CurrentRegion.Rows.Count
        LastColumn = wsTemp.Range("A1").CurrentRegion.Columns.Count
        For Each Cell In wsTemp.Range(StartCell, wsTemp.Cells(LastRow, LastColumn)).Cells

        If Not IsEmpty(Cell.Value) And IsNumeric(Cell.Value) And InStr(wsTemp.Cells(1, Cell.Column), "Client ID") <= 0 Then

            Dim Temp As Double
            Temp = Cell.Value
            Cell.ClearContents
            Cell.NumberFormat = "@"
            Cell.Value = CStr(Temp)
          End If
        Next
    Next sht
End Sub
子格式allcellsastext()
Dim wsTemp作为工作表
最后一排一样长
将最后一列变长
暗淡的StartCell As范围
对于sht=3的工作表。计数
设置wsTemp=板材(sht)
设置StartCell=范围(“A4”)
LastRow=wsTemp.Range(“A1”).CurrentRegion.Rows.Count
LastColumn=wsTemp.Range(“A1”).CurrentRegion.Columns.Count
对于wsTemp.Range中的每个单元格(StartCell、wsTemp.Cells(LastRow、LastColumn)).Cells
如果不是IsEmpty(Cell.Value)和IsNumeric(Cell.Value)以及InStr(wsTemp.Cells(1,Cell.Column),“客户端ID”)
应该是

Set StartCell = wsTemp.Range("A4")

我只想总结一下,因为我可能发现了这个问题。
在我发布的第一个代码中,我并没有设置后来引用的单元格变量。我认为这个单元格对于VBA来说是不言自明的

始终有助于准确指出哪一行抛出错误您应该在
sht
中包含元音。这可能很重要。除了告诉我们哪一行触发了错误,还请包括错误的文本。我们不能总是记住哪个文本属于哪个错误号@蒂姆:我试图指出错误行,用空行将其与其他行分开。我认为有更好的方法,但我还没有学会。顺便说一句,谢谢你的回复-这是有益的,但没有解决问题error@Cindy:错误为“需要对象”
Set StartCell = wsTemp.Range("A4")