Very Useful Short VBA Codes

Resize Range

Range("a1").Resize(4, 5).Select


Filter Non-Blank Cells in a Range

Set rng=Range("a1").CurrentRegion

rng.autofilter Field:=5, Criteria:="<>"

or

rng.autofilter Field:=col, Criteria:="<>"

'Here col is a variable which holds column number

Filter Blank Cells in a Range

Set rng=Range("a1").CurrentRegion

rng.autofilter Field:=5, Criteria:="="

or

rng.autofilter Field:=col, Criteria:="="

'Here col is a variable which holds column number

Filter values other than some already known values in a Range

Dim var_Arr() as Variant, int_X as Integer

Dim rng_R as Range, rng_TitleRange as Range

ReDim var_Arr(10000) As Variant

        int_X = 1

        For Each rng_R In rng_TitleRange

                     If rng_R <> "A" And rng_R <> "B" And rng_R <> "C" And rng_R <> "D" And rng_R <> "E" And rng_R <> "F" And rng_R <> "G" And rng_R <> "H" And rng_R <> "I" And rng_R <> "J" Then

                           If rng_R = "" Then

                               var_Arr(int_X - 1) = "="

                               Else

                            var_Arr(int_X - 1) = rng_R.Value

                        End If

                        int_X = int_X + 1

                End If

        Next rng_R

    ReDim Preserve var_Arr(int_X)

    Range("A1").AutoFilter field:=int_col_1, Criteria1:=var_Arr, Operator:=xlFilterValues



Remove Filter Mode From A Worksheet

ActiveSheet.AutoFilterMode=False


Save Excel Workbook in .XLSM Format

ThisWorkbook.SaveAs "D:\Excel Practice\FileFolderPractice1stApr20", 52

Note: Here 52 is used to save the workbook in macro-enabled format.


Error Number and Error Description

Sub ErrNumber()

On Error Resume Next

i = 1 / 0

MsgBox Err.Number & "-" & Err.Description

End Sub


Find Column Number and assign it to a range

'-----Find Column Number-------------

Dim rng as Range

ActiveSheet.Rows("1:1").Select

'XYZ is the column name here

col = Selection.Find(what:="XYZ", After:=ActiveCell, LookIn:=xlFormulas, _

        LookAt:=xlWhole, searchorder:=xlByRows, searchdirection:=xlNext, _

        MatchCase:=False, SearchFormat:=False).Column

Set rng = Range(Cells(2, col), Range(Cells(Rows.Count, col), Cells(Rows.Count, col)).End(xlUp))

or

Set rng = Range(Cells(2, col),Cells(Rows.count,col))


Open a folder using Shell Object

var_FD = Shell("explorer.exe" & " " & ThisWorkbook.Path & "\Input Folder", vbNormalFocus)

Here var_FD is a variant type variable.

Above 1 line code will open a folder.

Comments

Popular posts from this blog

Power Automate - Automatically fetch data from Power BI in to Excel and Send the copy of the Excel file via Email

Separate Text (Characters) & Numbers from Alpha Numeric String Using Formula and Macro (VBA)

File System Object