Comparing Calculated Value to Array Value

t_vanhoek

New Member
Joined
Nov 5, 2013
Messages
1
Hi, I am using excel 2010 and am trying to automate a tedious process that takes me about half an hour. The code below already works but it takes about 5-7mins to run.

Basically, I have a list of predefined numbers below that I insert into column A. Then I start at the far right of the table's header's and compare the unique predefined number of that header to the list I have put into column A. If the header's unique predefined number matches one of the numbers in my predefined list in column A the code moves on, if the header's unique predefined value does not match one of the numbers in my predefined list in column a, then it deletes the table's column. The table is very very very large and I'd like to speed up the code. I don't know if using arrays would do this or necessarily how to incorporate them. Any Ideas?
Rich (BB code):
Sub example1()


statusBarState = Application.DisplayStatusBar
calcState = Application.Calculation
eventsState = Application.EnableEvents
screenUpdateState = Application.ScreenUpdating
Application.DisplayStatusBar = False
Application.Calculation = xlCalculationManual
Application.EnableEvents = False
Application.ScreenUpdating = False


Dim lastrow2 As Integer, lastcolumn1 As Integer, Counter As Integer, variable1 as string
Dim i As Integer, x As Integer, PctDone As Single
Dim ws As Worksheet, lstList As ListObject


Set ws = ActiveSheet 'Beg this basically makes a table into a range to speed up the code if there is a table

For Each lstList In ws.ListObjects
    If ws.ListObjects(1) <> "" Then
        Ticker = 1
        ActiveSheet.ListObjects(1).Unlist
        Exit For
    End If
Next


If ActiveSheet.AutoFilterMode = True Then 'this turns off any filters
    ActiveSheet.AutoFilterMode = False
End If


Columns("A:A").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove


Range("A1").Value = "ID Number" 'Inserts the predefined list into column A
Range("A2").Value = "10761"
Range("A3").Value = "10188"
Range("A4").Value = "10748"
Range("A5").Value = "10657"
Range("A6").Value = "10012"
Range("A7").Value = "10653"
Range("A8").Value = "10665"
Range("A9").Value = "10084"
Range("A10").Value = "11402"
Range("A11").Value = "10178"
Range("A12").Value = "10085"
Range("A13").Value = "10179"
Range("A14").Value = "11397"
Range("A15").Value = "11398"
Range("A16").Value = "10086"
Range("A17").Value = "10028"
Range("A18").Value = "11085"
Range("A19").Value = "11424"
Range("A20").Value = "10684"
Range("A21").Value = "10102"
Range("A22").Value = "11240"
Range("A23").Value = "11313"
Range("A24").Value = "10663"
Range("A25").Value = "10096"
Range("A26").Value = "10664"
Range("A27").Value = "10210"
Range("A28").Value = "11456"
Range("A29").Value = "10399"
Range("A30").Value = "10326"
Range("A31").Value = "10104"
Range("A32").Value = "10106"
Range("A33").Value = "10149"
Range("A34").Value = "10578"
Range("A35").Value = "11086"
Range("A36").Value = "10661"
Range("A37").Value = "10108"
Range("A38").Value = "10098"
Range("A39").Value = "10206"
Range("A40").Value = "10724"
Range("A41").Value = "11087"
Range("A42").Value = "10046"
Range("A43").Value = "10208"
Range("A44").Value = "10110"
Range("A45").Value = "10099"
Range("A46").Value = "10207"
Range("A47").Value = "10290"
Range("A48").Value = "10686"
Range("A49").Value = "10728"
Range("A50").Value = "10209"
Range("A51").Value = "10111"
Range("A52").Value = "10100"
Range("A53").Value = "10240"
Range("A54").Value = "10094"
Range("A55").Value = "10173"


lastrow1 = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row 'counts the number of predefined numbers in column a


Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
lastcolumn2 = Selection.Columns.Count 'Counts the number of headers I need to cycle through


For i = lastcolumn2 To 21 Step -1 'Starts at the far right of the table's headers and goes toward the left (step-1)
    If Cells(1, i).Value <> "" Then 'if the header value does not equal ""
ID_Number = Left(Right(Cells(1, i).Value, 6), 5) 'trims out the predefined header value
        Cells(1, 1).Value = ID_Number 'puts the trimmed out header value in range A1
            For x = 1 To lastrow1 'For x from 1 to last row of the predefined list entered in column A
                If Cells(1, 1).Value = Cells(1 + x, 1).Value Then 'if trimmed header value in range A1  is equal to any of the values in the predefined list in column A
                    Exit For 'if its true, exit the for statement move to the next header
                End If
                Counter = Counter + 1 'adds a counter for the progress bar form
            Next
        If x > lastrow1 Then 'if x is greater than the variable lastrow1, which if none of the values in the above for statement matched the value in cell A1, then x will be greater than the variable lastrow1
            Range(Cells(1, i), Cells(lastcolumn2, i)).Delete 'then delete the table column
        End If
        PctDone = Counter / (lastcolumn2 * lastrow1) 'progress bar stuff
            With UserForm2 'progress bar stuff
                .FrameProgress.Caption = Format(PctDone, "0%") 'progress bar stuff
                .LabelProgress.Width = PctDone * (.FrameProgress.Width - 10) 'progress bar stuff
            End With 'progress bar stuff
'       The DoEvents statement is responsible for the form updating 'progress bar stuff
            DoEvents 'progress bar stuff
    End If
Next
Unload UserForm2 'progress bar stuff
Unload UserForm1 'progress bar stuff
Columns("A:A").Delete 'deletes the predefined list I inserted into column A


Range("A2").Select 
Range(Selection, Selection.End(xlToRight)).Select
lastcolumn2 = Selection.Columns.Count


lastrow2 = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row
ActiveSheet.ListObjects.Add(xlSrcRange, Range(Cells(2, 1), Cells(lastrow2, lastcolumn2)), , xlYes).Name = "Table1" 'reinserts a table if I deleted it at the beginning, and even if I didn't delete it at the begininng


Application.DisplayStatusBar = statusBarState
Application.Calculation = calcState
Application.EnableEvents = eventsState
Application.ScreenUpdating = screenUpdateState

End Sub
This code generally takes around 5 minutes to run all the way through, like I said, its a large table. I was thinking of making the predefined list in column A above into an array, and comparing the array to the variable ID_Number.

My question is, if I incorporated arrays in the above described way, would it make the code run faster, and if yes, then what would the code look like?

Thank you in advance for your input.
 
Last edited by a moderator:

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Hi..

Try this..

It puts each value in Column A into the Dictionary and then goes through the Column headers in Row 1 and if a Column Header value does NOT exist in the Dictionary then it adds that column headers address to a range and then deletes that range at the end..

Or at least thats what i does in my head.. :)

Code:
Private Sub CommandButton1_Click()
    Dim rRngToDelete As Range
    With CreateObject("scripting.dictionary")
        .CompareMode = 1
        For Each it In Columns(1).SpecialCells(2)
            x0 = .Item(it.Value)
        Next
        For Each j In Rows(1).SpecialCells(2)
            If Not .exists(j.Value) Then
                If rRngToDelete Is Nothing Then
                    Set rRngToDelete = Sheets("Sheet1").Range(j.Address)
                Else
                    Set rRngToDelete = Union(rRngToDelete, Sheets("Sheet1").Range(j.Address))
                End If
            End If
        Next j
    End With
    If Not rRngToDelete Is Nothing Then
        rRngToDelete.EntireColumn.Delete
    End If
End Sub

Heres the Worksheet structure i was testing it on..
<b>Sheet1</b><br /><br /><table border="1" cellspacing="0" cellpadding="0" style="font-family:Calibri,Arial; font-size:11pt; background-color:#ffffff; padding-left:2pt; padding-right:2pt; "> <colgroup><col style="font-weight:bold; width:30px; " /><col style="width:126px;" /><col style="width:64px;" /><col style="width:64px;" /><col style="width:64px;" /><col style="width:64px;" /><col style="width:64px;" /><col style="width:64px;" /><col style="width:64px;" /><col style="width:64px;" /><col style="width:64px;" /><col style="width:64px;" /><col style="width:64px;" /><col style="width:64px;" /><col style="width:64px;" /><col style="width:64px;" /><col style="width:64px;" /><col style="width:64px;" /><col style="width:64px;" /><col style="width:64px;" /></colgroup><tr style="background-color:#cacaca; text-align:center; font-weight:bold; font-size:8pt; "><td > </td><td >A</td><td >B</td><td >C</td><td >D</td><td >E</td><td >F</td><td >G</td><td >H</td><td >I</td><td >J</td><td >K</td><td >L</td><td >M</td><td >N</td><td >O</td><td >P</td><td >Q</td><td >R</td><td >S</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >1</td><td > </td><td style="text-align:right; ">2</td><td style="text-align:right; ">3</td><td style="text-align:right; ">4</td><td style="text-align:right; ">5</td><td style="text-align:right; ">6</td><td style="text-align:right; ">7</td><td style="text-align:right; ">44</td><td style="text-align:right; ">9</td><td style="text-align:right; ">12</td><td style="text-align:right; ">13</td><td style="text-align:right; ">88</td><td style="text-align:right; ">15</td><td style="text-align:right; ">16</td><td style="text-align:right; ">17</td><td style="text-align:right; ">18</td><td style="text-align:right; ">19</td><td style="text-align:right; ">20</td><td style="text-align:right; ">21</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >2</td><td style="text-align:right; ">2</td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >3</td><td style="text-align:right; ">3</td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >4</td><td style="text-align:right; ">4</td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >5</td><td style="text-align:right; ">5</td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >6</td><td style="text-align:right; ">6</td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >7</td><td style="text-align:right; ">7</td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >8</td><td style="text-align:right; ">8</td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >9</td><td style="text-align:right; ">9</td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >10</td><td style="text-align:right; ">10</td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >11</td><td style="text-align:right; ">11</td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >12</td><td style="text-align:right; ">12</td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >13</td><td style="text-align:right; ">13</td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >14</td><td style="text-align:right; ">14</td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >15</td><td style="text-align:right; ">15</td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >16</td><td style="text-align:right; ">16</td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >17</td><td style="text-align:right; ">17</td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >18</td><td style="text-align:right; ">18</td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >19</td><td style="text-align:right; ">19</td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >20</td><td style="text-align:right; ">20</td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >21</td><td style="text-align:right; ">21</td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td></tr></table> <br />Excel tables to the web - Excel Jeanie Html 4


After running macro.. (notice the values that were in Column H and L are now gone..)
<b>Sheet1</b><br /><br /><table border="1" cellspacing="0" cellpadding="0" style="font-family:Calibri,Arial; font-size:11pt; background-color:#ffffff; padding-left:2pt; padding-right:2pt; "> <colgroup><col style="font-weight:bold; width:30px; " /><col style="width:126px;" /><col style="width:64px;" /><col style="width:64px;" /><col style="width:64px;" /><col style="width:64px;" /><col style="width:64px;" /><col style="width:64px;" /><col style="width:64px;" /><col style="width:64px;" /><col style="width:64px;" /><col style="width:64px;" /><col style="width:64px;" /><col style="width:64px;" /><col style="width:64px;" /><col style="width:64px;" /><col style="width:64px;" /><col style="width:64px;" /></colgroup><tr style="background-color:#cacaca; text-align:center; font-weight:bold; font-size:8pt; "><td > </td><td >A</td><td >B</td><td >C</td><td >D</td><td >E</td><td >F</td><td >G</td><td >H</td><td >I</td><td >J</td><td >K</td><td >L</td><td >M</td><td >N</td><td >O</td><td >P</td><td >Q</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >1</td><td > </td><td style="text-align:right; ">2</td><td style="text-align:right; ">3</td><td style="text-align:right; ">4</td><td style="text-align:right; ">5</td><td style="text-align:right; ">6</td><td style="text-align:right; ">7</td><td style="text-align:right; ">9</td><td style="text-align:right; ">12</td><td style="text-align:right; ">13</td><td style="text-align:right; ">15</td><td style="text-align:right; ">16</td><td style="text-align:right; ">17</td><td style="text-align:right; ">18</td><td style="text-align:right; ">19</td><td style="text-align:right; ">20</td><td style="text-align:right; ">21</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >2</td><td style="text-align:right; ">2</td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >3</td><td style="text-align:right; ">3</td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >4</td><td style="text-align:right; ">4</td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >5</td><td style="text-align:right; ">5</td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >6</td><td style="text-align:right; ">6</td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >7</td><td style="text-align:right; ">7</td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >8</td><td style="text-align:right; ">8</td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >9</td><td style="text-align:right; ">9</td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >10</td><td style="text-align:right; ">10</td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >11</td><td style="text-align:right; ">11</td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >12</td><td style="text-align:right; ">12</td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >13</td><td style="text-align:right; ">13</td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >14</td><td style="text-align:right; ">14</td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >15</td><td style="text-align:right; ">15</td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >16</td><td style="text-align:right; ">16</td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >17</td><td style="text-align:right; ">17</td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >18</td><td style="text-align:right; ">18</td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >19</td><td style="text-align:right; ">19</td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >20</td><td style="text-align:right; ">20</td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >21</td><td style="text-align:right; ">21</td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td><td > </td></tr></table> <br />Excel tables to the web - Excel Jeanie Html 4
 
Upvote 0
Hi..

I added the part where it automatically inserts your value into Column A (shown in red).. just adjust the values and the number of values to suit..

Code:
Private Sub CommandButton1_Click()
    Dim rRngToDelete As Range, i, n As Long, Nums As Variant
[COLOR=#ff0000]    Nums = Array(2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21)[/COLOR]
[COLOR=#ff0000]    n = 0[/COLOR]
[COLOR=#ff0000]    For i = LBound(Nums) To UBound(Nums)[/COLOR]
[COLOR=#ff0000]        Sheets("Sheet1").Range("A2").Offset(n).Value = Nums(i)[/COLOR]
[COLOR=#ff0000]        n = n + 1[/COLOR]
[COLOR=#ff0000]    Next i[/COLOR]
    With CreateObject("scripting.dictionary")
        .CompareMode = 1
        For Each it In Columns(1).SpecialCells(2)
            x0 = .Item(it.Value)
        Next
        For Each j In Rows(1).SpecialCells(2)
            If Not .exists(j.Value) Then
                If rRngToDelete Is Nothing Then
                    Set rRngToDelete = Sheets("Sheet1").Range(j.Address)
                Else
                    Set rRngToDelete = Union(rRngToDelete, Sheets("Sheet1").Range(j.Address))
                End If
            End If
        Next j
    End With
    If Not rRngToDelete Is Nothing Then
        rRngToDelete.EntireColumn.Delete
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,231
Messages
6,170,885
Members
452,364
Latest member
springate

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top