Need Help with event

auto998

New Member
Joined
Aug 28, 2009
Messages
14
Hello,
I have been looking into this for awhile. I have a very basic knowledge with excel 2003.
I am trying to create a macro based on events. The event should be triggered as soon as the user leaves the cell by clicking on a different cell or by using the arrows on the keyboard. So if the user type SONY in cell A1, A2-A10 will be populated right on the spot automatically based on the word Sony, for example A2 will have electronic populated automatically, A3 will have <?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:eek:ffice:smarttags" /><st1:country-region w:st="on"><st1:place w:st="on">Japan</st1:place></st1:country-region>...and so on <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:eek:ffice:eek:ffice" /><o:p></o:p>
If the user enter GMC instead in cell A1, A2-A10 will be populated right on the spot automatically based on the word GMC. A2 will have Automaker populated automatically, A3 will have <st1:country-region w:st="on"><st1:place w:st="on">USA</st1:place></st1:country-region>, ...and so on.
After I created a small VB sample it looks like I need to press the execute button every time to run the program in VB instead of triggering an event automatically.
Can you please help? Here is a small sample I am using to test:
<o:p></o:p>
Sub test()<o:p></o:p>
<o:p></o:p>
<o:p></o:p>
If Range("a1").Value = 10 Then<o:p></o:p>
<o:p></o:p>
Range("c1").Value = "Yes"<o:p></o:p>
Else<o:p></o:p>
Range("c1").Value = "No"<o:p></o:p>
End If<o:p></o:p>
<o:p></o:p>
<o:p></o:p>
End Sub
 
Sometimes I have to move columns around, add or even delete columns. So the location for the column could change and reference could be tricky and involved for every change. What I have in mind is to assign each column to a variable, and in case I need to move things around it will be the variables for the columns names.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:eek:ffice:eek:ffice" /><o:p></o:p>
<o:p></o:p>
<o:p></o:p>
Also I am having (run-time error'13': Type mismatch) when I copy multiple cells from a different excel file in paste them in row 1<o:p></o:p>
Thanks again for the help<o:p></o:p>
 
Upvote 0

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
The only thing fixed in the code is the row (1). Everything else is done using offsets.

This will fix the error:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim c As Range
If Target.Row = 1 Then
    Application.EnableEvents = False
    For Each c In Target
        Select Case LCase(c.Value)
            Case "sony"
                c.Offset(1).Value = "electronic"
                c.Offset(2).Value = "Japan"
            Case "gmc"
                c.Offset(1).Value = "automaker"
                c.Offset(2).Value = "USA"
        End Select
    Next c
    Application.EnableEvents = True
End If
End Sub
 
Upvote 0
Thanks...I have another problem. When I copy multiple cells from a different excel file and paste them in column A, I get the result in one row ONLY. For example, if I past multiple cells all at once in A2, A3 and A4. The code will generate the results in F2 and G2. What I want to do is to past the result of A2 in (F2 and G2), A3 in (F3 and G3) and A4 in (F4 and G4) ...Any ideas? Thanks again for the help.
Here is a copy of the code:

Code:
Function ColumnLetter(ColumnNumber As Integer) As String
  If ColumnNumber > 26 Then

    ' 1st character:  Subtract 1 to map the characters to 0-25,
    '                 but you don't have to remap back to 1-26
    '                 after the 'Int' operation since columns
    '                 1-26 have no prefix letter

    ' 2nd character:  Subtract 1 to map the characters to 0-25,
    '                 but then must remap back to 1-26 after
    '                 the 'Mod' operation by adding 1 back in
    '                 (included in the '65')

    ColumnLetter = Chr(Int((ColumnNumber - 1) / 26) + 64) & _
                   Chr(((ColumnNumber - 1) Mod 26) + 65)
  Else
    ' Columns A-Z
    ColumnLetter = Chr(ColumnNumber + 64)
  End If
End Function

Private Sub Worksheet_Change(ByVal Target As Range)
Dim c As Range
Dim brand As Integer
Dim Mis As Integer
Dim dept As Integer
Dim made As Integer
Dim extra As Integer
Dim NextCell As String


brand = 1   'brand column
Mis = 2     'Mis column
dept = 6    'dept column
made = 7    'made column
extra = 5   'extra column

ColumnLetter (Target.Column)
NextCell = ActiveCell.Address

If Target.Column = brand Then
    Application.EnableEvents = False
    For Each c In Target
        Select Case LCase(c.Value)
            Case "sony"
                Range(ColumnLetter(dept) & Target.Row).Select
                ActiveCell.FormulaR1C1 = "electronic"
                Range(ColumnLetter(made) & Target.Row).Select
                ActiveCell.FormulaR1C1 = "Japan"
            Case "gmc"
                Range(ColumnLetter(dept) & Target.Row).Select
                ActiveCell.FormulaR1C1 = "automaker"
                Range(ColumnLetter(made) & Target.Row).Select
                ActiveCell.FormulaR1C1 = "USA"
        End Select
    Next c
    Application.EnableEvents = True
    Range(NextCell).Select
End If
End Sub
 
Upvote 0
Hello again, I see the problem is Target.Row, it is not incrementing when I paste multiple cell<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:eek:ffice:eek:ffice" /><o:p></o:p>
 
Upvote 0

Forum statistics

Threads
1,216,098
Messages
6,128,812
Members
449,468
Latest member
AGreen17

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