+ Reply to Thread
Results 1 to 7 of 7

VBA to insert IF statement

  1. #1
    Registered User
    Join Date
    05-18-2014
    MS-Off Ver
    Excel 2003
    Posts
    7

    VBA to insert IF statement

    Hi there,

    I have a worksheet that contains data arranged in fields from columns A to J. The relevant columns for the purposes of the macro are columns B (customer codes) and G (sales values). Column B may contain a single instance of a customer code, or multiple (over 50), depending on how active a customer has been. The worksheet always contains many different customer codes with varying numbers of rows for each customer, sorted by customer. What I'm trying to do is write a macro that will loop through the worksheet and insert a blank line immediately after a change in value of column B (customer code), and in this blank line, insert the customer code in column B (which comes from the cell immediately above), a sum formula for all values within a range that relate to that particular customer in column G, and an IF statement in column J that relates to the SUM formula. The loop concludes when there are no longer values in column B.

    I have code that allows me to insert blank lines on change in values in column B and the SUM formula, but the IF statement is a problem.

    Please Login or Register  to view this content.
    My IF statement that references the SUM formula in column G is place in the same row in column J. An example is:

    Please Login or Register  to view this content.
    Thanks very much for any help.
    Peter

  2. #2
    Registered User
    Join Date
    05-18-2014
    MS-Off Ver
    Excel 2003
    Posts
    7

    Re: VBA to insert IF statement

    Here's a copy of the spreadsheet I'm working with.
    Attached Files Attached Files

  3. #3
    Forum Contributor Gregor y's Avatar
    Join Date
    10-24-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2010 32-Bit
    Posts
    276

    Re: VBA to insert IF statement

    why not just add the following below the lines of code that start with: Cells(dRow, 7).FormulaR1C1
    Please Login or Register  to view this content.
    also, depending on how accurate your if statments need to be you may want to consider
    Please Login or Register  to view this content.

  4. #4
    Registered User
    Join Date
    05-18-2014
    MS-Off Ver
    Excel 2003
    Posts
    7

    Re: VBA to insert IF statement

    I've got a working version of the macro in a version of the attached spreadsheet which operates perfectly, using a formula very similar to the code you posted. However, when I copy the code and paste it into a module for a different spreadsheet (with exactly the same structure/arrangement of columns), it inserts the IF statement into every single line, not just the blank lines created by the macro. I've really no idea what the problem is.

    It works fine with the sample worksheet, but in a different worksheet, with the same structure, it fills every row in column J with IF statements.

    Any ideas would be greatly appreciated.
    Peter

  5. #5
    Registered User
    Join Date
    05-18-2014
    MS-Off Ver
    Excel 2003
    Posts
    7

    Re: VBA to insert IF statement

    I've tested the macro on data formatted as a table and it fails, while it works perfectly on plain data without any formatting. So, perhaps this is the problem. When I copy all the data into another worksheet in the same file but Paste Special > Values, it drops the formatting and consequently the macro works fine.
    I've not encountered this problem before.

  6. #6
    Forum Contributor Gregor y's Avatar
    Join Date
    10-24-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2010 32-Bit
    Posts
    276

    Re: VBA to insert IF statement

    Sorry my clairvoyance can only go so far, and eventually a mechanic must see the car they’re trying to repair instead of just knowing it’s a Ford; however to just take a blind shot in the dark I’d guess that you probably need to add something similar to this at the beginning of your sub.
    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    05-18-2014
    MS-Off Ver
    Excel 2003
    Posts
    7

    Re: VBA to insert IF statement

    You would make an expert clairvoyant mechanic. Works a treat.

+ 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. [SOLVED] Insert image with IF statement
    By BRISBANEBOB in forum Excel Formulas & Functions
    Replies: 16
    Last Post: 06-13-2017, 05:57 PM
  2. [SOLVED] INSERT YES/NO value with INSERT Statement
    By jonboy6257 in forum Access Tables & Databases
    Replies: 1
    Last Post: 01-27-2014, 12:38 PM
  3. Insert Sound In a If Statement
    By rami10 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-05-2013, 12:29 PM
  4. Insert Into Statement
    By Indikirt in forum Access Tables & Databases
    Replies: 1
    Last Post: 12-13-2010, 05:27 PM

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