srikanth sare
New Member
- Joined
- May 1, 2020
- Messages
- 30
- Office Version
- 2013
- Platform
- Windows
- MacOS
- Mobile
- Web
The Below code will copy newly added sheet using codename of another workbook to current workbook.
The Project vba is password protected
it works fine only for few workbooks and not all for all workbooks and sometime when vba is unprotected it works fine for all workbooks
kindly help in solving this error
The Project vba is password protected
it works fine only for few workbooks and not all for all workbooks and sometime when vba is unprotected it works fine for all workbooks
kindly help in solving this error
VBA Code:
Public Function SheetFromCodeName(Name As String, wbK As Workbook) As Worksheet
Dim Wks As Worksheet
For Each Wks In wbK.Worksheets
If Wks.CodeName = Name Then
Set SheetFromCodeName = Wks
Exit For
End If
Next Wks
End Function
Public Function GetLastCreatedSheet()
Dim lastAddedSheet As Worksheet
Dim oneSheet As Worksheet
Set lastAddedSheet = Worksheets(1)
For Each oneSheet In Worksheets
If Val(Mid(oneSheet.CodeName, 6)) > Val(Mid(lastAddedSheet.CodeName, 6)) Then
Set lastAddedSheet = oneSheet
End If
Next oneSheet
GetLastCreatedSheet = lastAddedSheet.CodeName
End Function
Private Sub COPY_PROJECT()
On Error GoTo EH
Application.Run "TurnOff"
Sheet4.Range("A1").ClearContents
Dim Path As String
Sheet3.Range("A1").Value = Environ("Username")
Select Case Sheet3.Range("A1").Value
Case "sri CA nth Sare"
Path = "G:\srik_CA_nth\PARTNERSHIP FIRMS\SARVAHITHA DEVELOPERS\FINAL PROJECTS"
Case "SARVAHITA"
Path = "F:\DRIVE\PROJECTS\BP OF FINAL PROJECTS"
End Select
Dim wbK As Workbook: Set wbK = Workbooks.Open(Path & Application.PathSeparator & Sheet1.Range("Y1").Value & ".xlsb", Password:="Ssca@1818")
Dim Wks As Worksheet: Set Wks = SheetFromCodeName(GetLastCreatedSheet, wbK)
Wks.Cells.Copy
Sheet4.Range("A1").PasteSpecial xlPasteAll
Application.CutCopyMode = False
On Error Resume Next
Sheet4.Range("A:A,D:F,H:BG,BK:BL,BP:DG,DQ:EB").EntireColumn.Delete Shift:=xlToLeft
On Error GoTo 0
CleanUp: On Error Resume Next
wbK.Close SaveChanges = False
Application.Run "TurnOn"
Exit Sub
EH: Debug.Print Err.Description ' Do error handling
MsgBox "Sorry, an error occured." & vbCrLf & Err.Description, vbCritical, "Error!"
Resume CleanUp
End Sub