Getting number of columns of a particular sheet while looping through sheets

athielem

New Member
Joined
Apr 22, 2020
Messages
9
Hi all!

I want to automatically check a number of files whether their column numbers corresponds to the number of columns of another sheet.

I have tried to solve that like this:

VBA Code:
Workbooks.Open Filename:=Path2 & NewList(o), ReadOnly:=True, UpdateLinks:=False

For Each Sheet In ActiveWorkbook.Sheets

If Sheet.Name = "korr" Then
Lastcol = ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column

Else        
End If         

Next Sheet

Workbooks(Filename).Close savechanges:=False
Filename = Dir()
The main problem is that it only identifies the correct number of columns when the sheet of interest (which is called "korr") was the last sheet to be activated; if it is another one, it simply counts the columns of the first sheet. Therefore it seems that I am not properly formulating the Lastcol variable because activesheet is not referring to the sheet name that I am looking for.

I would be very grateful if someone could give me a hint of what I'm doing wrong! :) Couldn't find an alternative way of establishing column numbers so far...

Best regards,
Andreas
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Try this, it will open the workbook and set LastCol according to that books sheet korr, if there is no korr sheet LastCol will be 0

VBA Code:
dim wb As Workbook
Set wb = Workbooks.Open(Filename:=Path2 & NewList(o), ReadOnly:=True, UpdateLinks:=False)

Lastcol = 0
On Error Resume Next
Lastcol = wb.Worksheets("korr").Cells(1, Columns.Count).End(xlToLeft).Column
On Error Goto 0
 
Upvote 0
Thank you very much! Works perfectly and it even helped me to adjust another part of the code that was causing an issue(y)
 
Upvote 0

Forum statistics

Threads
1,214,932
Messages
6,122,331
Members
449,077
Latest member
jmsotelo

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