+ Reply to Thread
Results 1 to 5 of 5

Using Arrays With IF-THEN Statements in VBA

  1. #1
    Registered User
    Join Date
    02-03-2011
    Location
    Pennsylvania
    MS-Off Ver
    Excel 2003
    Posts
    4

    Using Arrays With IF-THEN Statements in VBA

    Hello,

    I'm trying to write a macro to copy a cell if the first three integers in the adjacent cell are present in an array. I'm brand new to the concept of arrays, and having a difficult time trying to figure out how to proceed. When I run the code below, I get a Type Mismatch error. I've uploaded an example of the worksheet I'm using.

    Sub mcrFinancialCodeTemplates1()

    Dim aryAMPTH() As Integer
    Dim aryBUs As Integer
    Dim i As Integer

    aryBUs = "180, 181, 182, 184, 185, 751, 752, 753, 754, 755, 757, 758, 759, 762, 765, 766, 771, 773, 774, 776, 777, 810, 811, 813, 817, 818, 818, 819, 820, 822, 825, 831, 833, 834, 835, 838, 839, 950, 951, 952, 953, 954, 955, 956, 957, 959, 960, 962, 963, 964, 965, 967, 968, 969, 970, 971, 974, 975, 976, 977"
    aryAMPTH = Split(aryBUs, ", ")

    For i = LBound(aryAMPTH) To UBound(aryAMPTH)

    Sheets("Main").Select
    Range("A1").Activate
    Do While Not IsEmpty(ActiveCell.Value)
    If (Left(ActiveCell, 3) = aryAMPTH) Then
    ActiveCell.Offset(0, 1).Copy
    Sheets("Sheet1").Select
    ActiveSheet.Paste
    ActiveCell.Offset(0, -2) = "AMPTH"

    Any help would be appreciated!
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Using Arrays With IF-THEN Statements in VBA

    Please use code tags with your code as per forum's rule.
    There are a couple of issue with your code.

    Unless you are going to assign a range in to array, the way you have done is wrong. All values with the array must be put in parenthesis.

    I have done few of them for you, but you need to do the rest. I have also corrected some of the errors, but you will encounter further errors.
    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    02-03-2011
    Location
    Pennsylvania
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: Using Arrays With IF-THEN Statements in VBA

    Thank you. I added the quotation marks, and am still getting the type mismatch error.

    Please Login or Register  to view this content.

  4. #4
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Using Arrays With IF-THEN Statements in VBA

    Change this line

    Dim aryBUs As Integer

    In to

    Please Login or Register  to view this content.

  5. #5
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Using Arrays With IF-THEN Statements in VBA

    You don't need "" around every item, keep it as a string like it was in your first post.

    This will compile, but it won't work - think it might send you into an infinite loop.

    The reason it doesn't work is that you never advance the active cell.
    Please Login or Register  to view this content.
    What is it you actually want the code to do?

    This code will move the values from column B to Sheet2 when the corresponding value in A matches one of the values in the array.
    Please Login or Register  to view this content.
    If posting code please use code tags, see here.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. If statements, arrays or VB macro
    By The Kiwi in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-26-2012, 07:47 PM
  2. manipulating ranges/arrays with if/then statements?
    By losmi8 in forum Excel Programming / VBA / Macros
    Replies: 27
    Last Post: 11-24-2009, 03:57 AM
  3. [SOLVED] If Statements and Arrays
    By Currie in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 09-06-2005, 03:05 AM
  4. [SOLVED] RE: If Statements and Arrays
    By Peo Sjoblom in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 05-27-2005, 05:05 PM
  5. [SOLVED] EXCEL ARRAYS & IF STATEMENTS
    By Dan in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 05-24-2005, 02:06 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1