VBA for HTML - Create HTML Table from Excel Table

Excel table to HTML table
Sub ExcelTableToHTMLTable()

    Dim ws As Worksheet
    Dim rng As Range
    Dim As integer, j as integer
    Dim txtFilePath As String
    Dim txtFileNumber As Integer
    Dim rowDataH As String, rowDataB As String, thStart1 As String,
thStart2 As String, thEnd1 As String,thEnd2 As String
    Dim tbStart1 As String, tbStart2 As String,tbStart3 As String, tbEnd1 As String,tbEnd2 As String,tbEnd3 As String

    'Set the worksheet you want to export from
    Set ws = ThisWorkbook.Sheets("Sheet1") 'Change "Sheet1" to your sheet name

    'Define the path to the text file
    txtFilePath = Application.GetSaveAsFilename( _
                        InitialFileName:="ExportedData.txt", _
                        FileFilter:="Text Files (*.txt), *.txt")

    If txtFilePath = "False" Then Exit Sub 'User cancelled the file dialog

    'Get a free file number
    txtFileNumber = FreeFile

    'Open the text file for output
    Open txtFilePath For Output As txtFileNumber

        'HTML code for table header
        thStart1 = "<div>" & vbNewLine & "<table align=""center"" style=""width: 50%;"">" & vbNewLine & "<thead>" & vbNewLine & "<tr>" & vbNewLine & "<th style=""background-color: #cccccc;"">"
        thStart2 = "<th style=""background-color: #cccccc;"">"
        thEnd1 = "</th>"
        thEnd2 = "</th>" & vbNewLine & "</tr>" & vbNewLine & "</thead>" & vbNewLine & "<tbody>"

        ' Loop through each cell for the table header
        For i = 1 To WorksheetFunction.CountA(Range("1:1"))
            If i = 1 Then
                rowDataH = thStart1 & Cells(1, i).Value & thEnd1
            ElseIf i = WorksheetFunction.CountA(Range("1:1")) Then
                rowDataH = thStart2 & Cells(1, i).Value & thEnd2
            Else
                rowDataH = thStart2 & Cells(1, i).Value & thEnd1
            End If
            Print #txtFileNumber, rowDataH
        Next i

        'HTML code for table body
        tbStart1 = "<tr style=""background-color: #f2f2f2;"">" & vbNewLine & "<td>"
        tbStart2 = "<tr style=""background-color: white;"">" & vbNewLine & "<td>"
        tbStart3 = "<td>"
        tbEnd1 = "</td>"
        tbEnd2 = "</td>" & vbNewLine & "</tr>"
        tbEnd3 = "</td>" & vbNewLine & "</tr>" & vbNewLine & "</tbody>" & vbNewLine & "</table>" & vbNewLine & "</div>"

        ' Loop through each cell for the table body
        For j = 2 To WorksheetFunction.CountA(Range("A:A"))
            For i = 1 To WorksheetFunction.CountA(Range("1:1"))
                If j Mod 2 = 0 Then
                    If i = 1 Then
                        rowDataB = tbStart1 & Cells(j, i).Value & tbEnd1
                    ElseIf i = WorksheetFunction.CountA(Range("1:1")) And j = WorksheetFunction.CountA(Range("A:A")) Then
                        rowDataB = tbStart3 & Cells(j, i).Value & tbEnd3
                    ElseIf i = WorksheetFunction.CountA(Range("1:1")) Then
                        rowDataB = tbStart3 & Cells(j, i).Value & tbEnd2
                    Else
                        rowDataB = tbStart3 & Cells(j, i).Value & tbEnd1
                    End If

                Else

                    If i = 1 Then
                        rowDataB = tbStart2 & Cells(j, i).Value & tbEnd1
                    ElseIf i = WorksheetFunction.CountA(Range("1:1")) And j = WorksheetFunction.CountA(Range("A:A")) Then
                        rowDataB = tbStart3 & Cells(j, i).Value & tbEnd3
                    ElseIf i = WorksheetFunction.CountA(Range("1:1")) Then
                        rowDataB = tbStart3 & Cells(j, i).Value & tbEnd2
                    Else
                        rowDataB = tbStart3 & Cells(j, i).Value & tbEnd1
                    End If
                End If
                'print each line into text file
                Print #txtFileNumber, rowDataB
            Next i
        Next j      

    ' Close the text file
    Close txtFileNumber

    MsgBox "HTML created successfully to " & txtFilePath

End Sub

Code explanation:
यह VBA macro Excel data को HTML table में convert करने के लिए बनाया गया है। Sub ExcelTableToHTMLTable() एक automation code है जो Excel sheet के data को पढ़कर उसे एक HTML formatted text file में save करता है। यह खास तौर पर Email Reporting, Web Page, Dashboard और MIS Report के लिए बहुत उपयोगी है।

इस code में Sheet1 को data source के रूप में set किया गया है। सबसे पहले यह user को Save As dialog box दिखाता है, जिससे user तय कर सकता है कि HTML output किस location पर save करना है। इसके बाद FreeFile का उपयोग करके एक नई text file खोली जाती है।

Code Excel की पहली row को Table Header (<th>) के रूप में convert करता है और हल्का grey background देता है। इसके बाद दूसरी row से लेकर आख़िरी row तक का data Table Body (<td>) में डाला जाता है। हर एक row के लिए अलग-अलग background color रखा गया है, जिससे HTML table पढ़ने में साफ और professional दिखे।

यह macro Excel VBA Loop, HTML Tags, Text File Handling और Dynamic Table Creation का बेहतरीन उदाहरण है। पूरा HTML structure जैसे <table>, <thead>, <tbody> automatic बनता है। अंत में file बंद करके एक message box दिखाया जाता है कि HTML file सफलतापूर्वक बन गई है।

अगर आप Excel से HTML Table Export, Excel VBA to HTML, या automated reporting सीखना चाहते हैं, तो यह code बहुत काम का है।


Open browser in background

Sub OpenEricollFolders()
Dim URL As String
Dim IE As InternetExplorer
   
'Create InternetExplorer Object
    Set IE = New InternetExplorerMedium
 
 With IE
    'Set IE.Visible = True to make IE visible, or False for IE to run in the background
    .Visible = False
    'Define URL
    URL = "https://yourdomain.sharepoint.com/sites/Finmigrationtest/"
    'Navigate to URL
    .Navigate URL
        Do While .ReadyState = 4: DoEvents: Loop
        Do While .ReadyState <> 4: DoEvents: 
Loop
    'Application.Wait (Now() + TimeValue("00:00:20"))
    .Quit
End With
    eEricollFolder = Shell("explorer.exe """ & SharepointAddress_LogFile & "", vbHide)
    'eEricollFolder1 = Shell("iexplorer.exe """ & "" & "", vbHide)
'Set IE = Nothing
    
End Sub

Code explanation:
यह VBA macro SharePoint website और folder को automatic खोलने के लिए उपयोग किया जाता है। Sub OpenEricollFolders() का मुख्य उद्देश्य Internet Explorer के माध्यम से एक SharePoint URL को background में access करना और फिर Windows Explorer में संबंधित SharePoint folder को खोलना है। यह code खास तौर पर Finance, ERP, e-Collection और Log File Access Automation के लिए उपयोगी है।

इस code में InternetExplorerMedium object का उपयोग किया गया है, जिससे Internet Explorer बिना user को दिखे (Visible = False) SharePoint site को load करता है। .Navigate command दिए गए URL पर browser को ले जाती है। Do While .ReadyState <> 4 loop यह सुनिश्चित करता है कि web page पूरी तरह load हो जाए, ताकि आगे की प्रक्रिया में कोई error न आए।

इसके बाद .Quit command से Internet Explorer को बंद कर दिया जाता है। फिर Shell("explorer.exe") का उपयोग करके SharePoint से जुड़े local या sync किए गए folder को Windows Explorer में खोला जाता है। इससे user को manually browser या folder ढूंढने की जरूरत नहीं पड़ती।

यह VBA code Excel VBA Automation, SharePoint Access, Internet Explorer Automation, Folder Auto Open और Productivity Enhancement के लिए बहुत उपयोगी है। अगर आप Excel से SharePoint folder या website automatic access करना चाहते हैं, तो यह macro एक आसान और professional समाधान प्रदान करता है।


Words count on a web page

Sub WordCountOnWebPage()
    Dim http As Object, html As Object, text As String
    Dim words As Variant
    
    Set http = CreateObject("MSXML2.XMLHTTP")
    http.Open "GET", "https://yourwebsite.com", False
    http.Send
    
    Set html = CreateObject("HTMLFile")
    html.body.innerHTML = http.responseText
    
    text = html.body.innerText
    words = Split(Trim(text))
    
    MsgBox "Word Count: " & UBound(words) + 1
End Sub

Code explanation:
यह VBA macro किसी भी web page का Word Count निकालने के लिए उपयोग किया जाता है। Sub WordCountOnWebPage() Excel या VBA editor से सीधे किसी website का content पढ़कर उसमें मौजूद शब्दों की कुल संख्या बताता है। यह code खास तौर पर SEO Analysis, Content Audit, Website Content Review और Digital Marketing से जुड़े लोगों के लिए बहुत उपयोगी है।

इस code में MSXML2.XMLHTTP object का उपयोग करके दिए गए URL से web page का HTML data GET Request के माध्यम से load किया जाता है। इसके बाद HTMLFile object की मदद से उस HTML को parse किया जाता है। innerText property केवल दिखाई देने वाला text निकालती है और HTML tag को हटा देती है।

इसके बाद Split function का उपयोग करके पूरे text को अलग-अलग शब्दों में तोड़ा जाता है। UBound(words) + 1 से कुल शब्दों की संख्या निकाली जाती है। अंत में MsgBox के जरिए user को website का Total Word Count तुरंत दिखा दिया जाता है।

यह VBA code Excel VBA Web Scraping, Website Text Analysis, SEO Word Count Tool, HTTP Request और HTML Parsing सीखने के लिए एक आसान और practical उदाहरण है। अगर आप बिना किसी third-party tool के website content का analysis करना चाहते हैं, तो यह macro एक सरल और प्रभावी समाधान है।


SharePoint File Download

#If Win64 Then
  Private Declare PtrSafe Function URLDownloadToFile Lib "urlmon" Alias _
          "URLDownloadToFileA" ( _
          ByVal pCaller As LongLong, _
          ByVal szURL As String, _
          ByVal szFileName As String, _
          ByVal dwReserved As LongLong, _
          ByVal lpfnCB As LongLongAs LongLong
#Else
  Private Declare Function URLDownloadToFile Lib "urlmon" Alias _
          "URLDownloadToFileA" ( 
_
          ByVal pCaller As Long, 
_
          ByVal szURL As String, 
_
          ByVal szFileName As String, 
_
          ByVal dwReserved As Long,
_
          ByVal lpfnCB As Long) As Long
#End If
Sub SharePointFileDownload()
Dim strUrl As String, strSavePath As String, strFile As String, countSlash As Byte
strUrl = "https://www.sharepoint.com/sites/filename.xlsb" 'SharePoint Path for the file
strSavePath = "C:\Users\username\Downloads\" 'Folder to save the file
'countSlash = ""
strFile = "filename.xlsb"
If DownloadFile(strUrl, strSavePath & strFile) Then
'If DownloadFile(strUrl, strSavePath) Then
    MsgBox "File saved to: " & vbNewLine & strSavePath
Else
    MsgBox "Unable to downloaf file:" & vbNewLine & strFile & vbNewLine & "Check url string and that document is shared", vbCritical
End If
End Sub


Function DownloadFile(Url As String, SavePathName As String) As Boolean
    DownloadFile = URLDownloadToFile(0, Replace(Url, "\", "/"), SavePathName, 0, 0) = 0
End Function

Code explanation:
यह VBA code SharePoint से file को automatic download करने के लिए उपयोग किया जाता है। यह खास तौर पर Excel VBA Automation, SharePoint Integration और File Download Automation के लिए बनाया गया है। #If Win64 Then condition यह सुनिश्चित करती है कि यह code 32-bit और 64-bit Windows दोनों में सही तरीके से काम करे।

इस code में URLDownloadToFile Windows की urlmon library का उपयोग करता है, जिससे किसी भी URL से file सीधे local system में save की जा सकती है। SharePointFileDownload subroutine में SharePoint file का पूरा URL, local save path और file नाम set किया गया है। इससे user को manually browser खोलकर download करने की जरूरत नहीं पड़ती।

DownloadFile function दिए गए URL से file को download करके तय किए गए folder में save करता है। अगर download सफल होता है तो message box में File saved का संदेश दिखता है, और अगर असफल होता है तो error message आता है। यह automatic checking code को और ज्यादा भरोसेमंद बनाती है।

यह VBA code Excel से SharePoint file download, Auto File Download, Windows API in VBA और Enterprise Automation के लिए बहुत उपयोगी है। अगर आप SharePoint से Excel, PDF या अन्य documents automatic download करना चाहते हैं, तो यह code एक आसान और professional समाधान है।


Open Share Point Folder

Sub OpenSharePointFolder()
Dim URL As String
Dim IE As SHDocVw.InternetExplorer
Set IE = New SHDocVw.InternetExplorer

With IE
    'Set IE.Visible = True to make IE visible, or False for IE to run in the background
    .Visible = True
    'Define URL
    URL = "https://www.sharepoint.com/sites/Shared%20Documents/Test/"
    'Navigate to URL
    .Navigate URL
End With
    
    Application.Wait (Now + TimeValue("0:00:02"))
    
    'reconnect Internet Explorer to VBA
        '*********************************************

        Dim shellWins As SHDocVw.ShellWindows
        
Dim explorer As SHDocVw.InternetExplorer
        
        Set shellWins = New SHDocVw.ShellWindows
        
        For Each explorer In shellWins
            If explorer.Name = "Internet Explorer" Then
                Set IEObject1 = explorer
                Debug.Print explorer.LocationURL
                Debug.Print explorer.LocationName
            End If
        Next

        
        Set shellWins = Nothing
        
Set explorer = Nothing
        '*********************************************
    
    Do While IEObject1.ReadyState <> READYSTATE_COMPLETE
        DoEvents
    Loop
IEObject1.Quit
Set IE = Nothing
Set IEObject1 = Nothing
End Sub

Code explanation:
यह VBA macro SharePoint folder को Internet Explorer के माध्यम से automatic खोलने के लिए उपयोग किया जाता है। Sub OpenSharePointFolder() का उद्देश्य Excel VBA से सीधे किसी SharePoint library या folder URL को access करना है। यह code खास तौर पर SharePoint Automation, Excel VBA Integration और Document Management के लिए उपयोगी है।

इस code में SHDocVw.InternetExplorer object का उपयोग करके एक नया Internet Explorer instance बनाया जाता है। .Visible = True रखने से browser user को दिखाई देता है, जिससे SharePoint login और navigation आसानी से हो जाता है। .Navigate command दिए गए SharePoint URL पर browser को ले जाती है।

Application.Wait का उपयोग यह सुनिश्चित करता है कि page load होने के लिए थोड़ा समय मिले। इसके बाद ShellWindows collection से खुले हुए Internet Explorer window को दोबारा VBA से connect किया जाता है। ReadyState check करके यह सुनिश्चित किया जाता है कि SharePoint folder पूरी तरह load हो चुका है।

अंत में .Quit के जरिए Internet Explorer को बंद कर दिया जाता है और सभी objects को clean किया जाता है। यह VBA code Excel से SharePoint folder खोलना, IE Automation, Enterprise File Access, और Process Automation के लिए बहुत उपयोगी है। अगर आप SharePoint folder को manually खोलने का समय बचाना चाहते हैं, तो यह एक सरल और प्रभावी समाधान है।


Sync Files

Sub SyncFiles()
  Dim f As String
  Const LaptopFolder = "C:\Users\username\MyFolder\"
  Const ServerFolder = "https://www.sharepoint.com/sites/Forms/AllItems.aspx"
  f = Dir(LaptopFolder & "*.*")
  
  While Len(f)
    GetAttr ServerFolder & f
    If Err Then
      Err.Clear
      FileCopy LaptopFolder & f, ServerFolder & f
    End If
    f = Dir()
  Wend
End Sub

Code explanation:
यह VBA macro local computer folder और SharePoint/server folder के बीच file sync करने के उद्देश्य से लिखा गया है। Sub SyncFiles() का मुख्य काम यह जांचना है कि local laptop folder में मौजूद कौन-सी files server या SharePoint location पर मौजूद नहीं हैं, और फिर उन्हें अपने आप copy कर देना है। यह code Excel VBA Automation, File Sync और Backup Process के लिए उपयोगी है।

इस code में LaptopFolder में local system का path दिया गया है, जहाँ से files ली जाएँगी। ServerFolder में server या SharePoint का address दिया गया है, जहाँ files sync करनी हैं। Dir function का उपयोग करके local folder की सभी files को एक-एक करके पढ़ा जाता है।

GetAttr statement यह check करता है कि वही file server folder में पहले से मौजूद है या नहीं। अगर file मौजूद नहीं होती, तो Err trigger होता है और उसी स्थिति में FileCopy command के जरिए file को server location पर copy कर दिया जाता है। इससे duplicate file copy नहीं होती।

यह VBA code Auto File Upload, Folder Synchronization, Excel VBA File Handling और Productivity Automation का सरल उदाहरण है। अगर आप manually file upload या backup करने का समय बचाना चाहते हैं, तो यह macro एक आसान और उपयोगी समाधान प्रदान करता है।


Close Internet Explorer

Sub CloseIE()
Dim IE As InternetExplorer
Set IE = New InternetExplorerMedium
    IE.Visible = True
    IE.Navigate "https://www.sharepoint.com/sites/"
    Application.Wait (Now() + TimeValue("00:00:05"))
IE.Quit
End Sub

Code explanation:
यह VBA macro Internet Explorer को automatic खोलने और बंद करने के लिए उपयोग किया जाता है। Sub CloseIE() का मुख्य उद्देश्य Excel VBA से Internet Explorer को control करना है, ताकि किसी website या SharePoint site को load करके browser को अपने आप बंद किया जा सके। यह code खास तौर पर SharePoint Access Automation, Browser Control और VBA Productivity के लिए उपयोगी है।

इस code में InternetExplorerMedium object का इस्तेमाल किया गया है, जिससे Internet Explorer का एक नया instance बनाया जाता है। .Visible = True रखने से browser screen पर दिखाई देता है। .Navigate command के जरिए दिए गए URL, जैसे SharePoint site, को Internet Explorer में खोला जाता है।

Application.Wait का उपयोग किया गया है ताकि web page को load होने के लिए कुछ second का समय मिल सके। इससे यह सुनिश्चित होता है कि site पूरी तरह खुल जाए, खासकर जब SharePoint login या network slow हो। तय समय के बाद .Quit command के जरिए Internet Explorer को बंद कर दिया जाता है।

यह VBA code Excel से Browser Automation, Internet Explorer VBA, SharePoint URL Auto Open, और Process Automation सीखने के लिए एक सरल उदाहरण है। अगर आप manually browser खोलने और बंद करने का समय बचाना चाहते हैं, तो यह macro एक आसान और प्रभावी समाधान प्रदान करता है।


Open Edge browser with predefined url

Sub Edge_Open()
Call Shell("C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe", vbNormalFocus)
CreateObject("Shell.Application").ShellExecute "microsoft-edge:https://www.sharepoint.com/sites/home.aspx"
Application.Wait (Now + TimeValue("00:00:10"))
DoEvents
End Sub

Code explanation:
यह VBA macro Microsoft Edge browser को automatic खोलने के लिए बनाया गया है। Sub Edge_Open() का मुख्य उद्देश्य Excel VBA से सीधे Microsoft Edge launch करना और किसी तय SharePoint website या web page को अपने आप खोलना है। यह code खास तौर पर Browser Automation, SharePoint Access और Office Productivity के लिए उपयोगी है।

इस code में Shell command का उपयोग करके Microsoft Edge का executable file (msedge.exe) run किया जाता है। vbNormalFocus से browser general mode में खुलता है और user को दिखाई देता है। इसके बाद Shell.Application का उपयोग करके microsoft-edge: protocol के जरिए सीधे दिए गए SharePoint URL को Edge में open किया जाता है।

Application.Wait और DoEvents यह सुनिश्चित करते हैं कि browser और website को पूरी तरह load होने के लिए पर्याप्त समय मिले। यह खासकर तब जरूरी होता है जब network slow हो या SharePoint login की जरूरत हो।

यह VBA code Excel से Microsoft Edge open करना, SharePoint URL Automation, Browser Control via VBA, और Manual Work Reduction के लिए बहुत उपयोगी है। अगर आप Excel macro के ज़रिए बार-बार SharePoint site खोलते हैं, तो यह code आपका समय बचाता है और काम को तेज़ बनाता है।


Open SharePoint using Shell

Sub OpenSharePointFolder()
    Dim sCmd As String
    Dim sURL As String
    
    sURL = "https://www.sharepoint.com/sites/test/"
    sCmd = "start microsoft-edge:" & sURL
    Shell "cmd /c """ & sCmd & """", vbHide
    
End Sub

Code explanation:
यह VBA macro SharePoint folder या website को Microsoft Edge browser में automatic खोलने के लिए उपयोग किया जाता है। Sub OpenSharePointFolder() का मुख्य उद्देश्य Excel VBA से बिना किसी manual step के सीधे SharePoint URL को open करना है। यह code खास तौर पर Excel VBA Automation, SharePoint Access और Browser Control के लिए बहुत उपयोगी है।

इस code में पहले SharePoint site का URL sURL variable में store किया जाता है। इसके बाद start microsoft-edge: command का उपयोग करके Edge browser में उस URL को खोलने का command तैयार किया जाता है। Shell "cmd /c" का उपयोग Windows Command Prompt के जरिए इस command को run करने के लिए किया गया है।

vbHide parameter से Command Prompt background में चलता है, जिससे user को कोई काली screen दिखाई नहीं देती। यह तरीका तेज़ और भरोसेमंद है, क्योंकि इसमें Internet Explorer या किसी पुराने browser object की जरूरत नहीं होती।

यह VBA code Excel से SharePoint folder open करना, Microsoft Edge Automation, CMD के जरिए browser launch, और Office Productivity Automation के लिए एक आधुनिक समाधान है। अगर आप Excel macro के माध्यम से SharePoint site या document library जल्दी खोलना चाहते हैं, तो यह code सरल, सुरक्षित और समय बचाने वाला विकल्प है।

एक टिप्पणी भेजें

Please do not enter any spam link in the comment box.

और नया पुराने