How to get the excel file name / path in VBA

If you mean VBA, then you can use FullName, for example: strFileFullName = ThisWorkbook.FullName (updated as considered by the comments: the former used ActiveWorkbook.FullName could more likely be wrong, if other office files may be open(ed) and active. But in case you stored the macro in another file, as mentioned by user @user7296559 here, and …

Read more

Border around each cell in a range

You only need a single line of code to set the border around every cell in the range: Range(“A1:F20”).Borders.LineStyle = xlContinuous It’s also easy to apply multiple effects to the border around each cell. For example: Sub RedOutlineCells() Dim rng As Range Set rng = Range(“A1:F20”) With rng.Borders .LineStyle = xlContinuous .Color = vbRed .Weight …

Read more

AndAlso/OrElse in VBA

The only short circuiting (of a sort) is within Case expression evaluation, so the following ungainly statement does what I think you’re asking; Select Case True Case (myObject Is Nothing), Not myObject.test() MsgBox “no instance or test == false” Case Else MsgBox “got instance & test == true” End Select End Sub

Class (Static) Methods in VBA

1. Create a normal class containing the public method(s) you need to be ‘static’ 2. Include a public method [in this ‘static’ class] that initialises the [private] ‘static fields’ within the class (it can take parameters if you wish) 3. Create a module acts as a factory Public Function CreateStaticClass(parameters for ‘constructor’) As StaticClass Dim …

Read more

How to Freeze Top Row and Apply Filter in Excel Automation with C#

I figured it out! @Jaime’s solution to freezing the top row worked perfectly. And the following is my solution to applying the filter: Thanks, KBP // Fix first row workSheet.Activate(); workSheet.Application.ActiveWindow.SplitRow = 1; workSheet.Application.ActiveWindow.FreezePanes = true; // Now apply autofilter Excel.Range firstRow = (Excel.Range)workSheet.Rows[1]; firstRow.AutoFilter(1, Type.Missing, Excel.XlAutoFilterOperator.xlAnd, Type.Missing, true);

Quickest way to clear all sheet contents VBA

The .Cells range isn’t limited to ones that are being used, so your code is clearing the content of 1,048,576 rows and 16,384 columns – 17,179,869,184 total cells. That’s going to take a while. Just clear the UsedRange instead: Sheets(“Zeros”).UsedRange.ClearContents Alternately, you can delete the sheet and re-add it: Application.DisplayAlerts = False Sheets(“Zeros”).Delete Application.DisplayAlerts = …

Read more