/ / Spalten und Format als Datum finden - Excel, VBA, Excel-Vba

Finden Sie Spalten und Format als Datumsangaben - Excel, VBA, Excel-Vba

Ich habe diesen Code aus einem anderen Posting und ich gefundenfragte mich, wie ich das nur auf das Sheet3 anwenden konnte. Dieser Code funktioniert durch alle Blätter und ich brauche das nicht. Könnte mir jemand dabei helfen? Vielen Dank.

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

Antworten:

1 für die Antwort № 1

Ersetzen:

For Each ws In ThisWorkbook.Sheets

mit:

Set ws = worksheets("Sheet3")

und das letzte entfernen Next

Hoffe das hilft.


0 für die Antwort № 2

Sie können dies erreichen, indem Sie alle Referenzen von ersetzen wsDies ist die Variable, die die Blätter beim Durchlaufen aller Arbeitsblätter enthält, mit ThisWorkbook.Sheets("Sheet3"). Testen Sie den folgenden Code, beachten Sie jedoch, dass er nicht getestet wurde:

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

Grüße,


0 für die Antwort № 3

Ersetze das

For Each ws In ThisWorkbook.Sheets

mit

set ws = worksheets("sheet3")

und entferne die

Next

Am Ende.