kalcerro_1
New Member
- Joined
- Feb 28, 2020
- Messages
- 27
- Office Version
- 365
- Platform
- Windows
Hello.
I have a macro which is loading two worksheets from other workbook.
I want the macro to assign a name to those new sheets using a cell value. (Sheet #1 taking value from cell B4, sheet #2 need a generic one)
A third sheet inside the workbook is taking some values from new sheet #2.
The problem I'm facing is that with the code I have written I can only load one pair of sheets, when I try to load a new pair, one sheet already have that name taken.
Please take a look at the code below, and tell me what can I improve to have a functional code to load 'n" pair of sheets, with different names in it.
Thanks
Karin.
I have a macro which is loading two worksheets from other workbook.
I want the macro to assign a name to those new sheets using a cell value. (Sheet #1 taking value from cell B4, sheet #2 need a generic one)
A third sheet inside the workbook is taking some values from new sheet #2.
The problem I'm facing is that with the code I have written I can only load one pair of sheets, when I try to load a new pair, one sheet already have that name taken.
Please take a look at the code below, and tell me what can I improve to have a functional code to load 'n" pair of sheets, with different names in it.
Thanks
Karin.
VBA Code:
Private Sub CommandButton1_Click()
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Dim FileToImport As Variant
Dim WbCount As Integer
FileToImport = Application.GetOpenFilename(FileFilter:="XLSM's (*.xlsm), *.xlsm", Title:="Select Partner Result to import")
If FileToImport = False Then Exit Sub
MsgBox "This process will take a few seconds, please wait", vbOKOnly
Workbooks.Open FileToImport
Worksheets(Array("6.Results", "TechResults")).Copy After:=ThisWorkbook.Sheets(1)
Sheets("6.results").Select
Sheets("6.Results").Rows("38:67").Select
Selection.Delete Shift:=xlUp
Sheets("6.Results").Range("B4").Select
Sheets("6.Results").Name = ActiveSheet.Range("B4")
Sheets("TechResults").Visible = True
Sheets("TechResults").Select
Sheets("TechResults").Range("G1").Value = ("TPR1")
Sheets("TechResults").Name = ActiveSheet.Range("G1")
Sheets("Sheet1").Range("AA12").Formula = "=IFERROR('tpr1'!A2,0)"
Sheets("Sheet1").Range("AB12").Formula = "=IFERROR('tpr1'!F50,0)"
Sheets("Sheet1").Range("AC12").Formula = "=IFERROR('tpr1'!I50,0)"
Sheets("Sheet1").Range("AD12").Formula = "=IFERROR('tpr1'!L50,0)"
Sheets("Sheet1").Range("AE12").Formula = "=IFERROR('tpr1'!O50,0)"
Sheets("Sheet1").Range("AF12").Formula = "=IFERROR('tpr1'!R50,0)"
Sheets("Sheet1").Range("AG12").Formula = "=IFERROR('tpr1'!U50,0)"
Sheets("Sheet1").Range("AH12").Formula = "=IFERROR('tpr1'!Y50,0)"
Sheets("Sheet1").Range("AI12").Formula = "=IFERROR('tpr1'!AG50,0)"
Sheets("Sheet1").Range("AJ12").Formula = "=IFERROR('tpr1'!AO50,0)"
Sheets("Sheet1").Range("AK12").Formula = "=IFERROR('tpr1'!AS50,0)"
Sheets("Sheet1").Range("AL12").Formula = "=IFERROR('tpr1'!AV50,0)"
WbCount = Workbooks.Count
For i = WbCount To 1 Step -1
If Workbooks(i).Name <> ThisWorkbook.Name Then
Workbooks(i).Close
End If
Next i
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub