Delete a line help

depcdivr

Active Member
Joined
Jan 21, 2008
Messages
349
Office Version
  1. 365
Platform
  1. Windows
I have a macro that opens files within a directory that start with a certain string. Then I clear the contents of one sheet to refresh the data with the macro. After that I switch to a different sheet and I want to clear the contents of only the bottom line. Later in the macro I may be adding more lines and then will replace the cleared line at the end of the macro. Here is the code that I am using.

VBA Code:
Set fileNamesCollection = New Collection
        MyFile = Dir(strx & "\*.xlsx", vbDirectory)
        Do While MyFile <> ""
            fileNamesCollection.Add MyFile
            MyFile = Dir
        Loop
    
        For Each MyFile In fileNamesCollection
            
            If Left(MyFile, 5) = "Rep S" Then
                Set wbtemp = Workbooks.Open(strx & "\" & MyFile)
                Sheets("POS Data").Range("A2: N2000 ").ClearContents
                Set wstemp = wbtemp.Worksheets("POS Summary")
                irow = wstemp.Cells(Rows.Count, 1).End(xlUp).Row
                Sheets(wstemp).Range(wstemp.Cells(irow, 1), wstemp.Cells(irow, 160)).ClearContents
            End If
          
        Next

The first part of the code works fine but it gets hung up on
Code:
                Sheets(wstemp).Range(wstemp.Cells(irow, 1), wstemp.Cells(irow, 160)).ClearContents
returning a Run-Time error '13' Type Mismatch.

I recently modified the code that was working extending the range from 55 to 160. I ultimately want to delete the whole line or just the contents from column 1 to 160. what am I missing or doing wrong?
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Sheets(wstemp) should just be wstemp (although you could just omit it)
 
Upvote 0
Solution
Thats odd. I have run that code many times before and it worked. Made one change to the number and it failed. Your suggestion worked. Thank you
 
Upvote 0
You're welcome.
Sheets(wstemp) can never work like that as wstemp is a worksheet (an object) and Sheets is looking for a number or a string.
Sheets(wstemp.Name) would work (but is over complicating it) but not Sheets(wstemp)
 
Upvote 0

Forum statistics

Threads
1,214,948
Messages
6,122,420
Members
449,083
Latest member
Ava19

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