+ Reply to Thread
Results 1 to 8 of 8

VBA Merge Help

  1. #1
    Registered User
    Join Date
    05-09-2023
    Location
    Mississippi, U.S.
    MS-Off Ver
    18.2303.1201.0
    Posts
    5

    VBA Merge Help

    Level II Help.jpg
    Good Day! I am active duty military that runs construction projects for the Navy. I am currently in the process of automating a template while teaching myself VBA at the same time. The file I'm currently working lists my Project's Master Activities on the left and tracks them via 3 rows that have time estimated, time earned, and time expended. Those Columns fall under a bi-weekly reporting periods. Row 6 is the full date (5/9/2023 format) but uses the "ddd" display to show just the day. Rows 4 and 5 are =TEXT(G$6,"mmmm") and supposed to merge together to display the month. The goal is that the user can change G6s date, that trickles down the columns with a +14 days for the bi-weekly reporting. (that already works) then the VBA automatically unmerges G4:AF5 (a full year), autofills the range with =TEXT(G$6,"mmmm"), then it runs a merge code that should merge all similar valued cells together. However, where the code currently sits it only merges similar cells across rows 4 and 5, but does not merge G4:G5, or J4:L5 for November for example. I have tried running the merge code a second time but that runs into errors once added. Any help with this would be very much appreciated!
    Code:

    Private Sub Worksheet_Change(ByVal Target As Range)

    Application.ScreenUpdating = False
    Application.DisplayAlerts = False

    If Target.Address = "$G$6" Then

    Range("G4:AF5").Select
    Selection.UnMerge
    Selection.Borders(xlDiagonalDown).LineStyle = xlNone
    Selection.Borders(xlDiagonalUp).LineStyle = xlNone
    With Selection.Borders(xlEdgeLeft)
    .LineStyle = xlContinuous
    .ColorIndex = 0
    .TintAndShade = 0
    .Weight = xlMedium
    End With
    With Selection.Borders(xlEdgeTop)
    .LineStyle = xlContinuous
    .ColorIndex = 0
    .TintAndShade = 0
    .Weight = xlMedium
    End With
    With Selection.Borders(xlEdgeBottom)
    .LineStyle = xlContinuous
    .ColorIndex = 0
    .TintAndShade = 0
    .Weight = xlMedium
    End With
    With Selection.Borders(xlEdgeRight)
    .LineStyle = xlContinuous
    .ColorIndex = 0
    .TintAndShade = 0
    .Weight = xlMedium
    End With
    With Selection.Borders(xlInsideVertical)
    .LineStyle = xlContinuous
    .ColorIndex = 0
    .TintAndShade = 0
    .Weight = xlMedium
    End With
    Range("G4").Select
    Selection.AutoFill Destination:=Range("G4:AF4"), Type:=xlFillDefault
    Range("G4:AF4").Select
    Range("G5").Select
    Selection.AutoFill Destination:=Range("G5:AF5"), Type:=xlFillDefault
    Range("G5:AF5").Select
    End If

    Dim rng As Range, cell As Range, mergedRange As Range
    Set rng = Range("G4:AF5")

    For Each cell In rng
    If cell.Value <> "" And cell.Value = cell.Offset(0, 1).Value Then
    If mergedRange Is Nothing Then
    Set mergedRange = cell
    End If
    Set mergedRange = Union(mergedRange, cell.Offset(0, 1))
    Else
    If Not mergedRange Is Nothing Then
    mergedRange.Merge
    mergedRange.HorizontalAlignment = xlCenter
    Set mergedRange = Nothing
    End If
    End If
    Next cell

    Application.DisplayAlerts = True
    Application.ScreenUpdating = True

    End Sub
    Last edited by BriceRandall; 05-10-2023 at 03:20 AM.

  2. #2
    Registered User
    Join Date
    05-09-2023
    Location
    Mississippi, U.S.
    MS-Off Ver
    18.2303.1201.0
    Posts
    5

    Re: VBA Merge Help

    Also, I do know it has to do with the (0,1) offset. Here's the dilemma, If I change the offset to (1,1) it doesn't do anything and if i change it to (1,0) it merges the top and bottom automatically then cant drag the +text formula across. Additionally, I know that once the code successfully merges all four, when unmerged i'll need to input

    Range("G4").Select
    Selection.AutoFill Destination:=Range("G4:G5"), Type:=xlFillDefault
    Range("G4:G5").Select

    To drag the code down before it can be dragged across. However, until I can get the code to merge all 4-6 cells this causes a block in the code. Just wanted you to know that I am tracking that part. Thanks again!

  3. #3
    Forum Expert torachan's Avatar
    Join Date
    12-27-2012
    Location
    market harborough, england
    MS-Off Ver
    Excel 2010
    Posts
    4,408

    Re: VBA Merge Help

    The simple answer is 'do not use merged cells' - browse the web for reasons not - they have no place on an Excel app - especially if you are using VBA to expedite actions.
    Torachan,

    Mission statement; Promote the use of Tables, Outlaw the use of 'merged cells' and 'RowSource'.

  4. #4
    Registered User
    Join Date
    05-09-2023
    Location
    Mississippi, U.S.
    MS-Off Ver
    18.2303.1201.0
    Posts
    5

    Re: VBA Merge Help

    I appreciate the feedback, do you have an alternate means of method for me to get the affect I am trying to achieve? Is there a way to have the full Month names displayed, centered, above their respective reporting periods in rows 4 and 5? I have tried collates and contras. Would I be able to achieve this with a pivot table? I have never worked with them before.

    Very Respectfully,
    Brice Randall

  5. #5
    Forum Expert CheeseSandwich's Avatar
    Join Date
    12-22-2021
    Location
    Kent, England
    MS-Off Ver
    365 - 2501
    Posts
    1,431

    Re: VBA Merge Help

    You can try centre across selection as an alternative to merging:

    1, Select the range of cells over which you want to center text.
    2, Right-click the selected cells, and then click Format Cells.
    3, Click the Alignment tab.
    4, In the Horizontal list, click Center Across Selection.
    5, Click OK.

    https://exceljet.net/videos/how-to-u...ction-in-excel
    If things don't change they stay the same

  6. #6
    Registered User
    Join Date
    05-09-2023
    Location
    Mississippi, U.S.
    MS-Off Ver
    18.2303.1201.0
    Posts
    5

    Re: VBA Merge Help

    I tried messing with a pivot table but it did not perform the way I was anticipating.

    Additionally, I finally did get mode code to work this morning, If anyone stumbles on this thread trying to achieve the same thing here is what i did: I changed:

    Range("G4").Select
    Selection.AutoFill Destination:=Range("G4:AF4"), Type:=xlFillDefault
    Range("G4:AF4").Select
    Range("G5").Select
    Selection.AutoFill Destination:=Range("G5:AF5"), Type:=xlFillDefault
    Range("G5:AF5").Select

    To

    Range("G4").Select
    Selection.AutoFill Destination:=Range("G4:G5"), Type:=xlFillDefault
    Range("G4:G5").Select
    Selection.AutoFill Destination:=Range("G4:AF5"), Type:=xlFillDefault

    Then input a second string of my merge code. First string is (0,1) offset so they merge across and second set is (1,0) so they merge down.

    Hope this helps!

  7. #7
    Registered User
    Join Date
    05-09-2023
    Location
    Mississippi, U.S.
    MS-Off Ver
    18.2303.1201.0
    Posts
    5

    Re: VBA Merge Help

    Thank you for you reply Cheesesandwhich! Unfortunately I did not catch it until after I posted my solve. I did have a question with that, would the formatting change if the months shift left or right? i.e. In my picture above, September would be centered in G4:H5. However, if the user's Project kicks off in October and first reports due on October 19, would the formatting shift with the change with the shifts of dates that those cells follow?

  8. #8
    Forum Expert CheeseSandwich's Avatar
    Join Date
    12-22-2021
    Location
    Kent, England
    MS-Off Ver
    365 - 2501
    Posts
    1,431

    Re: VBA Merge Help

    If you add or remove columns from the range that was centred then it will still centre across the range unless you delete the cell that has the month name in.

    The idea is that you place your value in the leftmost cell, then centre across selection, even though the value moves - it is still in the first leftmost cell. If you need to change the month then you can select the leftmost cell and edit the value, when you exit that cell it will go back to the centre.

+ 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. Replies: 5
    Last Post: 11-15-2018, 10:54 PM
  2. Mail Merge Directory - Creating merge with multiple lines
    By tiggynook in forum Excel General
    Replies: 3
    Last Post: 05-03-2017, 03:00 PM
  3. Replies: 0
    Last Post: 06-24-2015, 02:34 AM
  4. Email merge from excel merge file using pre-saved word template
    By d_max_c in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-17-2014, 12:33 PM
  5. Replies: 2
    Last Post: 01-22-2013, 11:37 AM
  6. Email Merge with variable merge records break by client level
    By sss047 in forum Word Formatting & General
    Replies: 0
    Last Post: 04-15-2011, 04:25 AM
  7. Replies: 0
    Last Post: 04-21-2006, 03:40 PM

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