excel 如何在新工作表上选择任意表?(VBA)

uujelgoq  于 2023-01-10  发布在  其他
关注(0)|答案(2)|浏览(145)

第一次张贴和真实的的初学者在VBA。
我试着写一个宏,它可以切换到下一个工作表(本质上是ctrl + pg dn函数),选择并突出显示表格中的值,然后清除内容。
我遇到的问题是宏似乎只从记录宏的地方选择一个表-而不是继续选择新工作表上的其他表。
我怎样才能得到代码来选择下一个表格呢?

Sub New_Hours()
'
' New_Hours Macro
' Moves down a sheet and removes previous hours
'
' Keyboard Shortcut: Ctrl+Shift+L
'
    ActiveSheet.Next.Select
    Range("Table13456789101112131415166188[[Sunday]:[Saturday]]").Select >> excel highlighted this as    the problem
    Selection.ClearContents
    Range("E9").Select
End Sub
4uqofj5v

4uqofj5v1#

清除Excel表格(ListObject)列

    • 主要**
Sub NewHours()
'
' New_Hours Macro
' Moves down a sheet and removes previous hours
'
' Keyboard Shortcut: Ctrl+Shift+L
'
    On Error GoTo ClearError
    
    With ActiveSheet.Next
        If ClearBetweenTableColumns(.ListObjects(1), "Sunday", "Saturday") Then
            Application.Goto .Range("E9")
        End If
    End With

ProcExit:
    Exit Sub
ClearError:
    'Debug.Print "Run-time error '" & Err.Number & "': " & Err.Description
    Resume ProcExit
End Sub
    • 方法**
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Purpose:      Clears the contents between two columns of an Excel table.
'               Returns a boolean indicating whether it was successful.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function ClearBetweenTableColumns( _
    ByVal Table As ListObject, _
    ByVal StartColumnID As Variant, _
    ByVal EndColumnID As Variant) _
As Boolean
    On Error GoTo ClearError

    With Table
        Dim sCol As Long: sCol = .ListColumns(StartColumnID).Index
        Dim eCol As Long: eCol = .ListColumns(EndColumnID).Index
        Dim cCount As Long: cCount = eCol - sCol + 1
        .DataBodyRange.Resize(, cCount).Offset(, sCol - 1).ClearContents
    End With

    ClearBetweenTableColumns = True

ProcExit:
    Exit Function
ClearError:
    'Debug.Print "Run-time error '" & Err.Number & "': " & Err.Description
    Resume ProcExit
End Function
    • 意外(主要分析)**
Sub NewHoursEDU()

    If ActiveSheet Is Nothing Then Exit Sub ' no visible workbooks open
    If ActiveSheet.Index = ActiveSheet.Parent.Sheets.Count Then Exit Sub ' last

    Dim sh As Object: Set sh = ActiveSheet.Next
    If Not TypeOf sh Is Worksheet Then Exit Sub ' not a worksheet
    If sh.ListObjects.Count = 0 Then Exit Sub ' no table

    If ClearBetweenTableColumns(sh.ListObjects(1), "Sunday", "Saturday") Then
        Application.Goto sh.Range("E9")
    'Else ' an error occurred in the called procedure; do nothing!?
    End If

End Sub
fcg9iug3

fcg9iug32#

您可以将此作为学习“真正的”VBA(不是宏记录的VBA)的良好起点:

Public Sub clearHours()

Dim ws As Worksheet
Dim lo As ListObject
Dim columnSunday As Long, columnSaturday As Long
Dim rgToClear As Range

For Each ws In ThisWorkbook.Worksheets
    For Each lo In ws.ListObjects
        With lo
            columnSunday = .ListColumns("Sunday").Index
            columnSaturday = lo.ListColumns("Saturday").Index
            
            Set rgToClear = .ListColumns(columnSunday).DataBodyRange.Resize(, columnSaturday - columnSunday + 1)
            rgToClear.ClearContents
            
        End With
    Next
Next

End Sub

该代码检查每个工作表,以及每个工作表上的每个表(= listobject)。
然后,它获取Sunday和Saturday的列索引,以从中检索要清除的范围(= DataBodyRange,因为您只想清除数据部分-而不是头,我假设)
最后一步是清除范围的内容。
(附加提示:阅读How to avoid using selectHow to avoid copy/paste会给您带来更多的见解。)

相关问题