VBA Help import file data from a specfic tab

AussieVic

Active Member
Joined
Jan 9, 2009
Messages
364
Hi, the below code imports data from the file path "rngOtcRg", how can i add to this so that the code imports from a specific tab, i have named range "rngMonth" where the cell will house the month which will reflect the worksheet which the code needs to find, if worksheet found then import data.

Code:
Sub RG()

Dim strFileName          As String
Dim lstRow               As Long
Dim lstRowSrc            As Range
Dim lstSrcCellRwNum      As Long
Dim shtDst               As Excel.Worksheet
Dim shtSrc               As Excel.Worksheet
Dim wkbThis              As ThisWorkbook
Dim wkbSrc               As Workbook
Dim shtTask              As Excel.Worksheet

  
Set shtTask = ThisWorkbook.Worksheets("Tasks")
 
 Application.ScreenUpdating = False
 
    strFileName = Range("rngOtcRg") & ".xls"

    If Dir(strFileName) = "" Then
            MsgBox prompt:="The File does not exist", _
            Buttons:=vbOKOnly + vbInformation
        GoTo ExitRoutine
      Else
         Set wkbSrc = Workbooks.Open(strFileName)
         Set shtSrc = wkbSrc.Worksheets(1)
         Set wkbThis = ThisWorkbook
         Set shtDst = wkbThis.Sheets("RawData")
    End If
     
    'Copy data from Downloaded data to Raw Worksheet
    With shtSrc
        Set lstRowSrc = .Cells(.Rows.Count, "A").End(xlUp)
                lstSrcCellRwNum = lstRowSrc.Row
         With .Range("A1:M" & lstSrcCellRwNum)
            On Error Resume Next
                Intersect(shtDst.UsedRange, shtDst.Range("A10:M" & Rows.Count)).ClearContents
            On Error GoTo 0
            .Copy shtDst.Range("A1")
            .EntireColumn.AutoFit
         End With
        Application.DisplayAlerts = False
            ActiveWorkbook.Close
        Application.DisplayAlerts = True

End Sub
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.

AlphaFrog

MrExcel MVP
Joined
Sep 2, 2009
Messages
16,451
Code:
Sub RG()

Dim strFileName          As String
Dim lstRow               As Long
Dim lstRowSrc            As Range
Dim lstSrcCellRwNum      As Long
Dim shtDst               As Excel.Worksheet
Dim shtSrc               As Excel.Worksheet
Dim wkbThis              As ThisWorkbook
Dim wkbSrc               As Workbook
Dim shtTask              As Excel.Worksheet

  
Set shtTask = ThisWorkbook.Worksheets("Tasks")
 
 Application.ScreenUpdating = False
 
    strFileName = shtTask.Range("rngOtcRg") & ".xls"
    
    ChDir "C:\temp"

    If Dir(strFileName) = "" Then
            MsgBox prompt:="The File does not exist", _
            Buttons:=vbOKOnly + vbInformation
        GoTo ExitRoutine
      Else
         Set wkbSrc = Workbooks.Open(strFileName)
         
         On Error Resume Next   ' Suspend error checking
            Set shtSrc = wkbSrc.Sheets(shtTask.Range("rngMonth").Value)
         On Error GoTo 0        ' Resume error checking
         If shtSrc Is Nothing Then
            MsgBox "Can't locate sheet " & shtTask.Range("rngMonth").Value & " in " & wkbSrc.Name
            GoTo ExitRoutine
         End If
         Set wkbThis = ThisWorkbook
         Set shtDst = wkbThis.Sheets("RawData")
    End If
    
    'Copy data from Downloaded data to Raw Worksheet
    With shtSrc
        Set lstRowSrc = .Cells(.Rows.Count, "A").End(xlUp)
                lstSrcCellRwNum = lstRowSrc.Row
         With .Range("A1:M" & lstSrcCellRwNum)
            On Error Resume Next
                Intersect(shtDst.UsedRange, shtDst.Range("A10:M" & Rows.Count)).ClearContents
            On Error GoTo 0
            .Copy shtDst.Range("A1")
            .EntireColumn.AutoFit
         End With
        Application.DisplayAlerts = False
            ActiveWorkbook.Close
        Application.DisplayAlerts = True
        
    End With
    
ExitRoutine:

End Sub
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,168,027
Messages
5,856,931
Members
431,839
Latest member
Guest user

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top