Flora Edwards
asked on
VBA Import frm TBL does not work. earlier helped by Norie and Rgonzo1971
I had this question after viewing VBA modification help needed from earlier solution (VBA to modification to take it from table instead of sheet).
both of the codes from the earlier post, when i embedded it into my actual file does not work.
it cannot import from the table. the culpirt is either the line SourceWs.ListObjects(1) or somehow the table which i could not figure out the problem. attached is the file with code and the sample data file which has the table to be imported in the main file.
please run the macro on the MAIN file, when prompted select the file "Data-in Table in SHeet1" then you will see that the data from the table is not imported.
MAIN.xlsb
both of the codes from the earlier post, when i embedded it into my actual file does not work.
it cannot import from the table. the culpirt is either the line SourceWs.ListObjects(1) or somehow the table which i could not figure out the problem. attached is the file with code and the sample data file which has the table to be imported in the main file.
please run the macro on the MAIN file, when prompted select the file "Data-in Table in SHeet1" then you will see that the data from the table is not imported.
Sub UploadQuery()
Dim TargetWs As Worksheet, SourceWs As Worksheet
Dim TargetLR As Long, TargetLC As Long, SourceLR As Long, SourceLC As Long
Dim FolderPath As String, Filter As String, Caption As String, SourceFName As Variant
Dim SourceWB As Workbook, TargetWB As Workbook
Dim ClearRng As Range, CopyRng As Range
FolderPath = Application.ThisWorkbook.Path
ChDir FolderPath
Filter = "Excel files (*.xl*),*.xl*"
Caption = "Please Browse & Select the downloaded File "
SourceFName = Application.GetOpenFilename(Filter, , Caption)
If SourceFName = False Then
MsgBox "You have CANCELLED selection of needed FILE", vbCritical, "- FOLLOW INSTRUCTION"
Exit Sub
Else
Set SourceWB = Application.Workbooks.Open(SourceFName, Format:=xlDelimited, Local:=True)
'Disable Events
With Application
.ScreenUpdating = False
.DisplayStatusBar = True
.StatusBar = "!!! Please Be Patient...Updating Records !!!"
.EnableEvents = False
.Calculation = xlManual
End With
'Clear Old Data
Set TargetWB = Application.ThisWorkbook
Set TargetWs = TargetWB.Worksheets("MySHEET")
TargetLR = TargetWs.Range("A1").SpecialCells(xlCellTypeLastCell).Row
TargetLC = TargetWs.Range("A1").SpecialCells(xlCellTypeLastCell).Column
Set ClearRng = TargetWs.Range(TargetWs.Range("A1"), TargetWs.Cells(TargetLR, TargetLC))
If Not IsEmpty(ClearRng) = True Then
ClearRng.ClearContents
End If
'Copy Data From Source Workbook
Set lo = Nothing
Set SourceWs = SourceWB.Sheets(1)
On Error Resume Next
Set lo = SourceWs.ListObjects(1)
On Error GoTo 0
If Not lo Is Nothing Then
Set CopyRng = lo.Range
Else
SourceLR = SourceWs.Range("A1").SpecialCells(xlCellTypeLastCell).Row
SourceLC = SourceWs.Range("A1").SpecialCells(xlCellTypeLastCell).Column
Set CopyRng = SourceWs.Range(SourceWs.Range("A1"), SourceWs.Cells(SourceLR, SourceLC))
End If
'Patient Number
CopyRng.Copy
TargetWs.Range("A1").PasteSpecial xlPasteAll
Application.CutCopyMode = False
' Close Source Workbook
Application.DisplayAlerts = False
SourceWB.Close SaveChanges:=False
Application.DisplayAlerts = True
TargetWs.Activate
TargetWs.Columns.AutoFit
TargetWs.Range("A4").Select
'Enable Events
With Application
.ScreenUpdating = True
.DisplayStatusBar = True
.StatusBar = False
.EnableEvents = True
.Calculation = xlAutomatic
End With
MsgBox "!!! File Import Is Completed Now !!!"
End If
End Sub
Data-in-Table-in-Sheet1.xlsxMAIN.xlsb
ASKER
Thanks very much.
how can i modify the code. so that if the sheet is hidden then the sheets(1) should be the visible one and hidden sheet should be ignored.
how can i modify the code. so that if the sheet is hidden then the sheets(1) should be the visible one and hidden sheet should be ignored.
As I said...
use
Set SourceWs = SourceWB.Sheets("RRAP")
This point to the sheet by name rather than to an item is the Sheets collection.
The problem is not, that the sheet is hidden, the problem is that Sheets(1) points to the wrong sheet.
An in the source file you have two sheets, were one in hidden...
use
Set SourceWs = SourceWB.Sheets("RRAP")
This point to the sheet by name rather than to an item is the Sheets collection.
The problem is not, that the sheet is hidden, the problem is that Sheets(1) points to the wrong sheet.
An in the source file you have two sheets, were one in hidden...
ASKER
thanks Bembi.
my sheet name is not always PRAP. it can change.
my sheet name is not always PRAP. it can change.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you very much. very much appreciated.
the reason is very simple...
If you step through the macro, you find the line
Set SourceWs = SourceWB.Sheets(1)
if you show the result of that line...
i.e. put
debug.print SourceWB.Sheets(1).Name
you will see that the result is "_options" and not "RRAP" as expected...
That means, that the macro writes into this "_options" table....
The code works, if your change the line
Set SourceWs = SourceWB.Sheets(1)
tp either
Set SourceWs = SourceWB.Sheets(2)
or maybe better
Set SourceWs = SourceWB.Sheets("RRAP")