我从不同的帖子中找到了这段代码,我想知道如何才能将它仅应用于Sheet3。此代码适用于所有工作表,我不需要。有人可以帮我这个吗?非常感谢。总是
Sub Sample()
Dim aCell As Range, bCell As Range
Dim ws As Worksheet
Dim lastRow As Long, i As Long
Dim ExitLoop As Boolean
For Each ws In ThisWorkbook.Sheets
Set aCell = ws.Rows(1).Find(what:="Date", LookIn:=xlValues, _
lookat:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
ExitLoop = False
If Not aCell Is Nothing Then
Set bCell = aCell
ws.Columns(aCell.Column).NumberFormat = "dd/mm/yyyy;@"
lastRow = ws.Range(Split(ws.Cells(, aCell.Column).Address, "$")(1) & _
ws.Rows.Count).End(xlUp).Row
For i = 2 To lastRow
With ws.Range(Split(ws.Cells(, aCell.Column).Address, "$")(1) & i)
.FormulaR1C1 = .Value
End With
Next i
ws.Columns(aCell.Column).AutoFit
Do While ExitLoop = False
Set aCell = ws.Rows(1).FindNext(After:=aCell)
If Not aCell Is Nothing Then
If aCell.Address = bCell.Address Then Exit Do
ws.Columns(aCell.Column).NumberFormat = "dd/mm/yyyy;@"
lastRow = ws.Range(Split(ws.Cells(, aCell.Column).Address, "$")(1) & _
ws.Rows.Count).End(xlUp).Row
For i = 2 To lastRow
ws.Range(Split(ws.Cells(, aCell.Column).Address, "$")(1) & i).FormulaR1C1 = _
ws.Range(Split(ws.Cells(, aCell.Column).Address, "$")(1) & i).Value
Next i
Else
ExitLoop = True
End If
Loop
End If
Next
答案 0 :(得分:1)
替换:
For Each ws In ThisWorkbook.Sheets
使用:
Set ws = worksheets("Sheet3")
并删除最后一个Next
希望这有帮助。
答案 1 :(得分:0)
您可以通过使用ws
替换ThisWorkbook.Sheets("Sheet3")
的所有引用来完成此操作,Sub Sample()
Dim aCell As Range, bCell As Range
Dim lastRow As Long, i As Long
Dim ExitLoop As Boolean
Set aCell = ThisWorkbook.Sheets("Sheet3").Rows(1).Find(what:="Date", LookIn:=xlValues, _
lookat:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
ExitLoop = False
If Not aCell Is Nothing Then
Set bCell = aCell
ThisWorkbook.Sheets("Sheet3").Columns(aCell.Column).NumberFormat = "dd/mm/yyyy;@"
lastRow = ThisWorkbook.Sheets("Sheet3").Range(Split(ThisWorkbook.Sheets("Sheet3").Cells(, aCell.Column).Address, "$")(1) & _
ThisWorkbook.Sheets("Sheet3").RoThisWorkbook.Sheets("Sheet3").Count).End(xlUp).Row
For i = 2 To lastRow
With ThisWorkbook.Sheets("Sheet3").Range(Split(ThisWorkbook.Sheets("Sheet3").Cells(, aCell.Column).Address, "$")(1) & i)
.FormulaR1C1 = .Value
End With
Next i
ThisWorkbook.Sheets("Sheet3").Columns(aCell.Column).AutoFit
Do While ExitLoop = False
Set aCell = ThisWorkbook.Sheets("Sheet3").Rows(1).FindNext(After:=aCell)
If Not aCell Is Nothing Then
If aCell.Address = bCell.Address Then Exit Do
ThisWorkbook.Sheets("Sheet3").Columns(aCell.Column).NumberFormat = "dd/mm/yyyy;@"
lastRow = ThisWorkbook.Sheets("Sheet3").Range(Split(ThisWorkbook.Sheets("Sheet3").Cells(, aCell.Column).Address, "$")(1) & _
ThisWorkbook.Sheets("Sheet3").RoThisWorkbook.Sheets("Sheet3").Count).End(xlUp).Row
For i = 2 To lastRow
ThisWorkbook.Sheets("Sheet3").Range(Split(ThisWorkbook.Sheets("Sheet3").Cells(, aCell.Column).Address, "$")(1) & i).FormulaR1C1 = _
ThisWorkbook.Sheets("Sheet3").Range(Split(ThisWorkbook.Sheets("Sheet3").Cells(, aCell.Column).Address, "$")(1) & i).Value
Next i
Else
ExitLoop = True
End If
Loop
End If
End Sub
是循环遍历所有工作表时保存工作表的变量。请测试以下代码,但请注意它未经测试:
>> df[df.cancel_date - df.login >= 3].user.value_counts().sort_index()
1 4
2 1
3 5
4 3
dtype: int64
此致
答案 2 :(得分:0)
替换
For Each ws In ThisWorkbook.Sheets
与
set ws = worksheets("sheet3")
并删除
Next
最后。