+ Reply to Thread
Results 1 to 11 of 11

Referencing a String variable format

  1. #1
    Forum Contributor
    Join Date
    02-20-2017
    Location
    Indiana
    MS-Off Ver
    2016
    Posts
    101

    Referencing a String variable format

    Hello,

    I'm trying to Select column M if my String = AETD or FBR, but the macro always uses the Else statement even when the variable is = AETD or FBR.
    This leads me to believe that there is a formatting issue with the first line in the if statement. Anyone know what's wrong?

    Please Login or Register  to view this content.

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

    Re: Referencing a String variable format

    The expression in the If statement looks fine to me, are you sure there are no trailing/leading spaces in the cell(s) you are trying to check, or some other problem with the data?
    If posting code please use code tags, see here.

  3. #3
    Forum Contributor
    Join Date
    02-20-2017
    Location
    Indiana
    MS-Off Ver
    2016
    Posts
    101

    Re: Referencing a String variable format

    Cell A1 is formatted as a text cell, meaning there is a ' in front of the cell value. Also A1 has conditional formatting so that you can select the value from a drop down list. Would any of these cause the code to not recognize the value?

    Also worth mentioning that the MsgBox returns AETD or FBR with no ' before the value, and the if statement still doesn't run correctly.

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

    Re: Referencing a String variable format

    Any chance you could upload a sample workbook?

    Click on GO ADVANCED, scroll down and click Manage Attachments.

  5. #5
    Forum Contributor
    Join Date
    02-20-2017
    Location
    Indiana
    MS-Off Ver
    2016
    Posts
    101

    Re: Referencing a String variable format

    The Worksheet in Basis(1) file is the raw data. The VBA is all in the .xlsm file > FormattingMacro module. Use Format and Save button to test. 'it wont actually save
    Attached Files Attached Files

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

    Re: Referencing a String variable format

    When I step through the code it's Columns("M:M").Select that's highlighted in this section not the Else part, Columns("H:H").Select.
    Please Login or Register  to view this content.
    That indicates that the expression in the If statement has evaluated to True, ie Program = 'AETD' or 'FBR'.

  7. #7
    Forum Contributor
    Join Date
    02-20-2017
    Location
    Indiana
    MS-Off Ver
    2016
    Posts
    101

    Re: Referencing a String variable format

    Interesting. If the code is selecting Column M, why is it pasting Column H into the new sheet?

    EDIT: Actually it's pasting Column I into the new sheet.
    Last edited by BryceVBA; 03-23-2017 at 01:07 PM.

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

    Re: Referencing a String variable format

    What happens if you change the code to this?
    Please Login or Register  to view this content.
    That explicitly states which column to copy (and which sheet/workbook it's in too) so you aren't relying on Selection.

  9. #9
    Forum Contributor
    Join Date
    02-20-2017
    Location
    Indiana
    MS-Off Ver
    2016
    Posts
    101

    Re: Referencing a String variable format

    Still paste column I into sheet2

    Please Login or Register  to view this content.

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

    Re: Referencing a String variable format

    Which column from which worksheet in which workbook do you want to paste into a new worksheet?

    Also, which workbook are you adding the new sheet to?

    PS I'm assuming you are working with 2 workbooks.

  11. #11
    Forum Contributor
    Join Date
    02-20-2017
    Location
    Indiana
    MS-Off Ver
    2016
    Posts
    101

    Re: Referencing a String variable format

    Except for determining variables, the whole macro is ran in the raw data wb (Worksheet in Basis(1)). I'm trying to paste column M in sheet1 to column A of Sheet2 (the macro inserts sheet2)

+ 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] How to extract a piece of a string variable, and assign to a new variable
    By jmccoughlin in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-04-2015, 11:50 PM
  2. [SOLVED] Use variable in cell format string
    By Tsjallie in forum Excel General
    Replies: 2
    Last Post: 03-11-2015, 10:01 AM
  3. Replies: 3
    Last Post: 02-17-2015, 11:53 PM
  4. referencing a string variable as a sheet name
    By prjames3 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-16-2013, 02:43 PM
  5. referencing a string variable as a sheet name
    By prjames3 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-16-2013, 02:09 PM
  6. Replies: 4
    Last Post: 07-12-2013, 12:14 PM
  7. Transferring a dynamically named string variable to another string variable.
    By Concorde Rules in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-09-2012, 04:08 AM

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