How Do I Insert a Subroutine in Excel 2007?

klicks

New Member
Joined
Sep 15, 2015
Messages
8
Hi,

This might be a dumb question... but I need help on how and where to insert the following subroutine in my excel 2007 worksheet. Exact instructions on how to insert the code and how to run it.

Thank you very much for any help.
Ted

Sub klicks()
Dim S1 As Worksheet, S2 As Worksheet, V1 As Variant, V2 As Variant, V3() As Variant
Dim R1 As Range, R2 As Range, d As Object
Dim i As Long, ct As Long, n As Variant
Set S1 = Sheets("Sheet1")
Set S2 = Sheets("Sheet2")
Set R1 = S1.Range("A2:B" & S1.Cells(Rows.Count, "A").End(xlUp).Row)
V1 = R1.Value
Set R2 = S2.Range("A2:B" & S2.Cells(Rows.Count, "A").End(xlUp).Row)
V2 = R2.Value
ReDim V3(1 To UBound(V1, 1) + UBound(V2, 1), 1 To 3)
Set d = CreateObject("Scripting.dictionary")
For i = 1 To UBound(V1, 1)
If Not d.exists(V1(i, 1)) Then
ct = ct + 1
d.Add V1(i, 1), ct
n = Application.CountIf(R2.Columns(1), V1(i, 1))
If n = 0 Then
V3(ct, 1) = V1(i, 1)
V3(ct, 2) = V1(i, 2)
V3(ct, 3) = ""
Else
V3(ct, 1) = V1(i, 1)
V3(ct, 2) = V1(i, 2)
V3(ct, 3) = V2(Application.Match(V1(i, 1), R2.Columns(1), 0), 2)
End If
End If
Next i
For i = 1 To UBound(V2, 1)
If Not d.exists(V2(i, 1)) Then
ct = ct + 1
d.Add V2(i, 1), ct
n = Application.CountIf(R1.Columns(1), V2(i, 1))
If n = 0 Then
V3(ct, 1) = V2(i, 1)
V3(ct, 2) = ""
V3(ct, 3) = V2(i, 2)
Else
V3(ct, 1) = V2(i, 1)
V3(ct, 2) = V2(i, 2)
V3(ct, 3) = V1(Application.Match(V2(i, 1), R1.Columns(1), 0), 2)
End If
End If
Next i
Application.ScreenUpdating = False
S1.Range("F1:H1").Value = Array("Item No.", "No.", "No.")
S1.Range("F2:H" & ct + 1).Value = V3
S1.Columns("F").NumberFormat = "0"
S1.Columns("F:H").AutoFit
Application.ScreenUpdating = True
End Sub
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
OK. first make a copy of your working spreadsheet.

no, really, stop and make a copy so you don't cry when all goes wrong..

Good!

Open your spreadsheet you want to use this macro in..
Once opened, press Alt+F11 to open the Visual Basic Editor.
On the menu bar at the top , click on Insert > Module
you will see a Module1 added to the workbook (pane on left side)
on the right side you now have a white screen with the cursor blinking on it.
Paste your code here.

.Go back to the spreadsheet.
press Alt+F8 to open the Macro window.
Select your macro (named) "klicks" then click the RUN button.

If all goes well, you won't need the backup copy.
If it doesn't go well, you can restore it by making a copy of the backup you created before you started.
You did make a backup, didn't you?
Then call the person who gave you the macro. If you got the code into the module and it did something wrong while running then call the person who wrote the code.
Only they will know exactly what is happening with this chunk of code.

good luck

 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,587
Messages
6,179,740
Members
452,940
Latest member
Lawrenceiow

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