+ Reply to Thread
Results 1 to 23 of 23

VBA - check range in worksheet contains values

Hybrid View

  1. #1
    Registered User
    Join Date
    07-06-2015
    Location
    Wales
    MS-Off Ver
    2013
    Posts
    14

    VBA - check range in worksheet contains values

    Hi

    What would be the code to check a range of cells in a column contains any type of text, numbers etc, then if any of the cells do contain values input the values into one cell in a different worksheet.

    Thanks in advance

  2. #2
    Valued Forum Contributor Parth007's Avatar
    Join Date
    12-01-2014
    Location
    Banglore
    MS-Off Ver
    2010
    Posts
    879

    Re: VBA - check range in worksheet contains values

    Can you provide an example.... any sample sheet with output
    Regards
    Parth

    I appreciate your feedback. Hit * if u Like.
    Rules - http://www.excelforum.com/forum-rule...rum-rules.html

  3. #3
    Forum Contributor
    Join Date
    08-04-2014
    Location
    Riga
    MS-Off Ver
    2010
    Posts
    579

    Re: VBA - check range in worksheet contains values

    From your initial post it seems you wish to copy many cells that contain data into a single cell in another worksheet?

  4. #4
    Registered User
    Join Date
    07-06-2015
    Location
    Wales
    MS-Off Ver
    2013
    Posts
    14

    Re: VBA - check range in worksheet contains values

    I would ideally not want to post the workbook as it is confidential.

    Yea I want to copy the range A4:A63 to a single cell in another worksheet, only if the range contains values. the reason for this is I don't want the procedure to run unless values are found in the range. Is this possible please.

  5. #5
    Forum Contributor
    Join Date
    08-04-2014
    Location
    Riga
    MS-Off Ver
    2010
    Posts
    579

    Re: VBA - check range in worksheet contains values

    You can write a formula like this to pull the values in a single cell, regardless of content:

    =A1&A2&A3&A4&A5&A6&A7&A8&A9&A10&A11&A12&A13&A14&A15&A16&A17&A18&A19&A20&A21&A22&A23

    I abuse Word and Find&Replace for managing long formulas like this.

  6. #6
    Valued Forum Contributor Parth007's Avatar
    Join Date
    12-01-2014
    Location
    Banglore
    MS-Off Ver
    2010
    Posts
    879

    Re: VBA - check range in worksheet contains values

    Hi Sopie....

    Sub Copy_Data()
      Application.ScreenUpdating = False
      Worksheets("Sheet2").Range("A4:A63").Value = Worksheets("Sheet1").Range("A4:A63").Value
      Application.ScreenUpdating = True
    End Sub
    Last edited by Parth007; 07-31-2015 at 07:20 AM.

  7. #7
    Registered User
    Join Date
    07-06-2015
    Location
    Wales
    MS-Off Ver
    2013
    Posts
    14

    Re: VBA - check range in worksheet contains values

    I have tried that but I want to write it in vba as I have a load of other procedures to run along side that which will be assigned to a button.

  8. #8
    Registered User
    Join Date
    07-06-2015
    Location
    Wales
    MS-Off Ver
    2013
    Posts
    14

    Re: VBA - check range in worksheet contains values

    Thank you Parth, although that's not exactly what im looking for.

    I want to check a range to see if any values exist and then if they do copy the range of values to one cell.

  9. #9
    Valued Forum Contributor Parth007's Avatar
    Join Date
    12-01-2014
    Location
    Banglore
    MS-Off Ver
    2010
    Posts
    879

    Re: VBA - check range in worksheet contains values

    Any specific value to be checked in range?
    or
    you would rather see if the range have blanks cells, if so then pick only non blank cells & move to other sheet.. may be

  10. #10
    Forum Contributor
    Join Date
    08-04-2014
    Location
    Riga
    MS-Off Ver
    2010
    Posts
    579

    Re: VBA - check range in worksheet contains values

    If one could figure out how to write this formula in VBA:
    =IF(COUNTA(A1:A20)>=1;A1&A2&A3&A4&A5&A6&A7&A8&A9&A10&A11&A12&A13&A14&A15&A16&A17&A18&A19&A20)

  11. #11
    Valued Forum Contributor Parth007's Avatar
    Join Date
    12-01-2014
    Location
    Banglore
    MS-Off Ver
    2010
    Posts
    879

    Re: VBA - check range in worksheet contains values

    bmouse .. i m puzzled with the formula you wrote.. ofcourse that can be diverted via VBA but... transfer the data & etc...

  12. #12
    Registered User
    Join Date
    07-06-2015
    Location
    Wales
    MS-Off Ver
    2013
    Posts
    14

    Re: VBA - check range in worksheet contains values

    No, no specific value which is annoying

    Yes if the only way is to look for blank cells then copy then non blank cells that will be fine. Will be better than nothing.

  13. #13
    Valued Forum Contributor Parth007's Avatar
    Join Date
    12-01-2014
    Location
    Banglore
    MS-Off Ver
    2010
    Posts
    879

    Re: VBA - check range in worksheet contains values

    Hi Sophie...

    you can see below ...(copy non blank cells to another worksheet)
    Sub CopyDataOnly()
    On Error Resume Next
    Sheets("Sheet1").Range("A1:A63"). _
    SpecialCells(xlCellTypeConstants, 23).Copy
    Sheets("Sheet2").Range("A1").PasteSpecial _
    Paste:=xlPasteValues, _
    Operation:=xlNone, _
    SkipBlanks:=False, _
    Transpose:=False
    On Error GoTo 0
    Application.CutCopyMode = False
    End Sub
    OR

    Sub CopyData()
        Dim rCell As Range, TestRange As Range, CopyRange As Range
         
        Set TestRange = Sheet1.Range("A1:A63")
         
        For Each rCell In TestRange
            If Not IsEmpty(rCell) Then
                If CopyRange Is Nothing Then
                    Set CopyRange = rCell
                Else
                    Set CopyRange = Union(CopyRange, rCell)
                End If
            End If
        Next rCell
         
        CopyRange.Copy Sheet2.Range("A1")
         
         
    End Sub

  14. #14
    Forum Contributor
    Join Date
    08-04-2014
    Location
    Riga
    MS-Off Ver
    2010
    Posts
    579

    Re: VBA - check range in worksheet contains values

    To my understanding the objective is to gather data from a range of cells into a single cell if there is ANY data in the range. So, COUNTA and IF check to see if even 1 cell from the range contains data. After that every cell in the range gets added together. Putting every cell in the formula is easier, because we can't really determine where the data is. Maybe it is in A1, maybe in A17, maybe both, etc., etc. The formula doesn't add anything if the cell is blank so it just gathers any and all data from the range into a single cell. Try it out, you will understand.

  15. #15
    Registered User
    Join Date
    07-06-2015
    Location
    Wales
    MS-Off Ver
    2013
    Posts
    14

    Re: VBA - check range in worksheet contains values

    Thank you Parth, that is half of what I need.

    What do I add to copy the values in the range in sheet 1 to a single cell in the sheet 2? As the current code gets rid of the blank cells and copies the range from sheet 1 but then pastes into sheet 2 but the values are as a list and in different rows instead of all being pasted into a single cell.

    Thanks again.
    Last edited by Sophie02; 08-03-2015 at 03:47 AM.

  16. #16
    Valued Forum Contributor Parth007's Avatar
    Join Date
    12-01-2014
    Location
    Banglore
    MS-Off Ver
    2010
    Posts
    879

    Re: VBA - check range in worksheet contains values

    Hi Sophie,

    Single cell means?
    can you provide an example..

  17. #17
    Registered User
    Join Date
    07-06-2015
    Location
    Wales
    MS-Off Ver
    2013
    Posts
    14

    Re: VBA - check range in worksheet contains values

    Hi sorry.

    For example copy values from Sheet1.Range("A1:A63") to Sheet2.Range("A1")

    With the code you previously suggested it copies the correct cells but that pastes into A1:A10. But I want all the values in Sheet1.Range("A1:A63") to be displayed in Sheet2.Range("A1"). So the text in cell A1 will look like;

    A1 - Test 1 Test 2 Test 3 etc.

    And not;

    A1 - Test 1
    A2 - Test 2
    A3 - Test 3

    Sorry if im not explaining it very well.

  18. #18
    Forum Contributor
    Join Date
    08-04-2014
    Location
    Riga
    MS-Off Ver
    2010
    Posts
    579

    Re: VBA - check range in worksheet contains values

    Have you tried using the formula I posted? If you want to put many cells together in 1 cell, you can use the "&" method.

  19. #19
    Registered User
    Join Date
    07-06-2015
    Location
    Wales
    MS-Off Ver
    2013
    Posts
    14

    Re: VBA - check range in worksheet contains values

    Yea I think that is going to be the best and quicker way to do it. Thank you

  20. #20
    Registered User
    Join Date
    07-06-2015
    Location
    Wales
    MS-Off Ver
    2013
    Posts
    14

    Re: VBA - check range in worksheet contains values

    I have just realised that the reason I didn't want to do it that way is because between each cell I am putting a space, e.g = A1&" "&A2&" "&A3&" "&A4 and then when I perform an if function on the cell e.g. =if(A1<>"",.....) even if it hasn't got text (Test 1 Test 2) and is just ( ) as the function has spaces in it the =if function still performs as it is looking up the spaces in cell A1.

    Do you know a way around this?

  21. #21
    Forum Contributor
    Join Date
    08-04-2014
    Location
    Riga
    MS-Off Ver
    2010
    Posts
    579

    Re: VBA - check range in worksheet contains values

    Use this formula. If there are no cells with data within the range, the formula just returns FALSE. If you get many blank spaces, you can try to remove them with Ctrl+H.
    Formula: copy to clipboard
    =IF(COUNTA(A4:A63)>=1;A4&" "&A5&" "&A6&" "&A7&" "&A8&" "&A9&" "&A10&" "&A11&" "&A12&" "&A13&" "&A14&" "&A15&" "&A16&" "&A17&" "&A18&" "&A19&" "&A20&" "&A21&" "&A22&" "&A23&" "&A24&" "&A25&" "&A26&" "&A27&" "&A28&" "&A29&" "&A30&" "&A31&" "&A32&" "&A33&" "&A34&" "&A35&" "&A36&" "&A37&" "&A38&" "&A39&" "&A40&" "&A41&" "&A42&" "&A43&" "&A44&" "&A45&" "&A46&" "&A47&" "&A48&" "&A49&" "&A50&" "&A51&" "&A52&" "&A53&" "&A54&" "&A55&" "&A56&" "&A57&" "&A58&" "&A59&" "&A60&" "&A61&" "&A62&" "&A63)

  22. #22
    Registered User
    Join Date
    07-06-2015
    Location
    Wales
    MS-Off Ver
    2013
    Posts
    14

    Re: VBA - check range in worksheet contains values

    Thank you bmouse will you that instead.

    Thank you all for your help.

  23. #23
    Forum Expert
    Join Date
    09-01-2012
    Location
    Norway
    MS-Off Ver
    Office 365
    Posts
    2,844

    Re: VBA - check range in worksheet contains values

    Sophie, a good idea is to post a workbook with mockup data, just the minimum required to illustrate the problem. That would make it much easier to help you.
    <----- If you were helped by my posts you can say "Thank you" by clicking the star symbol down to the left

    If the problem is solved, finish of the thread by clicking SOLVED under Thread Tools
    I don't wish to leave you with no answer, yet I sometimes miss posts. If you feel I forgot you, remind me with a PM or just bump the thread.

+ 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. VBA check values from range with loop
    By Tosters in forum Excel Programming / VBA / Macros
    Replies: 16
    Last Post: 12-11-2013, 06:15 PM
  2. Check for duplicate values within the same worksheet
    By aashishd233 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-16-2013, 03:34 PM
  3. Check if inserted value is within a range of values
    By drixneedshelp in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-29-2012, 04:05 PM
  4. Is there a good way to check values from a closed worksheet?
    By 111StepsAhead in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-11-2011, 07:18 PM
  5. Check range for values
    By KKEOGH in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 04-01-2011, 01:49 PM
  6. Check for numeric values in a range
    By KDN in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-16-2010, 05:43 PM
  7. Check if Value Exists in Range For Worksheet Filtering
    By DGA2008 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-03-2009, 12:03 PM
  8. [SOLVED] Q: check a range values
    By JIM.H. in forum Excel General
    Replies: 2
    Last Post: 10-20-2005, 08: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