Consulting

Results 1 to 6 of 6

Thread: Concatenate in VBA?

  1. #1
    VBAX Regular
    Joined
    Jun 2019
    Posts
    35
    Location

    Concatenate in VBA?

    I have this code, but rather than it copying Cell E7 in to Cell E5, I wondered if it was possible to concatenate E7, E9, H9 & H10 (I've written a formula but can't think how to do it in VBA): =IF(E7="","",CONCATENATE(E7,", ",TEXT(E9,"dd-mmm-yy"),", ",TEXT(H9,"hh:mm")," to ",TEXT(H10,"hh:mm")))

    I only ask, as the Course Name (E7) isn't unique, so want to add the date and time to the Event to make a UI?


    Sub Event_SaveUpdate()
    With Form
        If .Range("E7").Value = Empty Then
            MsgBox "Please choose a name for the Event (from the list provided) BEFORE trying to save"
            Exit Sub
        End If
        If .Range("B5").Value = Empty Then 'New Event
            .Range("D3").Value = .Range("B6").Value 'Event ID
            EventRow = Data.Range("C99999").End(xlUp).Row + 2 'Find first row without data
            Data.Range("C" & EventRow).Value = .Range("B6").Value 'Event ID
        Else 'Existing Event
            EventRow = .Range("B5").Value 'Event Row
        End If
        For EventCol = 4 To 27
            Data.Cells(EventRow, EventCol).Value = .Range(Data.Cells(1, EventCol).Value).Value 'Add event to Data worksheet
        Next EventCol
        .Range("B3").Value = True 'Event Update to TRUE
        .Range("E5").Value = .Range("E7").Value 'Event Name in Drop down list
        .Range("B3").Value = False 'Event Update to FALSE
        
    End With
    End Sub
    Last edited by Paul_Hossler; 05-20-2021 at 12:01 PM. Reason: Added CODE tags

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    Not tested

    Option Explicit
    
    
    ' =IF(E7="","",CONCATENATE(E7,", ",TEXT(E9,"dd-mmm-yy"),", ",TEXT(H9,"hh:mm")," to ",TEXT(H10,"hh:mm")))
    
    
    Sub drv()
        With ActiveSheet
            If Len(.Range("E7").Value) = 0 Then
                .Range("E5").ClearContents
            
            Else
                .Range("E5").Value = .Range("E7").Value & ", " & Format(.Range("E9").Value, "dd-mmm-yy") & ", " & _
                    Format(.Range("H9").Value, "hh:mm") & " to " & Format(.Range("H10").Value, "hh:mm")
            End If
        End With
    
    
    
    
    
    
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    Sub M_snb()
      msgbox iif(cells(7,5)="","",join(array([E7],[E9],[H9] & " to " & [H10]),", "))
    End Sub

  4. #4
    VBAX Regular
    Joined
    Jun 2019
    Posts
    35
    Location
    Thanks both - where would these bits of code fit within my existing?

    Sub Event_SaveUpdate()
    With Form
        If .Range("E7").Value = Empty Then
            MsgBox "Please choose a name for the Event (from the list provided) BEFORE trying to save"
            Exit Sub
        End If
        If .Range("B5").Value = Empty Then 'New Event
            .Range("D3").Value = .Range("B6").Value 'Event ID
            EventRow = Data.Range("C99999").End(xlUp).Row + 2 'Find first row without data
            Data.Range("C" & EventRow).Value = .Range("B6").Value 'Event ID
        Else 'Existing Event
            EventRow = .Range("B5").Value 'Event Row
        End If
        For EventCol = 4 To 27
            Data.Cells(EventRow, EventCol).Value = .Range(Data.Cells(1, EventCol).Value).Value 'Add event to Data worksheet
        Next EventCol
        .Range("B3").Value = True 'Event Update to TRUE
        .Range("E5").Value = .Range("E7").Value 'Event Name in Drop down list
        .Range("B3").Value = False 'Event Update to FALSE
        
    End With
    End Sub
    I tried this, but get an "Expected End Sub" error?
    Sub Event_SaveUpdate()
    With Form
        If .Range("E7").Value = Empty Then
            MsgBox "Please choose a name for the Event (from the list provided) BEFORE trying to save"
            Exit Sub
        End If
        If .Range("B5").Value = Empty Then 'New Event
            .Range("D3").Value = .Range("B6").Value 'Event ID
            EventRow = Data.Range("C99999").End(xlUp).Row + 2 'Find first row without data
            Data.Range("C" & EventRow).Value = .Range("B6").Value 'Event ID
        Else 'Existing Event
            EventRow = .Range("B5").Value 'Event Row
        End If
        For EventCol = 4 To 27
            Data.Cells(EventRow, EventCol).Value = .Range(Data.Cells(1, EventCol).Value).Value 'Add event to Data worksheet
        Next EventCol
        .Range("B3").Value = True 'Event Update to TRUE
        Sub drv()
            If Len(.Range("E7").Value) = 0 Then
                .Range("E5").ClearContents
            Else
                .Range("E5").Value = .Range("E7").Value & ", " & Format(.Range("E9").Value, "dd-mmm-yy") & ", " & _
                    Format(.Range("H9").Value, "hh:mm") & " to " & Format(.Range("H10").Value, "hh:mm")
            End If
        End With
        '.Range("E5").Value = .Range("E7").Value 'Event Name in Drop down list
        .Range("B3").Value = False 'Event Update to FALSE
        
    End With
    End Sub
    Last edited by Paul_Hossler; 05-21-2021 at 05:42 AM.

  5. #5
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    Please use code tags !!

  6. #6
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    Probably below the marked line


    Sub Event_SaveUpdate()
    With Form
        If .Range("E7").Value = Empty Then
            MsgBox "Please choose a name for the Event (from the list provided) BEFORE trying to save"
            Exit Sub
        End If
    
        If .Range("B5").Value = Empty Then 'New Event
            .Range("D3").Value = .Range("B6").Value 'Event ID
            EventRow = Data.Range("C99999").End(xlUp).Row + 2 'Find first row without data
            Data.Range("C" & EventRow).Value = .Range("B6").Value 'Event ID
    
        Else 'Existing Event
            EventRow = .Range("B5").Value 'Event Row
        End If
    
        For EventCol = 4 To 27
            Data.Cells(EventRow, EventCol).Value = .Range(Data.Cells(1, EventCol).Value).Value 'Add event to Data worksheet
        Next EventCol
    
        .Range("B3").Value = True 'Event Update to TRUE
    
    
    '------------------------------.Range("E5").Value = .Range("E7").Value 'Event Name in Drop down list
            If Len(.Range("E7").Value) = 0 Then
                .Range("E5").ClearContents
            
            Else
                .Range("E5").Value = .Range("E7").Value & ", " & Format(.Range("E9").Value, "dd-mmm-yy") & ", " & _
                    Format(.Range("H9").Value, "hh:mm") & " to " & Format(.Range("H10").Value, "hh:mm")
            End If
    
        .Range("B3").Value = False 'Event Update to FALSE
    End With
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Posting Permissions

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