473,396 Members | 1,714 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Convert excel vba to vb.net

hi good day to all, i have this code to compare two excel sheets and show their differences in the 3rd sheet but the code is in vba format and i want to convert this to vb.net. i am asking for help regarding this problem. im new here and i appreciate your help thank you.

here is the code:

Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2. Dim miMaxColumns As Integer
  3. Sub CompareSheets()
  4. Dim bChanged As Boolean, baChanged() As Boolean
  5. Dim iColEnd As Integer, iCol As Integer, iCol1 As Integer, iCol2 As Integer
  6. Dim lRow1 As Long, lRow2 As Long, lReportRow As Long
  7. Dim objDictOld As Object, objDictNew As Object
  8. Dim vKeys As Variant, vKey As Variant
  9. Dim vaInput() As Variant, vaOutput() As Variant, vaOutput2() As Variant
  10. Dim vaInputOld As Variant, vaInputNew As Variant
  11. Dim wsOld As Worksheet, wsNew As Worksheet, wsReport As Worksheet
  12.  
  13.  
  14. Set wsOld = Sheets("Sheet1")
  15. miMaxColumns = wsOld.Cells(1, Columns.Count).End(xlToLeft).Column
  16. Set objDictOld = PopulateDictionary(WS:=wsOld)
  17. Set wsNew = Sheets("Sheet2")
  18. Set objDictNew = PopulateDictionary(WS:=wsNew)
  19.  
  20. Set wsReport = Sheets("Sheet3")
  21.  
  22. With wsReport
  23.     .Cells.ClearFormats
  24.     .Cells.ClearContents
  25. End With
  26.  
  27. wsOld.Range("A1:" & wsOld.Cells(1, miMaxColumns).Address).Copy
  28. wsReport.Range("B1").PasteSpecial xlPasteValues
  29. Application.CutCopyMode = False
  30.  
  31. lReportRow = 1
  32. vKeys = objDictOld.Keys
  33. For Each vKey In vKeys
  34.     ReDim vaInputOld(1 To 1, 1 To miMaxColumns)
  35.     vaInputOld = objDictOld.Item(vKey)
  36.     If objDictNew.exists(vKey) Then
  37.         ReDim vaInputNew(1 To 1, 1 To miMaxColumns)
  38.         vaInputNew = objDictNew.Item(vKey)
  39.         ReDim vaOutput(1 To 1, 1 To miMaxColumns + 1)
  40.         ReDim vaOutput2(1 To 1, 1 To miMaxColumns + 1)
  41.         ReDim baChanged(1 To miMaxColumns)
  42.         bChanged = False
  43.         For iCol = 1 To miMaxColumns
  44.             vaOutput(1, iCol + 1) = vaInputOld(1, iCol)
  45.             If vaInputOld(1, iCol) <> vaInputNew(1, iCol) Then
  46.                 vaOutput2(1, iCol + 1) = vaInputNew(1, iCol)
  47.                 baChanged(iCol) = True
  48.                 bChanged = True
  49.             End If
  50.         Next iCol
  51.         If bChanged Then
  52.             lReportRow = lReportRow + 1
  53.             For iCol = 1 To UBound(baChanged)
  54.                 If baChanged(iCol) Then
  55.                     With wsReport
  56.                         .Range(.Cells(lReportRow, iCol + 1).Address, _
  57.                                .Cells(lReportRow + 1, iCol + 1).Address).Interior.Color = vbYellow
  58.                     End With
  59.                 End If
  60.             Next iCol
  61.  
  62.             vaOutput(1, 1) = "Changed"
  63.             With wsReport
  64.                 .Range(.Cells(lReportRow, 1).Address, _
  65.                        .Cells(lReportRow, miMaxColumns + 1).Address).Value = vaOutput
  66.                 lReportRow = lReportRow + 1
  67.                 .Range(.Cells(lReportRow, 1).Address, _
  68.                        .Cells(lReportRow, miMaxColumns + 1).Address).Value = vaOutput2
  69.             End With
  70.         End If
  71.         objDictOld.Remove vKey
  72.         objDictNew.Remove vKey
  73.     Else
  74.         ReDim vaOutput(1 To 1, 1 To miMaxColumns + 1)
  75.         vaOutput(1, 1) = "Deleted"
  76.         For iCol = 1 To miMaxColumns
  77.             vaOutput(1, iCol + 1) = vaInputOld(1, iCol)
  78.         Next iCol
  79.  
  80.         lReportRow = lReportRow + 1
  81.         With wsReport
  82.             .Range(.Cells(lReportRow, 1).Address, .Cells(lReportRow, miMaxColumns + 1).Address).Value = vaOutput
  83.             '-- Set the row to light grey
  84.             .Range(.Cells(lReportRow, 2).Address, .Cells(lReportRow, miMaxColumns + 1).Address).Interior.ColorIndex = 15
  85.         End With
  86.     End If
  87. Next vKey
  88.  
  89. If objDictNew.Count <> 0 Then
  90.     vKeys = objDictNew.Keys
  91.     For Each vKey In vKeys
  92.         ReDim vaOutput2(1 To 1, 1 To miMaxColumns + 1)
  93.         vaInputNew = objDictNew.Item(vKey)
  94.         vaOutput2(1, 1) = "Inserted"
  95.         For iCol = 1 To miMaxColumns
  96.             vaOutput2(1, iCol + 1) = vaInputNew(1, iCol)
  97.         Next iCol
  98.         lReportRow = lReportRow + 1
  99.         With wsReport
  100.             .Range(.Cells(lReportRow, 1).Address, .Cells(lReportRow, miMaxColumns + 1).Address).Value = vaOutput2
  101.             '-- Set the row to light green
  102.             .Range(.Cells(lReportRow, 2).Address, .Cells(lReportRow, miMaxColumns + 1).Address).Interior.ColorIndex = 4
  103.         End With
  104.     Next vKey
  105. End If
  106.  
  107. objDictOld.RemoveAll
  108. Set objDictOld = Nothing
  109. objDictNew.RemoveAll
  110. Set objDictNew = Nothing
  111. End Sub
  112. Private Function PopulateDictionary(ByRef WS As Worksheet) As Object
  113. Dim lRowEnd As Long, lRow As Long
  114. Dim rCur As Range
  115. Dim sKey As String
  116.  
  117. Set PopulateDictionary = Nothing
  118. Set PopulateDictionary = CreateObject("Scripting.Dictionary")
  119. lRowEnd = WS.Cells(Rows.Count, "A").End(xlUp).Row
  120. For lRow = 2 To lRowEnd
  121.     sKey = Trim$(LCase$(CStr(WS.Range("A" & lRow).Value)))
  122.     On Error Resume Next
  123.     PopulateDictionary.Add key:=sKey, Item:=WS.Range(WS.Cells(lRow, 1).Address, _
  124.                                             WS.Cells(lRow, miMaxColumns).Address).Value
  125.     On Error GoTo 0
  126. Next lRow
  127. End Function
  128.  
  129.  
Jun 18 '14 #1
0 1215

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: rama | last post by:
I need to convert excel file to txt file programatically which may contain 60000 records i want to convert it into txt file and upload it into unix server thanks in advance rama
1
by: Arvind P Rangan | last post by:
Hi, Trying to convert excel file data to base64 for encryption. Can anyone help me on this. Thanks Arvind
2
by: karups | last post by:
Hi when i convert Excel file to dataset using the following code, i find that, some col. such as Col1 ------ 404 403 NOT 222
10
subashini Thiyagarajan
by: subashini Thiyagarajan | last post by:
i want to convert excel into pdf file,i had lot ofoptions to convert all free trials i tried but nothing is supporting hyperlinks can any one help me with the useful URL thanks
0
by: payelit21 | last post by:
Hi Friends, can any body help me to convert excel cell from date to general and passinfg value to min value and max value of axis in chart through VB. Thanks Payel
3
by: binny | last post by:
i use jet engine ,and oledb database but with this i get four errors which r compile time errores,plz help me to solve these errors i wrote this programe bt it is not working . using System.Web;...
3
by: lyne_asp | last post by:
Please help me to convert excel date to asp date. Here is my code objConnEx.open "Provider=Microsoft.Jet.OLEDB.4.0;Data...
2
kamill
by: kamill | last post by:
I need to convert excel file into PDF file using PHP, how can i do it. I am able to generate a pdf file. Is there any solution to export excel data into mysql database? I am waiting for help.
0
by: Raymond Chiu | last post by:
Dear all, In my vb.net program, I am using excel object to create excel file and stored in a folder. How can it be converted to be pdf file automatically using dotnet coding or others? Actually...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.