Thank you for Visiting my Blog

Sunday, 18 June 2017

How to write data into notepad file without double quotes

Hi Guys!!!


In my previous post I used Write function to write the data into notepad file . Write function will write data with double quotes. To avoid the double quotes instead of WRITE function use PRINT,



 Sub Writenotepadwithoutquotes()

'Declaring variables
Dim TxtFile As String, rng As Range, TextValue As Variant, i As Integer, j As Integer, TextFile As Integer

'Provide the notepad file name

TxtFile = ActiveWorkbook.Path & "\result.txt"


'Determine the next file number available for use by the FileOpen function
  TextFile = FreeFile


  'Selecting the range . This can be also parameterized
 

Range("A1:E6").Select

Set rng = Selection
  
Open TxtFile For Output As #TextFile

For i = 1 To rng.Rows.Count
    For j = 1 To rng.Columns.Count
   
    TextValue = rng.Cells(i, j).Value
   
    If j = rng.Columns.Count Then
   
    'Print will write data without double quotes 


    Print #TextFile, TextValue

Else
    Print #TextFile, TextValue,
    
 End If


Next j

Next i
Close #TextFile
End Sub

Saturday, 17 June 2017

How to write data into Notepad from Excel using VBA



Below is the code to write data into notepad from Excel using write function. write function will delimit the data and write into the notepad but It will write data with double quotes.


Sub Writenotepadfile()

'------------Declaring variables
Dim TxtFile As String,
rng As Range,
TextValue As Variant,
i As Integer,
j As Integer


'Provide the notepad text file name with path


TxtFile = ActiveWorkbook.Path & "\result.txt"

'Select the range or parameterized it as per the requirement



Range("A1:E6").Select
Set rng = Selection
  
Open TxtFile For Output As #1

For i = 1 To rng.Rows.Count
    For j = 1 To rng.Columns.Count
   
    TextValue = rng.Cells(i, j).Value
   
    If j = rng.Columns.Count Then
  
    Write #1, TextValue
Else

    Write #1, TextValue,
 End If


Next j
Next i
Close #1

End Sub


 
 
 


Saturday, 10 June 2017

Login to a Webpage using VBA

Hi Guys,

Prerequisite :  Before start using below code please make sure :
  1. Add references to Microsoft Internet Controls (shdocvw.dll)
  2.  Microsoft HTML object Library.
 Below is the VBA code Please modified as per the need:

Sub Login_Webpage()

' Variable Declaration

    

    Dim ObjectIE As SHDocVw.InternetExplorer 'microsoft internet controls (shdocvw.dll)
    Dim htmlDoc As MSHTML.HTMLDocument 'Microsoft HTML Object Library
    Dim htmlInput As MSHTML.HTMLInputElement
    Dim htmlColl As MSHTML.IHTMLElementCollection

    Set ObjectIE = New SHDocVw.InternetExplorer
     


    With ObjectIE
        .Navigate "
https://www.facebook.com" ' desired URL
        .Visible = 1
        Do While .ReadyState <> 4: DoEvents: Loop
            Application.Wait (Now + TimeValue("0:00:02"))
            
             'set user name and password
            Set htmlDoc = .Document
            Set htmlColl = htmlDoc.getElementsByTagName("INPUT")
            Do While htmlDoc.ReadyState <> "complete": DoEvents: Loop
                For Each htmlInput In htmlColl
                    If htmlInput.Name = "email" Then  'This should be Name of Textbox ,which can be get using View source code of side
                        htmlInput.Value = "Username"
                   
                    End If
                   
                Next htmlInput
               
                 Do While htmlDoc.ReadyState <> "complete": DoEvents: Loop
                For Each htmlInput In htmlColl
                    If htmlInput.Name = "pass" Then
                        htmlInput.Value = "********"
                   
                    End If
                   
                Next htmlInput
               
                
                 'click login
                Set htmlDoc = .Document
                Set htmlColl = htmlDoc.getElementsByTagName("input")
                Do While htmlDoc.ReadyState <> "complete": DoEvents: Loop
                    For Each htmlInput In htmlColl
                        If Trim(htmlInput.Type) = "submit" Then  'This should be Type of Button
                            htmlInput.Click
                             Application.Wait (Now + TimeValue("0:00:05"))
                       
 

                            Exit For
                        End If
                    Next htmlInput
                End With
                
            End Sub

Thank you for Visiting ....

 

Sunday, 4 June 2017

How to get Last Column in a Row

There are various methods to find Last Column in a Row . Below listed is few of them:
#First:

'Last Column in Row using "End" method option:

Sub LastColumnInOneRow()

'Find the last used column in a Row: row 1 in this example
    Dim LastCol As Integer


    With ActiveSheet
        LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
    End With


    MsgBox LastCol
End Sub

Note:
#Second:


'Last Column in Row using Columns property
Sub LastColumns_Column_example()

    Dim LastCol As Long

    With ActiveSheet.UsedRange
        LastCol = .Columns(.Columns.Count).Column

    End With
    MsgBox LastCol

End Sub
#Third:
'Last Column  in  a Row using  " SpecialCells" option
Sub SpecialCells_Example_Column()
    Dim LastCol As Integer
    With ActiveSheet

        LastCol = .Range("A1").SpecialCells(xlCellTypeLastCell).Column

    End With
    MsgBox LastCol
End Sub
#Fourth:
'Last Column in a Row  using Find option
Sub Range_Find_Column()
'Finds the last non-blank cell on a sheet/range.
Dim lcol As Integer

    lcol = Cells.Find(What:="*", _
                    After:=Range("A1"), _
                    LookAt:=xlPart, _
                    LookIn:=xlFormulas, _
                    SearchOrder:=xlByRows, _
                    SearchDirection:=xlPrevious, _
                    MatchCase:=False).Column
   
    MsgBox "Last Row: " & lcol
End Sub
Note: all the places where I  hardcoded the row or column value that can be also parameterized.