Description: What I am try to do is allow user to select excel file via browse then copy data from sheet 3 in selected file and paste to current workbook sheet2 (which name is Raw data(STEP 1) ). From the result in the current workbook sheet2 I want to copy the data to a new sheet and want to rename the sheet base on their file name but not the full string but just the ending such as M 100P 1.
Example of my file name(just a dummy) & it contains almost 20 file is the folder:
abcd_19-10-10_17-26_efgh-ijkl-02_ww1_line0_M 100P 1
abcd_19-10-10_18-33_efgh-ijkl-02_ww1_line0_M 100P 16
For now I am using inputbox to rename the sheet, as my code below:
Private Sub OpenWorkBook_Click()
Dim myFile As Variant
Dim OpenBook As Workbook
Application.ScreenUpdating = False
myFile = Application.GetOpenFilename(Title:="Browse your file", FileFilter:="Excel Files(*.xls*),*xls*")
If myFile <> False Then
Set OpenBook = Application.Workbooks.Open(myFile)
OpenBook.Sheets(3).Range("A2:R3063").Copy
ThisWorkbook.Worksheets("Raw data(STEP 1)").Range("A3").PasteSpecial xlPasteValues
OpenBook.Close True
ThisWorkbook.Sheets(3).Range("A9:O27").Copy
myVal = InputBox("Enter Sheet Name")
Sheets.Add After:=Sheets(ActiveWorkbook.Sheets.Count)
ActiveSheet.Name = myVal
ThisWorkbook.ActiveSheet.Range("A1").PasteSpecial xlPasteAllUsingSourceTheme
ThisWorkbook.ActiveSheet.Range("A1").PasteSpecial xlPasteValues
ThisWorkbook.ActiveSheet.Range("A1:O19").ColumnWidth = 10.8
Application.CutCopyMode = False
Application.ScreenUpdating = True
End If
End Sub
Edited code
If myFile <> False Then
Set OpenBook = Application.Workbooks.Open(myFile)
OpenBook.Sheets(3).Range("A2:R3063").Copy
WB.Worksheets(2).Range("A3").PasteSpecial xlPasteValues
OpenBook.Close True
WB.Sheets(3).Range("A9:O27").Copy
With WB
.Sheets.Add(After:=.Sheets(.Sheets.Count)).Name = myVal = Split(WB.Name, ".")(0)
.ActiveSheet.Range("A1").PasteSpecial xlPasteAllUsingSourceTheme
.ActiveSheet.Range("A1").PasteSpecial xlPasteValues
.ActiveSheet.Range("A1:O19").ColumnWidth = 10.8
End With
Application.CutCopyMode = False
Application.ScreenUpdating = True
End If
Is there anyways to do that without using the inputbox?
Any help will be appreciate