gpt4 book ai didi

excel - 工作表循环、数组

转载 作者:行者123 更新时间:2023-12-02 11:03:50 31 4
gpt4 key购买 nike

我是初学者,我想在 Excel 文件的所有工作表中进行循环,执行此特定操作:将格式更改为具有特定标题的所有列。不幸的是,下面的代码仅适用于事件工作表,而不适用于事件工作簿中的其他工作表。你知道其中有什么问题吗?

非常感谢

Sub loop()

Dim timelist As Variant, sht as worksheet, rcell As Range,
rrow As Range, t As Integer

timelist = Array("Created", "Creation")

For Each sht In ActiveWorkbook.Worksheets

Set rrow = Range("A1.Z1")
For t = LBound(timelist) To UBound(timelist)
For Each rcell In rrow
rcell.Select
if rcell.Value = timelist(t) Then
ActiveCell.Offset(1, 0).Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.NumberFormat = "dd/mm/yyyy hh:mm:ss"
End If
Next rcell
Next t
Next sht

End Sub

最佳答案

循环工作表不会激活当前循环工作表,因此您必须限定对其的所有范围引用

您可以通过“With sht...End With” block 并在 block 内的所有范围引用之前添加一个点来做到这一点

并且不要“选择”任何东西,而只使用范围对象

For Each sht In ActiveWorkbook.Worksheets
With sht
Set rrow = .Range("A1:Z1")
For t = LBound(timelist) To UBound(timelist)
For Each rcell In rrow
If rcell.Value = timelist(t) Then
.Range(rcell.Offset(1, 0), rcell.Offset(1, 0).End(xlDown)).NumberFormat = "dd/mm/yyyy hh:mm:ss"
End If
Next rcell
Next t
End With
Next sht

关于excel - 工作表循环、数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53899731/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com