Skip to main content


Showing posts with the label VB.NET

Excel VBA copy or reference a value

Excel VBA provide options on how to pass value from worksheet to worksheet or from workbook to another workbook.
To optimize or make the VBA code run faster is to make the worksheet interaction lesser.
Recording an Excel VBA macro to copy and paste a value will have a similar output to the code below.
Sub Macro1() ' ' Macro1 Macro '     Range("A2").Select     Selection.Copy
    Sheets("Sheet2").Select     Range("A3").Select     ActiveSheet.Paste
End Sub
what the code does is to select the cell on the active sheet for copying and select another worksheet and select the cell where the value will be placed and paste the copied cell value.
Steps are: a. Select the cell and copy the value b. Select the worksheet where the value is to be copied c. Next step, is select the cell on the selected worksheet d. Paste the value on the cell
Steps are absolutely fine, if it works as expected. But if you are copying hundreds or thousands of cells. It will be a w…

Excel find last row with value in a specific column

1. How to find last row with value using vba in a specific column?

2. How to find last row with value  using vba in a worksheet?

The questions above are entirely two different scenarios.

The first question will find the last row with value in a specific column within the worksheet.

While the second question find the last row with value of the entire worksheet.

The first question is applicable if you need to find the last row in different columns.

Let's say Column 1 or Column A, or Column 2 or Column B etc.

Example code below will find the last row in a specific column via VBA code.

'command Cells(Rows.Count, 1) = find the last row with value in Column 1 for Sheet3

Dim last_row As Integer

last_row = Worksheets("sheet3").Cells(Rows.Count, 1).End(xlUp).Row

Msgbox last_row

'replace 1 with the desired column 
'ex: command Cells(Rows.Count, 7) = find the last row with value in Column 7

If you don't need to specify a worksheet or…

VB.Net remove line feed or new line

In or any programming language, if the program requires getting data from external programs, results or data from third party software that needs to be processed  may or may not have expected desired output.

For example, getting a string from a third party software. String is quite straight forward and it's quite basic.

But the issue arises,  if the desired output requires a specific string or text and other text should be discarded or deleted. Regex or any other methods might help on getting the specific text or data.

One basic issue, if the string needs to be formatted in one line but a line feed or a new line keeps breaking the line.

The first instinct that I think of, is to use "Trim" function but it doesn't solve the issue. Trim, Ltrim or Rtrim just delete spaces but it doesn't delete line feed.

In VB.Net a line feed can easily be removed by this code snippet below:

Dim strNoLineFeed As String = String_with_Line_Feed.Replace(vbCr, "").Replac…

Vbscript send email via Office 365

Sending automated email via Office 365 account requires the use of SSL or in some instances you need to use SSL and TLS both enabled on the script.

Below is an example that works fine using Vbscript.

This is useful if you need to automate email messages via Task Scheduler or Windows event attached to this script, when the event is triggered the script will execute the VBS file and send the message.

Modify the message subject, body and of course, the email domain name, user and password.

Open notepad copy and paste the code, save as "filename.vbs"

The quotes are important when doing save as via notepad or else the file will be save as .txt.

Enjoy! Cheers!

Dim xstrAll(), strMessage,intupper()

Set objFSO = CreateObject("Scripting.FileSystemObject")

'Send the mail

Sub SentMail

strMessage =  strMessage & "Your other message..."

strTo= "" 

Excel formula fixed cell address

How to insert a formula with a fixed cell address? Absolute referencing will come to the rescue.

Absolute reference in Excel is prefix with a dollar ($) sign.

$A$1: This means that Column "A" and Row "1" will not change when copied to another cell.



The above formula when copied from one cell to another cell, $A$1 will remain the same.

While A$1,$A2 will change its location when copied to another cell.

 A$1: This means that column "A" will change while row location (which is number 1) will not change when copied.

 So this absolute reference will change like D$1, G$1 or E$1 depends on the location. So number "1" value will never change.

 $A1: The column "A" will not change when copied from one cell to another cell, while the row value which is number "1" will change.

So the above absolute reference will change like $A3, $A10, $A12 etc., Column "A" will be constant while the row changes.


Word VBA Select Page

How to select a page in Word VBA?

Sub Select_Home()

'Select the first page of the document (page 1)
'Like pressing Ctrl + Home

Selection.GoTo wdGoToBookmark, , , "\StartOfDoc"

End Sub

Sub Select_document_end()

'Select the end of the document
'Like pressing "Ctrl+End"

Selection.GoTo wdGoToBookmark, , , "\EndOfDoc"

End Sub

Sub Select_via_Page_Number()

'specify the page number
'Example below will select page 5

Selection.GoTo wdGoToBookmark, , , "5"

End Sub

Tested and working using Word 2010.

Cheers..till next time :) Happy coding.

Free Android Apps:

Click on links below to find out more:

Linux Android App cheat sheet:

Catholic Rosary Guide  for Android:…

PowerShell split and join string

How to split a string by space using PowerShell?
PowerShell provides an easy way to do it using "split" method in PowerShell.
The split method accepts any delimiter; it can be space, dash, comma or other characters as long as it has a pattern on the string.
The split method will literally split the string and the output is displayed on a separate line or separated via a line feed.
$string = "This is my test string"
$split_string_array = $string -split " "
Output will be: This
If there is a need to remove the spaces but combine the string in one line, PowerShell also provides the "join" method which basically combine or joins the string in one line.
Example: $string = "This is my test string"
$split_string_array = $string -split " "
$join_string_array = $split_string_array -Join ""
Write-Host $join_string_array
Output: Thisismyteststring
Using the join method, a string can be manipu…

Set IP Address using Netsh and PowerShell

How to set IP Address of the remote computer using PowerShell?
How to set DNS IP Address of the remote computer using PowerShell?
How to set static IP and DNS address in local or remote computer via PowerShell?

Script below will set local IP or set remote IP Address using PowerShell with the help of Netsh command.

For the script to work Winrm or Windows Remote Management must be enabled either on the local PC or remote computer or server.
The script of course needs to run at an elevated mode with the proper rights or privileges or in simple words the script will work only if you have the administrator rights.

Here’s the script:
$set_IP= { netshinterfaceipsetaddress"Wi-Fi"static192.168.1.107255.255.255.0192.168.1.254}
$set_DNS1= {netshinterfaceipadddns"Wi-Fi" }
$SET_DNS2= { netshinterfaceipadddns"Wi-Fi"  }
$show_ipconfig= { ipconfig/all }

Invoke-Command-ScriptBlock$set_IP-ComputerName. Invoke-Command-ScriptBl…

PowerShell check video driver

Get the details of the video card such as driver, version and the video card name.
A simple script to get the details of the video card, script will get the driver and version to check whether a new driver needs to be installed or updated.
To check remote computers, replace the dot with the name of the remote computer to be checked.
Here’s the script:
"Video Name : "+$video_details.Name
"Video Description : "+$video_details.Description
"Video Driver Date : "+$video_details.DriverDate
"Video Version : "+$video_details.DriverVersion
"Video Processor :"+$video_details.VideoProcessor
"Video Drivers : "+$video_details.InstalledDisplayDrivers
Sample output:
Video Name : Intel(R) HD Graphics Video Description : Intel(R) HD Graphics Video Driver Date : 20121126000000.000000-000 Vide…