+ Reply to Thread
Results 1 to 23 of 23

My VBA code stopped working properly

  1. #1
    Registered User
    Join Date
    12-16-2016
    Location
    New Mexico
    MS-Off Ver
    2007
    Posts
    36

    My VBA code stopped working properly

    Something went wrong with my code it only does the first column, but when i first tested this it did all columns that had data in them.
    Please Login or Register  to view this content.

  2. #2
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,015

    Re: My VBA code stopped working properly

    .
    Except for adding a couple of DIM statements, the code works.

    Having it check to row 25500 on each sheet takes a L O N G time ...

    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    12-16-2016
    Location
    New Mexico
    MS-Off Ver
    2007
    Posts
    36

    Re: My VBA code stopped working properly

    What is that option explicit? it goes into the previous sub that I have.
    I don't know where to put that.

    EDIT: This also never runs that many rows that is just a safeguard because i want it to do multiple columns and have 99 stop it on each column. but it just stops on first 99 check all together and doesn't loop to check for more used ranges
    Last edited by Dark0Prince; 02-02-2017 at 10:37 AM.

  4. #4
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,015

    Re: My VBA code stopped working properly

    .
    "Option Explicit" is a setting you can enable in Excel to automatically be placed at the top of every module / macro you use. It forces you to use proper coding / syntax as it does a 'syntax check' kinda like
    auto-spelling. If you've entered a line of code that is incorrect, Option Explicit will highlight the error and provide an error code from which you might be able to discern an acceptable replacement syntax.

    You can leave the comment out if you desire. Doing so may make it more difficult for you to determine where an error lies in your code, especially if you are just beginning to code.

    Option Explicit always goes at the top of any module /macro ... above the statement Sub 'your sub name' ()

    EDIT: This also never runs that many rows that is just a safeguard because i want it to do multiple columns and have 99 stop it on each column. but it just stops on first 99 check all together and doesn't loop to check for more used ranges
    It will still search the entire range if there is data therein or not. Therefore, it will take alot longer to run if the range is set to 25500. Suggestion: if you will never be reviewing more than 99 rows, why not set the number to 100 or 150 ? as a safeguard.

  5. #5
    Registered User
    Join Date
    12-16-2016
    Location
    New Mexico
    MS-Off Ver
    2007
    Posts
    36

    Re: My VBA code stopped working properly

    Quote Originally Posted by Logit View Post
    .
    It will still search the entire range if there is data therein or not. Therefore, it will take alot longer to run if the range is set to 25500. Suggestion: if you will never be reviewing more than 99 rows, why not set the number to 100 or 150 ? as a safeguard.
    It still only checks the first column it doesn't seem to check anything other then column A when i want it to keep checking all columns with data. I can easily change that 25000 to something like 2000 I just didn't fix it yet.

  6. #6
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,015

    Re: My VBA code stopped working properly

    ,
    This is the result from running the macro here. Is this the result you are expecting ?

    99.jpg
    Attached Files Attached Files

  7. #7
    Registered User
    Join Date
    12-16-2016
    Location
    New Mexico
    MS-Off Ver
    2007
    Posts
    36

    Re: My VBA code stopped working properly

    Yes that is the result but my sheet has 99 at the end of each column so it doesn't keep going.

  8. #8
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,015

    Re: My VBA code stopped working properly

    .
    Try this code as replacement for the other. Then run it. You will see that it is checking all the columns.

    Enter 99 at the bottom of each column first. Say, A30, B30, C30 .... R30, S30, T30.

    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    12-16-2016
    Location
    New Mexico
    MS-Off Ver
    2007
    Posts
    36

    Re: My VBA code stopped working properly

    Quote Originally Posted by Logit View Post
    .
    Try this code as replacement for the other. Then run it. You will see that it is checking all the columns.

    Enter 99 at the bottom of each column first. Say, A30, B30, C30 .... R30, S30, T30.

    Please Login or Register  to view this content.

    mind just checks the first column and i'm using the same code as you

  10. #10
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,015

    Re: My VBA code stopped working properly

    .
    There is something else going on in your project or with your computer then.

    Here is the completed project ...
    Attached Files Attached Files

  11. #11
    Registered User
    Join Date
    12-16-2016
    Location
    New Mexico
    MS-Off Ver
    2007
    Posts
    36
    Quote Originally Posted by Logit View Post
    .
    There is something else going on in your project or with your computer then.

    Here is the completed project ...
    Yours works when I download it or point my button on my work sheet to your code but I'm putting this in a personal work book for later use and it didn't work after I copy and paste.

  12. #12
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,015

    Re: My VBA code stopped working properly

    Please post a copy of your personal workbook - without confidential or private data.

    Show or explain where you posted the code in your workbook. If there were errors, indicate what the error / s was / were and
    if the code was highlighted by the error, where in the code the highlight occurred.

    Give us an opportunity to trouble shoot this for you.

  13. #13
    Registered User
    Join Date
    12-16-2016
    Location
    New Mexico
    MS-Off Ver
    2007
    Posts
    36

    Re: My VBA code stopped working properly

    I know what the problem is but don't know how to fix it. It's because of where I'm putting it. When i put this code in Sheet1 it works fine, but when I put it in personal workbook it doesn't work fine. Even if I put in personal workbook(Sheet1) area. There is no other code to show you in my personal workbook it just doesn't work when i put it in there.

  14. #14
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,015

    Re: My VBA code stopped working properly

    What is the name of the sheet you are attempting to insert the code ?
    Look at the red Sheet1 name below ...

    Option Explicit

    Sub practice()
    Dim k, i As Integer
    Dim val As String
    val = ""

    For k = 1 To Sheet1.UsedRange.Columns.Count '<--- Change sheet name to match

    For i = 2 To 25
    If Cells(i, k).Value = "" Then
    Cells(i, k).Value = val
    ElseIf Cells(i, k).Value = "99" Or Cells(i, k).Value = 99 Then
    Exit For
    Else
    val = Cells(i, k).Value
    End If
    Next i
    Next k

    MsgBox ("all done!")

    End Sub

    When code has Sheet1 ... or Sheet2 ... in it, that is referring to the order of the worksheets. When you open a new workbook (nothing has been done to it yet), it will have Sheet1 / Sheet2 / Sheet3 to start with.
    Those sheets are in the order of 1 - 2 - 3. Sheet1 (red word above) is referring to the first sheet on the left. For example: if the tabs were in the order of Sheet2 / Sheet1 / Sheet3 ..... as far as Excel is concerned, it see's
    Sheet2 as the first sheet in the three because that is the order they are being displayed in the workbook. So ... in code ... in this specific case, Sheet1 is referring to the FIRST SHEET in the three, which is actually Sheet2.

    (I tried to explain that in such a way that is clear. Hope it didn't confuse you).

    Now, having said all of that ... let's do this.

    Looking at the tabs from left to right ... what are the names ? It is important they be read left to right.

    Let me give you an example: The left most tab is named "My Other Sheet" ... the middle tab is named "My First Sheet" ... the right most tab is named "Receipts"

    Excel 'reads' these tabs as : My Other Sheet = Sheet1 ........ My First Sheet = Sheet2 ......... Receipts = Sheet3.

    In a coded macro these sheets would be referred to as : Sheets("My Other Sheet") ..... Sheets("My First Sheet") ..... Sheets("Receipts")

    Using that example as a guide, in the code, change the red SHEET1 to the appropriate order number of the sheet you are pasting the code.
    Last edited by Logit; 02-03-2017 at 10:02 PM.

  15. #15
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,015

    Re: My VBA code stopped working properly

    Deleted duplicate post.

  16. #16
    Registered User
    Join Date
    12-16-2016
    Location
    New Mexico
    MS-Off Ver
    2007
    Posts
    36
    Quote Originally Posted by Logit View Post
    What is the name of the sheet you are attempting to insert the code ?
    Look at the red Sheet1 name below ...

    Option Explicit

    Sub practice()
    Dim k, i As Integer
    Dim val As String
    val = ""

    For k = 1 To Sheet1.UsedRange.Columns.Count '<--- Change sheet name to match

    For i = 2 To 25
    If Cells(i, k).Value = "" Then
    Cells(i, k).Value = val
    ElseIf Cells(i, k).Value = "99" Or Cells(i, k).Value = 99 Then
    Exit For
    Else
    val = Cells(i, k).Value
    End If
    Next i
    Next k

    MsgBox ("all done!")

    End Sub

    When code has Sheet1 ... or Sheet2 ... in it, that is referring to the order of the worksheets. When you open a new workbook (nothing has been done to it yet), it will have Sheet1 / Sheet2 / Sheet3 to start with.
    Those sheets are in the order of 1 - 2 - 3. Sheet1 (red word above) is referring to the first sheet on the left. For example: if the tabs were in the order of Sheet2 / Sheet1 / Sheet3 ..... as far as Excel is concerned, it see's
    Sheet2 as the first sheet in the three because that is the order they are being displayed in the workbook. So ... in code ... in this specific case, Sheet1 is referring to the FIRST SHEET in the three, which is actually Sheet2.

    (I tried to explain that in such a way that is clear. Hope it didn't confuse you).

    Now, having said all of that ... let's do this.

    Looking at the tabs from left to right ... what are the names ? It is important they be read left to right.

    Let me give you an example: The left most tab is named "My Other Sheet" ... the middle tab is named "My First Sheet" ... the right most tab is named "Receipts"

    Excel 'reads' these tabs as : My Other Sheet = Sheet1 ........ My First Sheet = Sheet2 ......... Receipts = Sheet3.

    In a coded macro these sheets would be referred to as : Sheets("My Other Sheet") ..... Sheets("My First Sheet") ..... Sheets("Receipts")

    Using that example as a guide, in the code, change the red SHEET1 to the appropriate order number of the sheet you are pasting the code.
    I'm posting my code in the personal workbook on sheet1 so I can use this code no matter what I open. so after I post it there I create a new workbook to test and it only does the first column. It's easy to duplicate what I'm doing I've gone to multiple computers and it's the same result. I can't save code on just that workbook because that defeats it's purpose.

    On a side note I don't even use the second or third sheets to test because I know they won't work.

  17. #17
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,015

    Re: My VBA code stopped working properly

    I believe you are wanting the macro code to do something other than what it is designed to do. However, it is confusing to me what the goal is.

    The information in your posts, as I understand it, resulted in the macro provided.

    If there is another way of describing the goal, it may make a difference. Or perhaps a fresh set of eyes on this thread would also assist.

    This is not an attempt to question your or my abilities .... it's just that the written word doesn't always convey the intended message. You know what
    your goal is - you can see it in front of you - but alas, I can't. So a different way of describing what needs to be done is in order.

  18. #18
    Forum Contributor
    Join Date
    08-09-2012
    Location
    British Columbia
    MS-Off Ver
    Excel 2010
    Posts
    189

    Re: My VBA code stopped working properly

    I'm putting this in a personal work book
    When code has Sheet1 ... or Sheet2 ... in it, that is referring to the order of the worksheets.
    actually those are the sheets default Code Names, the order would be Sheet(1), Sheet(2), etc.
    you can't use a sheets code name when referring to it in another workbook.

    Try putting this into a standard module in your "personal work book". Change the workbook and sheet name to what you're actually working on.
    Please Login or Register  to view this content.

    @ Logit
    Have a look at this page, down about half way, there's a heading
    Pay Attention To Variables Declared With One Dim Statement

  19. #19
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,624

    Re: My VBA code stopped working properly

    Don't quote whole posts -- it's just clutter. If you are responding to a post out of sequence, limit quoted content to a few relevant lines that makes clear to whom and what you are responding

    For normal conversational replies, try using the QUICK REPLY box below.
    Ben Van Johnson

  20. #20
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,015

    Re: My VBA code stopped working properly

    Hey NoSparks ... good to 'see' you again.

    Thanks for the heads up. Still learning here.
    Bad habit picked up from someone else who isn't aware either.

    Good information.

  21. #21
    Registered User
    Join Date
    12-16-2016
    Location
    New Mexico
    MS-Off Ver
    2007
    Posts
    36

    Re: My VBA code stopped working properly

    Why do i need to refer this to anything other then activesheet. That code does work if i save the workbook exactly what's in the code, but i can't expect that to be a practical solution when opening a workbook and having to change my code each time. I don't have to make such accommodations for anything else I have written.

  22. #22
    Forum Contributor
    Join Date
    08-09-2012
    Location
    British Columbia
    MS-Off Ver
    Excel 2010
    Posts
    189

    Re: My VBA code stopped working properly

    You don't, provided you have the right sheet active.

    The issue is using Sheet1 in the previous codes.
    Written like that it's the sheets vba code name which is limited to the workbook containing the code.

  23. #23
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: My VBA code stopped working properly

    Quote Originally Posted by NoSparks View Post
    The issue is using Sheet1 in the previous codes.
    Written like that it's the sheets vba code name which is limited to the workbook containing the code.
    Hi,

    That is not absolutely correct. If you set a reference from one workbook to another, you may refer to the codenames of sheets in the referenced workbook, prefixed with the name of the project.
    Don
    Please remember to mark your thread 'Solved' when appropriate.

+ 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. My VBA code stopped working please help.
    By Dark0Prince in forum Excel General
    Replies: 4
    Last Post: 01-10-2017, 06:46 PM
  2. [SOLVED] VBA Code is stopped working?
    By stoicy in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-29-2016, 05:26 PM
  3. Macro has stopped working properly
    By mvparker79 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 11-06-2015, 02:18 AM
  4. [SOLVED] Alphanumeric sort stopped working properly.
    By Pubicon in forum Excel General
    Replies: 6
    Last Post: 01-26-2015, 01:14 PM
  5. [SOLVED] Why has my code stopped working?!
    By JamesFletcher in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-21-2013, 07:07 AM
  6. Code stopped working
    By kinleyr in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-16-2010, 12:25 PM
  7. Code stopped working
    By chris46521 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-09-2006, 10:05 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