我有一些工作表,例如M&MFIN.NS,M&M.NS,L&TFH.NS,我正在尝试查找其中一个,然后执行特定任务。 但是,如果找不到上述工作表之一,则代码终止(退出子)。 我需要帮助,如果找不到工作表,则应转到下一个搜索选项,然后其余代码
请指导
Sub SearchSheetNameandcreatenewsheet()
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Dim sName As String
Dim sFound As Boolean
sName = "M&MFIN.NS"
If sName = "" Then Exit Sub
sFound = False
On Error Resume Next
ActiveWorkbook.Sheets(sName).Select
Range(Range("E3"), Range("E3").End(xlDown)).Select
Selection.Copy
Worksheets("Close Price").Activate
Cells.Find(What:="M&MFIN.NS", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
ActiveCell.Offset(1, 0).Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Dim sName1 As String
Dim sFound1 As Boolean
sName1 = "M&M.NS"
If sName1 = "" Then Exit Sub
sFound1 = False
On Error Resume Next
ActiveWorkbook.Sheets(sName1).Select
Range(Range("E3"), Range("E3").End(xlDown)).Select
Selection.Copy
Worksheets("Close Price").Activate
Cells.Find(What:="M&M.NS", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
ActiveCell.Offset(1, 0).Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Dim sName2 As String
Dim sFound2 As Boolean
sName2 = "L&TFH.NS"
If sName2 = "" Then Exit Sub
sFound2 = False
On Error Resume Next
ActiveWorkbook.Sheets(sName2).Select
Range(Range("E3"), Range("E3").End(xlDown)).Select
Selection.Copy
Worksheets("Close Price").Activate
Cells.Find(What:="L&TFH.NS", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
ActiveCell.Offset(1, 0).Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("A1").CurrentRegion.Select
Selection.Replace What:="null", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
'"creating close price sheet seperataly"
Sheets("Close Price").Select
Sheets("Close Price").Copy
ChDir "C:\Lookback Momentum Analysis"
ActiveWorkbook.SaveAs Filename:= _
"C:\Lookback Momentum Analysis\Close Price.xlsx", FileFormat:= _
xlOpenXMLWorkbook, CreateBackup:=False
ActiveWindow.Close
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Worksheets("Parameters").Activate
End Sub
答案 0 :(得分:0)
这是一个选择:
Option Explicit
Sub SearchSheetNameandcreatenewsheet()
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Dim wb As Workbook, ws As Worksheet
Set wb = ThisWorkbook 'the workbook which has the code
For Each ws In wb.Worksheets
Select Case ws.Name
Case "M&MFIN.NS"
'code
Case "M&M.NS"
'code
Case "L&TFH.NS"
'code
End Select
Next ws
End Sub
您只需要提供工作表名称,并在每个Case
下为特定的工作表名称介绍代码。