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

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
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
 
Upvote 0

Forum statistics

Threads
1,214,642
Messages
6,120,698
Members
448,979
Latest member
DET4492

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
Back
Top