Consulting

Results 1 to 4 of 4

Thread: Conditional selection in excel VBA?

  1. #1

    Question Conditional selection in excel VBA?

    Hi
    I am new to VBA so consequently am not sure how to select cells based on the values of other cells.

    I have a spreadsheet with values of 1 or 2 in column C. I would like to create a macro to copy A1 and B1 if the value of C1 is 1, or F1 if it is 2, and so on until the first blank cell in column C is reached.

    Hope that's clear and thank you very much for any help!

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    For i = 1 To Range("C1").End(xlDown).Row
       If Cells(i, "C").Value = 1 Then
         NextRow = NextRow + 1
         Cells(i, "A").Resize(,2).Copy Worksheets("Sheet2").Cells(NextRow, "A")
         ElseIf Cells(i, ""C").Value = 2 Then
         NextRow = NextRow + 1
         Cells(i, "F").Copy Worksheets("Sheet2").Cells(NextRow, "A")
       End If
    Next i
    Last edited by Aussiebear; 04-09-2023 at 04:12 AM. Reason: Adjusted the code tags
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    Quote Originally Posted by xld
    For i = 1 To Range("C1").End(xlDown).Row
    If Cells(i, "C").Value = 1 Then
    NextRow = NextRow + 1
    Cells(i, "A").Resize(,2).Copy Worksheets("Sheet2").Cells(NextRow, "A")
    ElseIf Cells(i, ""C").Value = 2 Then
    NextRow = NextRow + 1
    Cells(i, "F").Copy Worksheets("Sheet2").Cells(NextRow, "A")
    End If
    Next i
    Thanks for that, although I don't think I explained what I needed clearly enough. The values in column C are grouped, i.e. all the 1s and 2s are together. What I need is to copy all the cells in columns A and B which have corresponding values of 1 in column C so that they can be manually pasted into another program. After that I then need to copy the cells in column F with corresponding values of 2 in column C so that they can also be manually pasted into another program. I presume this is only possible with some user interaction with the macro between the two e.g. MsgBox
    Thanks again
    Last edited by Aussiebear; 04-09-2023 at 04:13 AM. Reason: Adjusted the code tags

  4. #4
    Moderator VBAX Master geekgirlau's Avatar
    Joined
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    1,464
    Location
    It sounds like you could do this fairly simply with an advanced filter, where you opt to copy the resulting cells to another location.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •