Quantcast
Channel: Excel Help Forum - Excel Programming / VBA / Macros
Viewing all articles
Browse latest Browse all 50049

Close Workbook And Rename Worksheet

$
0
0
Hello,
I got that vba code somwhere in the forum after some little amendment I took it into my work
The code does the following

Code:

Sub OpenFiles()
Dim Folderpath As String
Dim cell As Range
Dim r, LRow As Single
Dim CWB As Workbook
'Sheets("NewData").Name = "OldData"
Folderpath = Range("A1").Value
Set CWB = ActiveWorkbook

If Right(Folderpath, 1) <> "\" Then
  Folderpath = Folderpath & "\"
End If

For Each chkbx In ActiveSheet.CheckBoxes
    If chkbx.Value = 1 Then
        For r = 1 To Rows.Count
            If Cells(r, 1).Top = chkbx.Top Then
                Workbooks.Open FileName:=Folderpath & Range("A" & r).Value
                Sheets("Sheet1").Copy After:=Workbooks("CAPIProjectManagement.xlsm").Sheets(ThisWorkbook.Sheets.Count)
                Sheets("Sheet1").Name = "NewData"
                 
         
                Exit For
            End If
        Next r
        CWB.Activate
    End If
Next

End Sub


Based on the checkbox marked it opens that workbook copy Sheet1 and rename the copied worksheet with "NewData".


I want that vba code to close that workbook from where Sheet1 is copied after copying the worksheet.

I want it to check before copying if there is any worksheet named "NewData" in my active(CAPIProjectManagement.xlsm) workbook.If exists then rename the Sheet named "NewData" into "OldData" (And if there is already sheet named "OldData" then delete it) before copying and give the name "NewData" to newly copied worksheet.


I tried to do but I couldn't.:confused:


Any help will be appreciated!


Thank you

Viewing all articles
Browse latest Browse all 50049

Trending Articles