Option Explicit
'
#If VBA7 Then
' Timer settings for 64Bit Excel
Private Declare PtrSafe Function getFrequency Lib "kernel32" Alias _
"QueryPerformanceFrequency" (cyFrequency As Currency) As Long
Private Declare PtrSafe Function getTickCount Lib "kernel32" Alias _
"QueryPerformanceCounter" (cyTickCount As Currency) As Long
#Else
' Timer settings for 32Bit Excel
Private Declare Function getFrequency Lib "kernel32" Alias _
"QueryPerformanceFrequency" (cyFrequency As Currency) As Long
Private Declare Function getTickCount Lib "kernel32" Alias _
"QueryPerformanceCounter" (cyTickCount As Currency) As Long
#End If
'
Sub GetDataFromWeb() ' 19 avg secs with IE.
'
Dim StartTime As Double
StartTime = MicroTimer ' Start the stopwatch
'
Dim FormulaCount As Long
Dim NA_EndRow As Long, NA_StartRow As Long
Dim LastRow As Long, LastRowDownloadedSheet As Long
Dim cel As Range
Dim CopiedData As String
Dim DownloadedSheetName As String, DownloadedWorkbook As String
Dim MainColumn As String
Dim RenamedPurchasesSheet As String
Dim WB As Workbook
Dim WS As Worksheet
'
Set WS = ThisWorkbook.Sheets("Purchases") ' <--- Set this to the sheet to sort
RenamedPurchasesSheet = "GSTIN verification" ' <--- Set this to the name of the renamed 'Purchases' sheet
'
MainColumn = "B" ' <--- Set this to the column to be used for last row
LastRow = WS.Range(MainColumn & WS.Rows.Count).End(xlUp).Row ' Get last row used in Column B of 'Purchases' sheet
'
' Step 1
'
If WS.Range(MainColumn & "2") = vbNullString Then ' If B2 in Purchases sheet is blank then ...
MsgBox "Enter GSTIN Number in column B." ' Display message to user
Exit Sub ' Exit the code
End If
'
'--------------------------------------------------------------------------------------
'
' Step 2
'
With WS
.UsedRange.Interior.Pattern = xlNone ' Clear the green filled cell in the used range of 'Purchases' sheet
.Range(Columns("K"), .Columns(Columns.Count)).ClearFormats ' Clear formatting from Columns K to end of 'Purchases' sheet
End With
'
Application.CopyObjectsWithCells = False ' Don't copy the Macro button in the following copy
'
WS.Copy after:=Sheets(Sheets.Count) ' Copy 'Purchases' sheet to end of workbook
ActiveSheet.Name = RenamedPurchasesSheet ' Name the copy of the 'Purchases' sheet
'
'--------------------------------------------------------------------------------------
'
' Step 3
'
With Sheets(RenamedPurchasesSheet)
.Sort.SortFields.Clear ' Clear the sort fields
.Range("A2:J" & LastRow).Sort Key1:=.Range(MainColumn & "2"), _
Order1:=xlAscending, Header:=xlNo ' Sort the Purchases copied sheet by 'As Per' column
'
'' .Range(Columns("K"), Columns(Columns.Count)).ClearFormats ' Clear formatting from Columns K to end of sheet
'
.Range("B2:B" & LastRow).Copy ' Copy data from renamed sheet to clipboard
End With
'
'--------------------------------------------------------------------------------------
'
' Step 5
'
CopiedData = CreateObject("htmlfile").parentWindow.clipboardData.GetData("Text") ' Save contents from clipboard into CopiedData
'
Dim CheckCount As Long
Dim WebProcessedGSTINs As Long
Dim Browser As Object
Dim TextWindow As Object
Dim WebSite As String
'
Set Browser = CreateObject("InternetExplorer.Application")
'
WebSite = "https://my.gstzen.in/p/gstin-validator/" ' <--- Set this to the website that you want to go to
'
With Browser
.Visible = False ' Set IE window status to Invisible
.Navigate WebSite ' Go to the website
'
Do While .Busy Or .ReadyState <> 4 ' Loop to wait for website to fully load
DoEvents ' Process any pending events
Loop ' Loop back
'
On Error Resume Next ' If an error occurs, ignore it and proceed with next line of code
For CheckCount = 1 To 100 ' Loop to check if TextWindow has appeared
Set TextWindow = .document.getElementsByClassName("cz-no-double-click") ' Check to see if TextWindow has appeared
If Not TextWindow Is Nothing Then Exit For ' If TextWindow has appeared then Exit this loop
Next ' Loop back
On Error GoTo 0 ' Return Error handling back to Excel
'
.document.getElementsByTagName("textarea")(0).innerText = CopiedData ' Copy CopiedData to text box on website
.document.querySelector("button[type=submit]").Click ' Click the 'Check GSTIN/UIN Numbers' button on website
'
Do While .Busy Or .ReadyState <> 4 ' Loop to wait for website to fully load
DoEvents ' Process any pending events
Loop ' Loop back
'
On Error Resume Next ' If an error occurs, ignore it and proceed with next line of code
For CheckCount = 1 To 100 ' Loop to check if all GSTIN #s have been processed by website
WebProcessedGSTINs = .document.getElementsByTagName("table")(0).getElementsByTagName("TR").Length ' Save # of processed GSTIN #s into WebProcessedGSTINs
If WebProcessedGSTINs > (Sheets(RenamedPurchasesSheet).Range("B2:B" & _
LastRow).Rows.Count) Then Exit For ' If website has processed all of the GSTIN #s then Exit For loop
Next ' Loop back
On Error GoTo 0
'
Set WB = Workbooks.Open(.document.querySelector("[class='pull-right btn btn-sm btn-excel']")) ' Open the download link into workbook
'
.Quit ' Close the browser
End With
'
DownloadedWorkbook = ActiveWorkbook.Name ' Save name of downloaded file to DownloadedWorkbook
ActiveSheet.Copy after:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count) ' Copy the sheet from downloaded file to ThisWorkbook
'
Workbooks(DownloadedWorkbook).Close SaveChanges:=False ' Close the downloaded file
'
'--------------------------------------------------------------------------------------
'
' Step 6
'
DownloadedSheetName = ActiveSheet.Name ' Save the downloaded sheet name to DownloadedSheetName
'
With Sheets(RenamedPurchasesSheet)
Sheets(DownloadedSheetName).Range("A1:L1").Copy .Range("K1:V1") ' Copy Header row from the downloaded sheet to RenamedPurchasesSheet
'
LastRowDownloadedSheet = Sheets(DownloadedSheetName).Range("A" & _
Sheets(DownloadedSheetName).Rows.Count).End(xlUp).Row ' Get last row used in the DownloadedSheet
'
For FormulaCount = 1 To 12 ' Loop to write formulas across range
.Cells(2, 10 + FormulaCount).Formula = _
"=VLOOKUP(B2,'" & DownloadedSheetName & "'!$A$2:$L$" & _
LastRowDownloadedSheet & "," & FormulaCount & ",0)" ' Write formula to cell
Next ' Loop back
'
.Range("K2:V2").AutoFill Destination:=.Range("K2:V" & LastRow) ' Fill the formulas down the range
.Range("K2:V" & LastRow).Copy ' Copy formula range into memory (Clipboard)
.Range("K2:V" & LastRow).PasteSpecial xlPasteValues ' Paste just the values back to range
'
Application.CutCopyMode = False ' Clear clipboard & 'marching ants' around copied range
'
.Range("L2:L" & LastRow).NumberFormat = "dd-mm-yyyy" ' Format Dates in Column L
.UsedRange.EntireColumn.AutoFit ' Autofit all of the columns on the destination Sheet
End With
'
Dim rng As Range
Dim rngFound As Range
'
Set rng = Sheets(RenamedPurchasesSheet).Range("K2:K" & LastRow)
'
Set rngFound = rng.Find("#N/A")
'
If rngFound Is Nothing Then
MsgBox "All GSTIN Numbers Matched."
GoTo WrapUp
Else
Sheets(RenamedPurchasesSheet).Range("A1:K" & LastRow).Copy WS.Range("A1") ' Copy columns of data to 'Purchases' sheet
'
With WS
NA_StartRow = .Range("K:K").Find(what:="#N/A", after:=.Range("K" & LastRow)).Row ' Find the start row of '#N/A' values
NA_EndRow = .Range("K:K").Find(what:="#N/A", after:=.Range("K1"), _
searchdirection:=xlPrevious).Row ' Find the end row of '#N/A' values
'
.Sort.SortFields.Clear ' Clear the sort fields
.Range("A2:K" & LastRow).Sort Key1:=.Range("K" & "2"), Order1:=xlAscending, _
Header:=xlNo ' Sort the Purchases sheet by column K
.Range("A" & NA_StartRow & ":K" & NA_EndRow).Interior.Color = 65535
'
.Sort.SortFields.Clear ' Clear the sort fields
.Range("A2:K" & LastRow).Sort Key1:=.Range("A" & "2"), Order1:=xlAscending, _
Header:=xlNo ' Sort the Purchases sheet by column A
'
.Columns("K:K").Delete ' Delete the no longer needed column K from Purchases sheet
End With
'
MsgBox "GSTIN not matching. Check & Edit."
End If
'
WrapUp:
Debug.Print "Time elapsed = " & (MicroTimer - StartTime) & " seconds." ' Display Elapsed Time into Immediate Window (CTRL+G)
MsgBox "Completed." ' Notify user that the script has finished
End Sub
Public Function MicroTimer() As Double ' Precision depends on the frequency of the CPU in the computer
'
' Code by Charles Williams originally
' Uses Windows API calls to the high resolution timer
' Returns time in seconds
'
Dim cyTicks1 As Currency
Static cyFrequency As Currency
'
MicroTimer = 0 ' Initialize MicroTimer to zero
'
If cyFrequency = 0 Then getFrequency cyFrequency ' Get ticks/second aka frequency
'
getTickCount cyTicks1 ' Get # of ticks
'
If cyFrequency Then MicroTimer = cyTicks1 / cyFrequency ' Calculate seconds ... seconds = Ticks/Frequency
End Function