Loop through Sheets

elmnas

Board Regular
Joined
Feb 20, 2015
Messages
206
Hello people,


I need a script that loop through all sheets in a open workbook?

if a column first cell (always A1) have value="ordernr"
then
select the whole column


Could someone help me?

I wrote this could but doesn't work

Code:
Sub GetData()



Dim s As String
Dim Current As Worksheet
For Each Current In Worksheets
For Each C In ActiveSheet.UsedRange
If C.Value = "Ordernr" Then


Columns(1).Select
'Selection.Copy




'Workbooks("test1").Activate




'ActiveSheet.Name = "Company"




End If
Next
Next






End Sub

Thank you in advance
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
if a column first cell (always A1) have value="ordernr"
then
select the whole column


and do what with the selected column?
 
Upvote 0
This loops through the sheets but you don't say what to do so in this case it just copies column A where the value is found:

Code:
Sub LoopThroughWorkbook()

Dim sh As Worksheet

For Each sh In ActiveWorkbook.Worksheets
    If sh.Range("A1").Value = "Ordernr" Then
        sh.Range("A:A").Copy
    Exit For
    End If
Next sh

End Sub
 
Upvote 0
This loops through the sheets but you don't say what to do so in this case it just copies column A where the value is found:

Code:
Sub LoopThroughWorkbook()

Dim sh As Worksheet

For Each sh In ActiveWorkbook.Worksheets
    If sh.Range("A1").Value = "Ordernr" Then
        sh.Range("A:A").Copy
    Exit For
    End If
Next sh

End Sub

the code doesnt work for me, it still on the same sheet in the workbook hmm?
 
Upvote 0
The way you have written your code, I believe you may actually need to select the sheet first, i.e.
Code:
For Each sh In ActiveWorkbook.Worksheets
    If sh.Range("A1").Value = "Ordernr" Then
        sh.Activate
        sh.Range("A:A").Copy
        Exit For
    End If
Next sh
 
Upvote 0

Forum statistics

Threads
1,223,246
Messages
6,170,988
Members
452,373
Latest member
TimReeks

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