+ Reply to Thread
Results 1 to 2 of 2

If statements and VBA improvements

  1. #1
    Registered User
    Join Date
    11-22-2016
    Location
    Hamburg
    MS-Off Ver
    2013
    Posts
    1

    If statements and VBA improvements

    I'm new to VBA and need your assistance !

    I have the following macro that fetches specific column data from one workbook to another. In short: VBA allows you to select the location of the sournce file "ImportFrom", opens it, selects specific columns and pastes them in to pre-defined columns in the target workbook: "PasteTo".

    Actual code is attached as txt file.


    The copy/paste part works just fine, however I'm having problems with the "IF" statement towards the end. The data source I use has a lot of values that come up as (Null) instead of 0. I want to tell excel to change the string (Null) to the number 0, for every cell in column N. I tried using "IF" and "Like" functions but with no luck.

    For Each cell In Range("N2:N1500")
    If cell.Value Like "*(Null)*" Then
    cell.Value = "0"
    Else
    Exit Sub



    Another thing I would like to improve is the copy/paste ranges. Intead of having copy Range(J1:J1500) and want excel to use dynamic ranges; i.e. take Range(J1:to whatever the last used cell in this columns is).

    This was my initial idea:

    wsImportFrom.Range("A2", Cells(.Rows.Count, .Columns.Count)).Copy
    wsPasteTo.Range("J2", Cells(.Rows.Count, .Columns.Count)).PasteSpecial Paste:=xlPasteValues, _
    operation:=xlPasteSpecialOperationNone, SkipBlanks:=False, Transpose:=False _


    In addition, I want to make the code more dynamic and tell VBA to perform copy/paste on the basis of column headers, rather than pre-defined ranges (column J to E and so forth). For instance: I have a column called "Sales" in the source file (ImportFrom) and I want excel to take all entries and paste them in "Sales" located in the destination file (PasteTo).

    This is the code I use in another file, which does exactly that:

    Sheets("BIR").Select

    desc = WorksheetFunction.Match("Contract num", Rows("1:1"), 0)
    qty = WorksheetFunction.Match("Quantity", Rows("1:1"), 0)

    Sheets("BIR").Columns(desc).Copy Destination:=Sheets("DATA").Range("A1")
    Sheets("BIR").Columns(qty).Copy Destination:=Sheets("DATA").Range("B1")

    On this instance I copy and paste data that is located in the same workbook. However, I don't know how to tell excel - take data from whatever file is selected via the pop-up window; open it; search for column "Sales"; copy all entries' now find column "Sales" in the destination workbook and paste values there.


    I know this is a lot to ask for, but any help will be most appreciated !
    Attached Files Attached Files
    Last edited by gelen4o; 11-22-2016 at 07:01 AM.

  2. #2
    Forum Expert
    Join Date
    10-09-2014
    Location
    Newcastle, England
    MS-Off Ver
    2003 & 2013
    Posts
    1,986

    Re: If statements and VBA improvements

    Part 1
    Use ISNULL to check

    Please Login or Register  to view this content.
    If someone has helped you then please add to their Reputation

+ 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. Cost and Budget Improvements KPI's
    By alsalms3 in forum Excel General
    Replies: 3
    Last Post: 01-16-2013, 01:07 AM
  2. Improvements to ExcelForum.com
    By Phoenix5794 in forum Suggestions for Improvement
    Replies: 1
    Last Post: 07-12-2012, 11:02 AM
  3. Help with Drop Down Menu Improvements
    By Chance0066 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-14-2012, 04:10 PM
  4. Risk Log - Newbie improvements
    By garethdjohn in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-16-2012, 11:03 AM
  5. Performance Improvements while using RTD
    By [email protected] in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 12-19-2005, 08:15 AM
  6. Message Box Improvements
    By Phil Hageman in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-17-2005, 01:05 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