3

What my code do is that it will automatically get all the Excel files list in the folder and compare the Serial number on master list column then if the value is same it will copy the serial number and paste it on the C column. If both of the column formats are the same then it will work perfectly. But if the column is formatted in text, the comparison seems to fail. I've been searching quite some time to convert these cells to number format or general format so that the comparison can be successfully done without the concern on the format.

enter image description here

enter image description here

Sub Compare()

Dim Dic As Object
Dim fso As Object
Dim fldStart As Object
Dim fl As Object
Dim Mask As String, i As Long
Dim Wbk As Worksheet, w1 As Worksheet

Set fso = CreateObject("scripting.FileSystemObject")
Set fld = fso.GetFolder("C:\Users\kelvinwong\Desktop\Survey Testing")
Set w1 = Workbooks("Book1.xlsm").Sheets("Sheet1")

Set Dic = CreateObject("Scripting.Dictionary")

Mask = "*.xlsx"

For Each fl In fld.Files
    If fl.Name Like Mask Then
        Set Wbk = Workbooks.Open(fld & "\" & fl.Name).Sheets("Sheet1")
        i = Wbk.Cells.SpecialCells(xlCellTypeLastCell).Row
        For Each oCell In Wbk.Range("A2:A" & i)
            If Not Dic.exists(oCell.Value) Then
                Dic.Add oCell.Value, oCell.Offset(, 0).Value
            End If
        Next oCell

    End If
Next fl

i = w1.Cells.SpecialCells(xlCellTypeLastCell).Row

For Each oCell In w1.Range("A2:A" & i)
    For Each key In Dic
        If oCell.Value = key Then
            oCell.Offset(, 2).Value = Dic(key)
        End If
    Next
Next

End Sub
Teamothy
  • 2,000
  • 3
  • 16
  • 26
Kelvin Wong
  • 45
  • 1
  • 4

2 Answers2

3

Try to add the following line to your code: Columns("A").TextToColumns. It works.

Sorry for the minor edit. Someone downvoted my answer that turned out to be correct. T_T

  • This is not even close to a useful answer if you look at the question. Please delete or change your answer to match the question. TextToColumns method parses a column of cells that contain text into several columns. It has nothing to do with changing format of a cell – dbmitch Aug 05 '16 at 04:18
  • @dbmitch I tried this code to change numbers in text format to number format (General) in a column and it worked. I tried all my code before posting it as answers – Anastasiya-Romanova 秀 Aug 05 '16 at 04:23
  • 1
    Thank you Anastasiya for the solution. It works perfectly. – Kelvin Wong Aug 05 '16 at 04:33
  • You're very welcome Kelvin Wong. @dbmitch See it works? Now, would you reverse your downvote, please? :) – Anastasiya-Romanova 秀 Aug 05 '16 at 04:35
  • Will do - learn something everyday from this site! - Except for me to change my vote you need to edit it – dbmitch Aug 05 '16 at 04:40
  • @dbmitch I refresh the page many times but it's still in downvoted status. T_T – Anastasiya-Romanova 秀 Aug 05 '16 at 05:08
  • 1
    @dbmitch No worries. Thanks for reversing it. Thanks also for the (+1). I assume it comes from you :) – Anastasiya-Romanova 秀 Aug 05 '16 at 05:11
  • @Anastasiya-Romanova秀 actually what the code does ? I try to research and understand what it does but still yet confuse with it. Mind spare some time enlighten me ? Thanks :) – Kelvin Wong Aug 05 '16 at 09:20
  • @KelvinWong Did you mean why the code work? Well, actually I don't know either. I used to have a VBA code for changing text format to general but didn't work. So I tried to use this statement, it turned out the text format change to number format. I found it by accident. Try the following: type 1 in cell A1, by default Excel will change to number format (General). Now add an apostrophe (') before 1. The value will change to text format. Then select the cell A1 and then click Data > Text To Columns > Finish. The value will change again to number format. – Anastasiya-Romanova 秀 Aug 05 '16 at 09:32
  • @KelvinWong My guess, in order to form a text format, Excel seems to add a visible apostrophe (') before the character. Try to type an apostrophe in any cells. You won't be able to see it unless you see it from formula bar. – Anastasiya-Romanova 秀 Aug 05 '16 at 09:35
0

Another way

Let's say your range is from A1:A15 then you can do this as well

[A1:A15] = [INDEX(INT(A1:A15),)]

To understand what this does, you can see This post

Community
  • 1
  • 1
Siddharth Rout
  • 147,039
  • 17
  • 206
  • 250