I am Attempting this code and use it. But for some reason when importing worksheet it uploads the worksheet that is selected by the user when prompted instead of naming the sheet 'Import'. There for cant hide the tab that is imported. Also I am attempting to copy data from 'Import' to new Sheet2, since the data from 'Import' will be different every time. need it to scan and import to selected range in new sheet2
This is original Post
This is original Post
VBA - Import Workbook/Choose Sheet/Display Details
Hello, This was a follow-on question related to another thread here on the board. At the recommendation of the board (the original question is more than 1400 days old), as well as the original solution giver, I am posting this with some of my own directed details. I am trying to do a mixture...
www.mrexcel.com
VBA Code:
Option Explicit
Sub ImportData()
Dim OpenBook As Workbook
Dim TargetFile As String, msg As String, FileName As String, response As String
Dim lngCount As Long, i As Long, x As Long, counter As Long
Dim ws As Worksheet
'Prompts user to select and open a workbook (no screen updating)
'The open/selected workbook is recorded as being "TargetFile"
'Open the file dialog
With Application.FileDialog(3) '3 = msoFileDialogOpen
.AllowMultiSelect = False
If .Show = -1 Then
For lngCount = 1 To .SelectedItems.Count
Set OpenBook = Workbooks.Open(CStr(.SelectedItems(lngCount))) 'Assumes the workbook is not already open
TargetFile = Dir(CStr(.SelectedItems(lngCount)))
Next lngCount
Else
Exit Sub
End If
End With
Application.ScreenUpdating = False
'The following code looks at the newly selected/opened workbook, then prompts the user to choose which worksheet to copy from
'The information is then copied into the 'Import' tab
msg = "Choose Project BOM to copy from """ & TargetFile & """?"
With Workbooks(TargetFile)
For i = 1 To .Worksheets.Count
msg = msg & vbCrLf & "(" & i & ") " & .Worksheets(i).Name
Next i
response = InputBox(msg, "Type numbers for sheets to import")
If response = "" Then Exit Sub 'check for cancel button
On Error Resume Next
Set ws = .Worksheets(CLng(response))
If Err.Number = 0 Then
ws.Copy After:=ThisWorkbook.Worksheets(ThisWorkbook.Worksheets.Count) 'Copies the chosen worksheet into 'Import' tab
ThisWorkbook.Worksheets(ThisWorkbook.Worksheets.Count).Name = "Import" 'Name uploads as the worksheet chossen by user not 'Import'
End If
On Error GoTo 0
End With
'This routine pastes the path of the chosen/selected file on cell B6 of the 'Status' tab/sheet
ThisWorkbook.Sheets("PAGE").Range("B2").Value = TargetFile
ThisWorkbook.Sheets("Import").Visible = False
'Closes the chosen workbook file without making any saves or changes (no screen updating)
Workbooks(TargetFile).Close SaveChanges:=False
Application.ScreenUpdating = True
'Attempting to copy data that is on 'Import' sheet to new Destination Sheet, rows may differ depending on Import data. Also need to import data but keep destination format
Sheets("Import").Range("B4:E4").Copy Destination:=Sheets("Sheet2").Range("A13:D13")End(xlUp).Value = .SpecialCells(xlConstants).Value
End Sub