简体   繁体   English

遍历工作表中的Excel VBA范围对象错误

[英]Excel VBA Range object Error in Looping Through Worksheet

I am trying to loop through different sheets in my workbook. 我试图遍历工作簿中的不同工作表。 I have tried searching through stackoverflow but still stuck with the error. 我尝试通过stackoverflow搜索,但仍然停留在该错误中。 The error showed "Method 'Range' of object'_Worksheet' failed. 错误显示“对象'_Worksheet'的方法'范围'失败。

The purpose of this code is to standardise all formats in the worksheets. 此代码的目的是标准化工作表中的所有格式。

Sub formatting()
Dim ws as Worksheet
Dim lastRow as long, lastColumn as long

lastRow = Range("A" & Rows.Count).End(xlUp).Row
lastColumn = (Cells(1, Columns.Count).End(xlToLeft).Column)

For each ws in Activeworkbook.Worksheets
With ws.Range(cells(lastRow, 1), cells(1, lastColumn))
    'rest of the actions I want to perform'
    .font.bold = true
End With
Next ws

End Sub

I just got started on Excel vba, please enlighten me! 我刚刚开始使用Excel vba,请赐教! Thank you! 谢谢!

您必须限定Cells格调用和Range调用:

With ws.Range(ws.cells(lastRow, 1), ws.cells(1, lastColumn))

Few changes to be made: 1. You have to redefine the last row and last column for each sheet. 几乎没有更改:1.您必须重新定义每张纸的最后一行和最后一列。 With that said, place the variable assignment inside the loop. 如此说来,将变量分配放在循环内。

  1. Instead of ws.Range(cells...) , the proper syntax is Range(ws.cells...) 而不是ws.Range(cells...) ,正确的语法是Range(ws.cells...)

See below: 见下文:

Sub formatting()
    Dim ws As Worksheet
    Dim lastrow As Long, lastcolumn As Long


    For Each ws In ActiveWorkbook.Worksheets
        lastrow = ws.Range("A" & Rows.Count).End(xlUp).Row
        lastcolumn = (Cells(1, Columns.Count).End(xlToLeft).Column)
        With Range(ws.Cells(1, 1), ws.Cells(lastrow, lastcolumn))
            'rest of the actions I want to perform'
            .Font.Bold = True
        End With
    Next ws

End Sub

If you just got started in VBA I'd learn about setting ranges using .currentregion this will greatly help you in the future. 如果您刚刚开始使用VBA,我将学习使用.currentregion设置范围的知识,这将在将来为您提供很大帮助。

The code snippet makes all fields bold. 该代码段使所有字段变为粗体。 If you are applying to all then currentregion can save code and possible errors on one condition "that all the fields are connected." 如果您要全部申请,那么currentregion可以在“所有字段都已连接”的一种情况下保存代码和可能的错误

Sub formattingWithCurrentRegion()
    Dim ws As Worksheet

    For Each ws In ActiveWorkbook.Worksheets
        ws.Range("A1").CurrentRegion.Font.Bold = True
    Next ws

End Sub

By setting the area as a range you can get greater control over ranges to set different formatting without having to deal with loops in loops. 通过将区域设置为范围,您可以更好地控制范围以设置不同的格式,而不必处理循环。 See below 见下文

Sub formattingWithCurrentRegion()
    Dim ws As Worksheet
    Dim rSheet As Range

    For Each ws In ActiveWorkbook.Worksheets
        '' sets the CurrentRegion as a range. Use the immediate window rSheet.Select to see what CurrentRegion does.
        Set rSheet = ws.Range("A1").CurrentRegion

        '' now you can work with it
        rSheet.Font.Bold = True

        '' make the 2 column font.color to Red
        rSheet.Resize(rSheet.Rows.Count, 1).Offset(0, 1).Font.Color = vbRed

        ''now clean up. Always set ranges to nothing when done with them.
        Set rSheet = Nothing
    Next ws

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM