-1

I want to run my code when add-in loads. (Open excel file -> load addin -> run code)

But I have a problem with event handler.

I don't know what kind of event handler I need use and how to load installed addin.

(I try to use Workbook_open handler but I think it's wrong)

And I try to use Workbook_AddinInstall() event handler then when the install add-in, it works. But to make work my code, I need to reinstall add-in every time.

And How can I run add-in which already in the add-ins list.

Here is my code,

Private Sub Workbook_AddinInstall()
Dim counter As Long
Dim rowSize As Long
Dim userId As String
Dim answers As String
Dim vals As String

Dim i As Integer

Set currentSheet = ActiveWorkbook.ActiveSheet

MsgBox (currentSheet.Cells(1, 2).Value)

rowSize = currentSheet.Rows.Count
counter = 1


'Create Column

currentSheet.Cells(1, 7).Value = "Country"
currentSheet.Cells(1, 8).Value = "State"
currentSheet.Cells(1, 9).Value = "Age"

currentSheet.Cells(1, 7).Font.Bold = True
currentSheet.Cells(1, 8).Font.Bold = True
currentSheet.Cells(1, 9).Font.Bold = True

currentSheet.Cells(1, 7).HorizontalAlignment = xlCenter
currentSheet.Cells(1, 8).HorizontalAlignment = xlCenter
currentSheet.Cells(1, 9).HorizontalAlignment = xlCenter

currentSheet.Cells(1, 7).Borders().LineStyle = xlContinuous
currentSheet.Cells(1, 8).Borders().LineStyle = xlContinuous
currentSheet.Cells(1, 9).Borders().LineStyle = xlContinuous

'Set Value
Do While counter < rowSize

    If currentSheet.Cells(counter, 1).Value = Null Then Exit Do
    If currentSheet.Cells(counter, 4).Value = "3" Then

        userId = currentSheet.Cells(counter, 2).Value
        vals = currentSheet.Cells(counter, 6).Value
        'MsgBox (vals)

        temp = Split(vals, ",")
        i = 0

        Do While i < 10
            targetCell = counter + i
            If currentSheet.Cells(targetCell, 2).Value = userId Then
               currentSheet.Cells(targetCell, 7).Value = temp(0)
               currentSheet.Cells(targetCell, 8).Value = temp(1)
               currentSheet.Cells(targetCell, 9).Value = temp(2)

               currentSheet.Cells(targetCell, 7).HorizontalAlignment = xlCenter
               currentSheet.Cells(targetCell, 8).HorizontalAlignment = xlCenter
               currentSheet.Cells(targetCell, 9).HorizontalAlignment = xlCenter

               currentSheet.Cells(targetCell, 7).Borders().LineStyle = xlContinuous
               currentSheet.Cells(targetCell, 8).Borders().LineStyle = xlContinuous
               currentSheet.Cells(targetCell, 9).Borders().LineStyle = xlContinuous
            End If
            i = i + 1
        Loop
        temp = Null
       'parsing_question_1(vals, userId)
    End If

    counter = counter + 1
Loop

End Sub

Thank you.

JimmyPena
  • 8,694
  • 6
  • 43
  • 64
Expert wanna be
  • 10,218
  • 26
  • 105
  • 158
  • Why do you think the Workbook_Open isn't right? Here's a resource that indicates that IS where it should go: http://www.cpearson.com/excel/createaddin.aspx – Richard Morgan Sep 08 '11 at 17:03
  • Because, I don't want to run mycode on the other excel file. I need run vba code on specific excel file, not for the other. But using Workbook_Open then, I will effect to all of excel file when I open. – Expert wanna be Sep 08 '11 at 17:08
  • You've asked two questions with identical titles. Please choose a more descriptive title for your questions. – Jean-François Corbett Sep 08 '11 at 18:21

1 Answers1

0

Create the event in the addin:

www.cpearson.com/excel/appevent.htm

Also, if you wanted you could do the workbookopen event and just check to make sure the workbook matches the workbook name, or create a hidden worksheet that has a value in cell A1 that tells you if it is your workbook.

You can also uninstall the add-in every time your workbook closes (using the events again), if that is the route you prefer.

Jon49
  • 4,444
  • 4
  • 36
  • 73