If worksheet name contains text then copy paste value columns

jaime1182

New Member
Joined
Dec 11, 2007
Messages
49
Office Version
  1. 2013
Platform
  1. Windows
Hi everyone.

I am having trouble trying to get a line of code to run.

VBA Code:
For Each ws In ActiveWorkbook.Worksheets
    If ws.Name Like "*INV*" Or ws.Name Like "*STM*" Then
        ws.Columns("A:D").Copy
        ws.Columns("A:D").PasteSpecial Paste:=xlPasteValues
        ws.Range("A1:P1").Copy
        ws.Range("A1:P1").PasteSpecial Paste:=xlPasteValues
    Next ws
 End If

What I want to do is if the tab name contains "INV" or "STM", I need to copy paste the values of columns A:D as well as A1:P1.

I am getting a "Compile error. Next without For" prompt at debug.

I don't know if my logic is wrong because I tried to troubleshoot by changing the command to hiding the columns and the debug did not turn up.

Code:
For Each ws In ActiveWorkbook.Worksheets
    If ws.Name Like "*INV*" Or ws.Name Like "*STM*" Then ws.Columns("A:D").Hidden = True
    Next ws
 End If

Where am I going wrong?

Would appreciate any help. Thanks in advance.
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Try this. (Not Tested)
Code:
Sub Maybe()
Dim ws As Worksheet, i As Long
    For i = 1 To ThisWorkbook.Worksheets.Count
        If InStr(ws(i).Name, "INV") <> 0 Or InStr(ws(i).Name, "STM") <> 0 Then
            With ws
                .Range("A1:D1").Resize(.UsedRange.Rows.Count).Value = .Range("A1:D1").Resize(.UsedRange.Rows.Count).Value
                .Range("E1:P1").Value = .Range("E1:P1").Value
            End With
        End If
    Next i
End Sub
 
Upvote 0
Where am I going wrong?

1661410849130.png


These two lines are in the wrong order. Just swap them.
 
Upvote 0
Solution
Apologies for the late response (had a medical emergency). Thanks guys! I'll give this a go!
 
Upvote 0
No problem re timing. Glad to see you back. :)
 
Upvote 0

Forum statistics

Threads
1,214,639
Messages
6,120,679
Members
448,977
Latest member
dbonilla0331

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