VBA modification help needed from earlier solution (VBA to modification to take it from table instead of sheet)

I had this question after viewing VBA to modification to take it from table instead of sheet.

How can i modify the code in my earlier question, that if  the source worksheet has a table then take data from table
    'Copy Data From Source Workbook
    Set SourceWs = SourceWB.Sheets(1)
    Set CopyRng = SourceWs.ListObjects(1).Range  

Open in new window


If sheets(1) does not have a table then use the code below

   Set SourceWs = SourceWB.Sheets(1)
    SourceLR = SourceWs.Range("A2").SpecialCells(xlCellTypeLastCell).Row
    SourceLC = SourceWs.Range("A2").SpecialCells(xlCellTypeLastCell).Column
    Set CopyRng = SourceWs.Range(SourceWs.Range("A2"), SourceWs.Cells(SourceLR, SourceLC))

Open in new window

LVL 6
FloraAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rgonzo1971Commented:
Hi,

pls try
    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("A2").SpecialCells(xlCellTypeLastCell).Row
        SourceLC = SourceWs.Range("A2").SpecialCells(xlCellTypeLastCell).Column
        Set CopyRng = SourceWs.Range(SourceWs.Range("A2"), SourceWs.Cells(SourceLR, SourceLC))
    End If

Open in new window

Regards
1
NorieAnalyst Assistant Commented:
Here's a slightly different version.
    Set SourceWs = SourceWB.Sheets(1)

    With SourceWs

        If .ListObjects.Count = 0 Then
            SourceLR = .Range("A2").SpecialCells(xlCellTypeLastCell).Row
            SourceLC = .Range("A2").SpecialCells(xlCellTypeLastCell).Column
            Set CopyRng = .Range(.Range("A2"), .Cells(SourceLR, SourceLC))
        Else
            Set CopyRng = .ListObjects(1).Range
        End If

    End With

Open in new window

1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
FloraAuthor Commented:
Thank you very much Rgonzo1971 and Norie.

very much appreciated!    you guys saved me a lot of time.   it is really magical. i am so happy!
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

FloraAuthor Commented:
Dear Norie and Rgonzo1971

somehow the ListObject(1).Range does not work. i posted a new question here https://www.experts-exchange.com/questions/29070714/VBA-Import-frm-TBL-does-not-work-earlier-helped-by-Norie-and-Rgonzo1971.html
0
NorieAnalyst Assistant Commented:
Flora


How does it not work?
0
FloraAuthor Commented:
Thanks Norie.

it works now. it did not work because there was a hidden sheet.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VBA

From novice to tech pro — start learning today.