Vishesh's blog

Vishesh's picture

Solution-Copy data range to new worksheet

Solution for problem posted on url

http://excelexperts.com/copy-range-data-new-worksheet-when-matching-data...

Please see attached sheet. Click on the button provided on the sheet.

Vishesh's picture

Customised Progress Bar

Download the attached Excel workbook to see how the customised progress bar works. To implement this in your project simply copy the form (frmProgress) and general module (modProgressBar). And then, you just need to call ShowProgress procedure like this

Call ShowProgress("Task 4", 10)  
where "Task 4" is the task name and 10 is percentage completion of the task.

Customised Progress Bar

Vishesh's picture

VBA code to handle Access Imports and Query

Paste the following code in a general module
Public g_objConnection As ADODB.Connection
Public Const gc_strDBPath As String = "C:\Test.mdb"
 
Function blnConnectDatabase(strPath As String, strDBPass As String) As Boolean
'    If blnFileExists(strPath) = False Then
'        GoTo ErrH
'        Exit Function
'    End If
    Set g_objConnection = New ADODB.Connection
    On Error GoTo ErrH
    g_objConnection.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & _
        strPath & ";Jet OLEDB:Database Password=" & strDBPass & ";"
    On Error GoTo 0
    blnConnectDatabas
Vishesh's picture

Solution to Forum Ques

Solution for Forum Ques on url:

http://excelexperts.com/vishesh-need-macro-help

Vishesh's picture

Rows to column solved

Solution for problem asked in url: http://excelexperts.com/moving-data-rows-columns-depending-unique-conten...

Pls c attached sheet. I have tried to solve it using formula only (no macros as it would need more involvement).

Vishesh's picture

Filter/Find in Array

The following piece of code can be run from any general module. This code extracts all array elements based on the search criteria. You can also search the entire array element or part of it depending on the 3rd parameter of the function if its set to True or False.
Vishesh's picture

Count Array Dimensions

The following piece of code gives the number of dimensions an array has. In the TestRun procedure a range is assigned to variant and that variant is passed to GetArrayDimensions function as a parameter. GetArrayDimensions function returns the number of dimensions in an array. If the range has only one cell then it returns 0 as its not an array; if it has more than one cell its an array in which case it returns 2.
Vishesh's picture

Create In-Cell Chart in Excel using VBA

Paste the following code in a general module and pass the required parameters to create an chart in a cell. This code however works with Excel 2007 onwards.

In-Cell Chart

Vishesh's picture

Transpose Back

Solution for problem on url http://excelexperts.com/re-arrange-excel-data

Paste the following code in any module and run the Test procedure

Sub Test()
    Call TransposeBack(Sheet1.Range("A3:D10"), Sheet1.Range("I1"))
End Sub
 
Sub TransposeBack(rngData As Range, rngTarget As Range)
    Dim rngHeader       As Range
    Dim rngId           As Range
    Dim rngCell         As Range
    Dim rngDest         As Range
    Dim lngRept         As Long
 
    Set rngDest = rngTarget.Offset(1)
 
    With rngData
        Set rngHeader = Intersect(.Rows(1), .Rows(1).O
Vishesh's picture

Code generated ComboBox (ActiveX)

Manually add a combobox to Activesheet and right click to see its properties.
Vishesh's picture

Web Query Solution

Solution for problem posted on url:

http://excelexperts.com/automating-pulling-data-website

Please use the attached xl sheet. Change the symbol and click the button to see

Vishesh's picture

Copy Picture from Web to Excel

The following code pastes the picture from Web to the Excel range. The two parameters required by the function are the URL of the picture and address of target Excel range.
Vishesh's picture

Array to speed up (Range vs Array)

Below two procedures are different ways of populating a range of cells. The one at the bottom using arrays is the faster one. We can use the same to do processing/calculations in arrays and then put the calculated data back to range to speed up the process.
Vishesh's picture

Using Array to refer to multiple sheets

The following example code deletes multiple sheets in one go.
Sub DeleteMultipleSheets()
    Application.DisplayAlerts = False
    With ThisWorkbook.Worksheets(Array("Sheet2", "Sheet3"))
        .Delete
    End With
    Application.DisplayAlerts = True
End Sub
Vishesh's picture

Abbreviated Month name

DO NOT take first 3 characters from a month name to get an abbreviated month name. This can give you duplicate results in some languages as first 3 characters of two months may be same.
I have encountered this with the Czech language.

So, use the following:

If the month is 1 i.e. January

So, to get abbreviated month name ALWAYS use

Monthname(1, True)

This will always return standard abbreviated month name.

Vishesh's picture

Deleting all charts & shapes on a sheet

To delete all charts
sub DeleteAllCharts()
Sheet1.ChartObjects.delete
End Sub
To delete all shapes (this includes charts as well)
sub DeleteAllShapes()
Sheet1.Shapes.SelectAll
Selection.Delete
End Sub
Vishesh's picture

Create Chart from Array values

This code creates a chart from Array values only. Explore and modify this code to make more complex charts using array only.
Sub CreateChartFromArray()
    Dim objCht As ChartObject
    With Sheet1
        Set objCht = .ChartObjects.Add(10, 20, 500, 200)
        objCht.Chart.ChartWizard .Cells(1)
        objCht.Chart.SeriesCollection(1).Values = Array(56, 61, 45, 15, 30, 10)
        objCht.Chart.SeriesCollection(1).XValues = Array("A", "B", "C", "D", "E", "F")
    End With
    Set objCht = Nothing
End Sub
Vishesh's picture

Export Excel Range to Word Document in a Tabular Format

Paste the following piece of code in a general module and pass the range as a parameter.
Vishesh's picture

Zoom Select Range

This small piece of code can be very useful to keep the viewing area of a sheet across various screen resolutions when you deliver your project to your client.
Sub ZoomRange(rngZoom As Range)
    Application.Goto rngZoom
    ActiveWindow.Zoom = True
End Sub
 
Sub Test()
    Call ZoomRange(Sheet1.Range("A1:L23"))
End Sub
Vishesh's picture

Regular Expression to validate Email address

Pass a string as a parameter to the below procedure to check whether the string is a valid email id or not.
Public Function blnEmailValid(ByVal strEmailAdd As String) As Boolean
    With CreateObject("VBScript.RegExp")
        .IgnoreCase = True
        .Global = True
        .Pattern = "^([a-zA-Z0-9_\-\.]+)@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$"
        blnEmailValid = .Test(strEmailAdd)
    End With
End Function
 
Sub Test()
    MsgBox blnEmailValid("abc@EE.com")
End Sub
Vishesh's picture

Remove Duplicates (Get Uniques) Excel 2007

You can use the following simple procedure to remove duplicates either in place or get uniques at another range. The second parameter of the below procedure is optional. Try this in Excel 2007.
Sub RemoveDups(rngDups As Range, Optional rngTarget As Range)
    If rngTarget Is Nothing Then
        rngDups.RemoveDuplicates Columns:=1, Header:=xlNo
    Else
        rngDups.AdvancedFilter Action:=xlFilterCopy, CopyToRange:=rngTarget, Unique:=True
    End If
End Sub
 
Sub Test()
    Call RemoveDups(ActiveSheet.Range("A1:A10"), ActiveSheet.Range("D1"))
End Sub
Vishesh's picture

Simple way to add Event Proc skeleton in a sheet module

'Here is the code to add an Activate event to the sheet module.
Sub CreateEventProcedure(wks As Worksheet, strEvtProc As String)
    With ActiveWorkbook.VBProject.VBComponents(wks.CodeName).CodeModule
        'Add the blank procedure
        .CreateEventProc strEvtProc, "Worksheet"
    End With
End Sub
 
Sub TestProc()
    Call CreateEventProcedure(ThisWorkbook.Worksheets("Sheet2"), "Activate")
    MsgBox "Goto Sheet2 module in the code window. The activate procedure skeleton is added.", vbInformation, "Excel Experts"
End Sub
Vishesh's picture

Calling macro (with/without parameters) from other workbook

Following code runs the macro from other opened workbook.
Vishesh's picture

Open/Close CD Tray using VBA

Paste the following code in a module and try the two modules for open or close CD Tray.
Declare Sub mciSendStringA Lib "winmm.dll" (ByVal lpstrCommand As String, _
    ByVal lpstrReturnString As Any, ByVal uReturnLength As Long, _
    ByVal hwndCallback As Long)
 
Sub CDTrayOpen()
    mciSendStringA "Set CDAudio Door Open", 0&, 0, 0
End Sub
 
Sub CDTrayClose()
    mciSendStringA "Set CDAudio Door Closed", 0&, 0, 0
End Sub
Vishesh's picture

Check if VBProject is protected

This code lets you know whether the VB project is protected or not Add a reference to the Microsoft Visual Basic Extensibility Library It returns TRUE if the VB project in the active document is protected
Function ProtectedVBProject(ByVal wb As Workbook) As Boolean
    Dim intVBComp As Integer
    intVBComp = -1
    On Error Resume Next
    intVBComp = wb.VBProject.VBComponents.Count
    If Err.Number = 1004 Then
        MsgBox "Add reference to the Microsoft Visual Basic Extensibility Library", vbCritical, "Excel Experts"
    End If
    On Error GoTo 0
    If intVBComp = -
Vishesh's picture

Show Font List with Example

Here is a code that lists all installed fonts with example. Copy it in any module and run (F5)
Sub ShowInstalledFonts()
    Const StartRow As Integer = 4
    Dim cbcFontName As CommandBarControl, cbrFontCmd As CommandBar, strFormula As String
    Dim strFontName As String, i As Long, lngFontCount As Long, intFontSize As Integer
 
    intFontSize = 10
 
    If intFontSize = 0 Then Exit Sub
    If intFontSize < 8 Then intFontSize = 8
    If intFontSize > 30 Then intFontSize = 30
 
    Set cbcFontName = Application.CommandBars("Formatting").FindControl(ID:=1728)
 
Vishesh's picture

Dynamic Update solved

Solution for Question posted on url
http://excelexperts.com/node/1168

I have inserted a column at column A and concatenated the system and discipline. Used Vlookup to get the values. At some places it shows #N/A as there is no exact match of the discipline found.

Vishesh's picture

Generate Summary (Solved)

The attachment contains the solution for the problem posted on

http://www.excelexperts.com/node/1166

I have added a summary sheet and provided a button to generate the summary. Hope this solves your problem

Vishesh's picture

Display Images and Charts on Form

Attached is an excel workbook that shows how you can display all images and charts of a workbook on a form. See the general module in the code window to see the code that runs to make it happen. Modify it accordingly to suit your requirements.

Screen Shot:

ImageOnForm
Vishesh's picture

Reading from Clipboard

The following piece of code displays any text which is there in the clipboard. To try it paste the following code in the general module. Select any text from anywhere and press Ctrl+C to copy to clipboard. Now, run the following code. It will display what you selected and copied to clipboard using Ctrl+C.
Sub GetTextFromClipboard()
    'For DataObject add reference to Microsoft Forms 2.0 Object Library
    'If you cannot find this library just add a userform and
    'now you will find the library reference.
Syndicate content