+ Reply to Thread
Results 1 to 30 of 30

Ranges VBA

  1. #1
    Forum Contributor
    Join Date
    02-13-2013
    Location
    Columbia, SC
    MS-Off Ver
    Excel 2010
    Posts
    217

    Ranges VBA

    Hello, I was wondering if anyone could help me out with ranges? I would like to have a specific range chosen for each worksheet for the following function to be applied. The range needs to be
    X2:AI. the reason there is not a number after AI is because I need it to go down to the last row of data on each sheet. So you could maybe reference where column A stops? The amount of rows is different on each sheet and I would like to loop through all of my worksheets except the one titled "Summary" Can anyone help me please?

    Please Login or Register  to view this content.

  2. #2
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: Ranges VBA

    Untested -
    Please Login or Register  to view this content.
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  3. #3
    Forum Contributor
    Join Date
    02-13-2013
    Location
    Columbia, SC
    MS-Off Ver
    Excel 2010
    Posts
    217

    Re: Ranges VBA

    Hmmm that does seem like a very good start! When I go to cell AJ and type =GetCells(X2:AA2) it gives me the error: "Duplicate declaration in current scope"
    If I use my code and do the getcells formula it works, but the problem is that I have to go to each sheet and highlight each selection. Do you have any idea why your code produces that error?

  4. #4
    Forum Contributor
    Join Date
    02-13-2013
    Location
    Columbia, SC
    MS-Off Ver
    Excel 2010
    Posts
    217

    Re: Ranges VBA

    Does anyone know how to fix an error that says "Duplicate declaration in current scope"

  5. #5
    Registered User
    Join Date
    07-27-2012
    Location
    Malaysia
    MS-Off Ver
    Excel 2007/2010
    Posts
    86

    Re: Ranges VBA

    I assumed that the user doesn't get to select the cells
    Please Login or Register  to view this content.
    Boon

  6. #6
    Forum Contributor
    Join Date
    02-13-2013
    Location
    Columbia, SC
    MS-Off Ver
    Excel 2010
    Posts
    217

    Re: Ranges VBA

    Ok I see. No more error but when I go to col AJ and type =getcells(X2:AI2) it says #Value. Any ideas why?

  7. #7
    Registered User
    Join Date
    07-27-2012
    Location
    Malaysia
    MS-Off Ver
    Excel 2007/2010
    Posts
    86

    Re: Ranges VBA

    u try =getcells() and see if this is what you want

  8. #8
    Forum Contributor
    Join Date
    02-13-2013
    Location
    Columbia, SC
    MS-Off Ver
    Excel 2010
    Posts
    217

    Re: Ranges VBA

    It is getting really close! But it is just storing the data from the first worksheet and when I do get cells, it does all of them, instead of just the row i want :/

  9. #9
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: Ranges VBA

    In that case, you need to specify the myrange to be the range you want. You do not have to calculate the lastrow to specify the range.

  10. #10
    Forum Contributor
    Join Date
    02-13-2013
    Location
    Columbia, SC
    MS-Off Ver
    Excel 2010
    Posts
    217

    Re: Ranges VBA

    I am still not really getting it to work. When I use the code I posted and then go to excel and use the get cells formula, it gives me the #value error.

  11. #11
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: Ranges VBA

    It may help if you explain what you are trying to do with the function.

  12. #12
    Forum Contributor
    Join Date
    02-13-2013
    Location
    Columbia, SC
    MS-Off Ver
    Excel 2010
    Posts
    217

    Re: Ranges VBA

    I have 80 worksheets. On each worksheet I have column F that has a sentence. However, some of these sentences are too long. So I send text to columns over columns X:AI where I can edit individual words and make them shorter or delete some. Then, I concatenate them into column AJ. Then in column AK I show the character length of each cell in column AJ and if it is greater than 31, I highlight it. does this make sense? I have two codes that somewhat work but both have problems. The first is the following but if I edit any data, it doesn't automatically update column AJ (I have to run macro each time).

    Please Login or Register  to view this content.
    I also have the following but it's not the best because I have to highlight each range in each worksheet I want, and then type =getcells() in column AJ:
    Please Login or Register  to view this content.

  13. #13
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: Ranges VBA

    Why do you need a function for getcells? You can directly automate the whole process where the columns are copied over to X onwards and text to columns performed and the length checked and then highlighted.

    If you upload a sample file, i can try and help you with the pending portions.

  14. #14
    Forum Contributor
    Join Date
    02-13-2013
    Location
    Columbia, SC
    MS-Off Ver
    Excel 2010
    Posts
    217

    Re: Ranges VBA

    Well, i also need them to be concatenated and the issue there is spacing. I need just the concatenated cells to insert a space after every word except the last one. Can that be done also? I know it can, but just want to clarify. How do i attach a file? And I do not have to use the getcells formula. The first code works amazingly, it just doesnt update unless i rerun it

  15. #15
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: Ranges VBA

    Yes, through code we can try and get as much as possible automated.

    To Attach a File:

    1. Click on Go Advanced
    2. In the frame Attach Files you will see the button Manage Attachments
    3. Click the button.
    4. A new window will open titled Manage Attachments - Excel Forum.
    5. Click the Browse... button to locate your file for uploading.
    6. This will open a new window File Upload.
    7. Once you have located the file to upload click the Open button. This window will close.
    8. You are now back in the Manage Attachments - Excel Forum window.
    9. Click the Upload button and wait until the file has uploaded.
    10. Close the window and then click Submit.

  16. #16
    Forum Contributor
    Join Date
    02-13-2013
    Location
    Columbia, SC
    MS-Off Ver
    Excel 2010
    Posts
    217

    Re: Ranges VBA

    Well, i also need them to be concatenated and the issue there is spacing. I need just the concatenated cells to insert a space after every word except the last one. Can that be done also? I know it can, but just want to clarify. How do i attach a file?
    Attached Files Attached Files

  17. #17
    Forum Contributor
    Join Date
    02-13-2013
    Location
    Columbia, SC
    MS-Off Ver
    Excel 2010
    Posts
    217

    Re: Ranges VBA

    I just need a way to send all sheets to columns, be able to edit those columns, concatenate so that it is a sentence with the right spacing and do everything for all sheets at once.

  18. #18
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: Ranges VBA

    Regarding the concatenation, you have opened a thread http://www.excelforum.com/excel-prog...problem-2.html. Didnt it get resolved here?

  19. #19
    Forum Contributor
    Join Date
    02-13-2013
    Location
    Columbia, SC
    MS-Off Ver
    Excel 2010
    Posts
    217

    Re: Ranges VBA

    I cannot get the values in AJ to update still unless I rerun the macro manually. How can I fix this? I tried everything. He also said it would only work if a single cell in AI is changed. I can use his code if you can figure out a way to make the code run when any cell in X:AI is changed
    Last edited by bjcowen9000; 02-21-2013 at 10:15 AM.

  20. #20
    Forum Contributor
    Join Date
    02-13-2013
    Location
    Columbia, SC
    MS-Off Ver
    Excel 2010
    Posts
    217

    Re: Ranges VBA

    Ok, I figured this out but can I do it for all sheets rather than current selection. That way I do not have to right click every single tab:

    Please Login or Register  to view this content.

  21. #21
    Forum Contributor
    Join Date
    02-13-2013
    Location
    Columbia, SC
    MS-Off Ver
    Excel 2010
    Posts
    217

    run this code for all wrks

    Is there a way to have this code run for all worksheets instead of having to right click on each one and click view code
    Please Login or Register  to view this content.

  22. #22
    Forum Contributor
    Join Date
    09-29-2011
    Location
    Kolkata, India
    MS-Off Ver
    Excel 2003/2007
    Posts
    182

    Re: run this code for all wrks

    Hi,

    Try within Thisworkbook method


    Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)

    End Sub



    Regards
    taps

  23. #23
    Forum Contributor
    Join Date
    02-13-2013
    Location
    Columbia, SC
    MS-Off Ver
    Excel 2010
    Posts
    217

    Re: run this code for all wrks

    I am not sure what you mean. do i just create a new macro and put that? can you me exactly waht you mean?

  24. #24
    Forum Contributor
    Join Date
    09-29-2011
    Location
    Kolkata, India
    MS-Off Ver
    Excel 2003/2007
    Posts
    182

    Re: run this code for all wrks

    Excel VBA Editer.jpg

    Hi write your entire codes within the sub shown in picture.

    Regards
    taps

  25. #25
    Forum Contributor
    Join Date
    02-13-2013
    Location
    Columbia, SC
    MS-Off Ver
    Excel 2010
    Posts
    217

    Re: run this code for all wrks

    But in my code I have set ws = active sheet. wont that only work on 1 sheet?

  26. #26
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646
    Use Sh which is the sheet argument passed to the sub.
    If posting code please use code tags, see here.

  27. #27
    Forum Contributor
    Join Date
    09-29-2011
    Location
    Kolkata, India
    MS-Off Ver
    Excel 2003/2007
    Posts
    182

    Re: run this code for all wrks

    Testing is essential for every programming.
    Please try, hope it will work.

  28. #28
    Forum Contributor
    Join Date
    02-13-2013
    Location
    Columbia, SC
    MS-Off Ver
    Excel 2010
    Posts
    217

    Re: run this code for all wrks

    now when i run waht you said, it doesnt even update the current worksheet that i am on. maybe i am missing something? I have:

    Please Login or Register  to view this content.

  29. #29
    Forum Contributor
    Join Date
    09-29-2011
    Location
    Kolkata, India
    MS-Off Ver
    Excel 2003/2007
    Posts
    182

    Re: run this code for all wrks

    Hi,

    Could you please attached an excel sample file for testing?

    Regards
    taps


    Again:::: Sorry, I have missed out your previous post. Please don't attached the sample file.
    Last edited by taps; 02-22-2013 at 04:21 AM.

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

    Re: Ranges VBA

    Change this,
    Please Login or Register  to view this content.
    you this.
    Please Login or Register  to view this content.
    Or remove the line and change ws to Sh throughout the code.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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