Excel VBA For Each Worksheet Loop

Try to slightly modify your code: Sub forEachWs() Dim ws As Worksheet For Each ws In ActiveWorkbook.Worksheets Call resizingColumns(ws) Next End Sub Sub resizingColumns(ws As Worksheet) With ws .Range(“A:A”).ColumnWidth = 20.14 .Range(“B:B”).ColumnWidth = 9.71 .Range(“C:C”).ColumnWidth = 35.86 .Range(“D:D”).ColumnWidth = 30.57 .Range(“E:E”).ColumnWidth = 23.57 .Range(“F:F”).ColumnWidth = 21.43 .Range(“G:G”).ColumnWidth = 18.43 .Range(“H:H”).ColumnWidth = 23.86 .Range(“i:I”).ColumnWidth = 27.43 … Read more

Query my excel worksheet with VBA

You can programmtically create an AutoFilter, then select the matching values: Dim ws As Worksheet: Set ws = ActiveSheet With ws .AutoFilterMode = False .Range(“1:1”).AutoFilter .Range(“1:1″).AutoFilter field:=2, Criteria1:=”=Saturday”, Operator:=xlAnd With .AutoFilter.Range On Error Resume Next ‘ if none selected .Offset(1).Resize(.Rows.Count – 1).Columns(2).SpecialCells(xlCellTypeVisible).Select On Error GoTo 0 End With .AutoFilterMode = False End With

Reducing WithEvent declarations and subs with VBA and ActiveX

Open Notepad and copy code below and paste it in a new txt-file save it als CatchEvents2.cls VERSION 1.0 CLASS BEGIN MultiUse = -1 ‘True END Attribute VB_Name = “CatchEvents2” Attribute VB_GlobalNameSpace = False Attribute VB_Creatable = False Attribute VB_PredeclaredId = False Attribute VB_Exposed = False Private Type GUID Data1 As Long Data2 As Integer … Read more

C# – How to add an Excel Worksheet programmatically – Office XP / 2003

You need to add a COM reference in your project to the “Microsoft Excel 11.0 Object Library“ – or whatever version is appropriate. This code works for me: private void AddWorksheetToExcelWorkbook(string fullFilename,string worksheetName) { Microsoft.Office.Interop.Excel.Application xlApp = null; Workbook xlWorkbook = null; Sheets xlSheets = null; Worksheet xlNewSheet = null; try { xlApp = new … Read more

Excel tab sheet names vs. Visual Basic sheet names

In the Excel object model a Worksheet has 2 different name properties: Worksheet.Name Worksheet.CodeName the Name property is read/write and contains the name that appears on the sheet tab. It is user and VBA changeable the CodeName property is read-only You can reference a particular sheet as Worksheets(“Fred”).Range(“A1”) where Fred is the .Name property or … Read more

How to add a named sheet at the end of all Excel sheets?

Try this: Private Sub CreateSheet() Dim ws As Worksheet Set ws = ThisWorkbook.Sheets.Add(After:= _ ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)) ws.Name = “Tempo” End Sub Or use a With clause to avoid repeatedly calling out your object Private Sub CreateSheet() Dim ws As Worksheet With ThisWorkbook Set ws = .Sheets.Add(After:=.Sheets(.Sheets.Count)) ws.Name = “Tempo” End With End Sub Above can be … Read more