partial sheet name activating sheet

jordanburch

Active Member
Joined
Jun 10, 2016
Messages
440
Office Version
  1. 2016
Hey guys,

I have the below. When it says sheets("Clearedsheet").activate it gives me an object not defined error. It seems to work on other files just this one not sure why its erroring out.

VBA Code:
Sub LoopThroughFiles()
Dim MyObj As Object
Dim MySource As Object
Dim file As Variant
Dim wbThis                  As Workbook     'workbook where the data is to be pasted, aka Master file
Dim wbTarget                As Workbook     'workbook from where the data is to be copied from, aka Overnights file
Dim LastRow As Long
Dim sht1 As Worksheet
Dim sht2 As Worksheet
Application.DisplayAlerts = False
Application.ScreenUpdating = False
'set to the current active workbook (the source book, the Master!)
Set wbThis = ActiveWorkbook
   
Set sht1 = wbThis.Sheets("Sheet1")

Folder = "C:\Users\jordan.burch.ctr\Desktop\Combine files\"
Fname = Dir(Folder)

While (Fname <> "")

  Set wbTarget = Workbooks.Open(FileName:=Folder & Fname)
  wbTarget.Activate
  Application.DisplayAlerts = False
Application.ScreenUpdating = False

  Dim ws As Worksheet
    Dim ClearedSheet As String
    ClearedSheet = ""
    For Each ws In ActiveWorkbook.Worksheets
        If InStr(1, ws.Name, "ER", vbTextCompare) Then
            ClearedSheet = ws.Name
           Sheets(ClearedSheet).Activate
            With ActiveSheet
      If .AutoFilterMode Then
        If .FilterMode Then
            .ShowAllData
        End If
    Else
        If .FilterMode Then
            .ShowAllData
        End If
    End If
    End With

  
            Exit For
        End If
        Application.DisplayAlerts = False
Application.screeupdating = False
    Next
   If ClearedSheet <> "" Then


  Range("a2:T40000").Copy

  wbThis.Activate

 'Just add this line:
  LastRow = sht1.Range("b1").End(xlDown).Row + 1
 'And alter this one as follows:
  sht1.Range("a" & Range("A" & Rows.Count).End(xlUp).Row + 1).PasteSpecial

 Fname = Dir

 'close the overnight's file
  wbTarget.Close
End If
Wend
 
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub




Any thoughts?

Jordan
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Hi,​
as this variable is useless so directly use Ws.Activate but hoping the sheet is visible rather than hidden …​
And just using this worksheet variable activating it is so useless too !​
 
Upvote 0
In fact I just change my mind 'cause your post was not well reflecting your code so read my last version of my previous post, thanks.​
 
Upvote 0

Forum statistics

Threads
1,214,991
Messages
6,122,628
Members
449,095
Latest member
bsb1122

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