VBA if cell contains copy to another cell with a catch

mtnbkr0918

New Member
Joined
Oct 6, 2023
Messages
2
Office Version
  1. 365
  2. 2021
  3. 2019
Platform
  1. Windows
I have a spreadsheet that contains data in column A that I need to combine the cells below it. I have attached screenshot and attach the sample spreadsheet.

so cell a1, a2, skip a3 (has the word hardware), a4, and a5 combined into one row with a comma seaparting each cell
A1fc1fc1,Port Desc fc1,Port WWN 1,Peer port WWN 1-1
A2Port Desc fc1
A3Hardware
A4Port WWN 1
A5Peer port WWN 1-1

There is one catch. Some of the cells do not have a Port Desc FCx and this is random. To do this manually takes too long.

Any help will be greatly apprecaited.

so the combine would look like this.
A7fc2fc2,No Desc,Port WWN 2,Peer port WWN 2-2Missing Port Desc
A8Hardware
A9Port WWN 2
A10Peer port WWN 2-2

Between each FCx is a cell with -- so they would need to be skipped
A1fc1fc1,Port Desc fc1,Port WWN 1,Peer port WWN 1-1has port desc so it needs to be included
A2Port Desc fc1
A3Hardware
A4Port WWN 1
A5Peer port WWN 1-1
A6--
A7fc2fc2,No Desc,Port WWN 2,Peer port WWN 2-2Missing Port Desc
A8Hardware
A9Port WWN 2
A10Peer port WWN 2-2
A11--
A12fc3fc3,Port Desc fc3,Port WWN 3,Peer port WWN 3-3has port desc so it needs to be included
A13Port Desc fc3
A14Hardware
A15Port WWN 3
A16Peer port WWN 3-3
A17--


Any help would be greatly apprecaited
 

Attachments

  • Screenshot 2023-05-12 233141.png
    Screenshot 2023-05-12 233141.png
    11.9 KB · Views: 5

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Data in A:B. Result will be in Column C.
VBA code:
VBA Code:
Sub JoinText()
Dim M, A
Dim Lr&, T&, Ta&, R1&, R2&
Lr = Range("A" & Rows.Count).End(xlUp).Row
A = Sheets("Sheet1").Range("A1:B" & Lr)
ReDim B(1 To Lr, 1 To 1)
'k = "IF(lefT(B1:B" & Lr & ",2)=""fc"",Row(B1:B" & Lr & "),false)"
M = Filter(Evaluate("Transpose(IF(lefT(B1:B" & Lr & ",2)=""fc"",Row(B1:B" & Lr & "),false))"), False, False)
For T = LBound(M) To UBound(M)
R1 = M(T): If T = UBound(M) Then R2 = Lr - 1 Else R2 = M(T + 1) - 1

If Left(A(R1 + 1, 2), 12) <> "Port Desc fc" Then A(R1 + 1, 2) = "No Desc"
    For Ta = R1 To R2
    B(R1, 1) = B(R1, 1) & "," & A(Ta, 2)
    Next Ta
If B(R1, 1) <> "" Then B(R1, 1) = Mid(B(R1, 1), 2)

Next T
Range("C:C").Clear
Range("c1").Resize(M(UBound(M)), 1) = B
End Sub
A1fc1fc1,Port Desc fc1,Hardware,Port WWN 1,Peer port WWN 1-1,
A2Port Desc fc1
A3Hardware
A4Port WWN 1
A5Peer port WWN 1-1
A7fc2fc2,No Desc,Port WWN 2,Peer port WWN 2-2,
A8Hardware
A9Port WWN 2
A10Peer port WWN 2-2
A12fc3fc3,Port Desc fc3,Hardware,Port WWN 3,Peer port WWN 3-3
A13Port Desc fc3
A14Hardware
A15Port WWN 3
A16Peer port WWN 3-3
A17--
 
Upvote 0
If Data in column A. Result in Column B.
VBA code:
VBA Code:
Sub JoinText()
Dim M, A
Dim Lr&, T&, Ta&, R1&, R2&
Lr = Range("A" & Rows.Count).End(xlUp).Row
A = Sheets("Sheet1").Range("A1:A" & Lr)
ReDim B(1 To Lr, 1 To 1)
M = Filter(Evaluate("Transpose(IF(lefT(A1:A" & Lr & ",2)=""fc"",Row(A1:A" & Lr & "),false))"), False, False)

For T = LBound(M) To UBound(M)
R1 = M(T): If T = UBound(M) Then R2 = Lr - 1 Else R2 = M(T + 1) - 1

If Left(A(R1 + 1, 1), 1) <> "Port Desc fc" Then A(R1 + 1, 1) = "No Desc"
    For Ta = R1 To R2
    B(R1, 1) = B(R1, 1) & "," & A(Ta, 1)
    Next Ta
If B(R1, 1) <> "" Then B(R1, 1) = Mid(B(R1, 1), 2)

Next T
Range("B:B").Clear
Range("B1").Resize(M(UBound(M)), 1) = B
End Sub
fc1fc1,No Desc,Hardware,Port WWN 1,Peer port WWN 1-1,
Port Desc fc1
Hardware
Port WWN 1
Peer port WWN 1-1
fc2fc2,No Desc,Port WWN 2,Peer port WWN 2-2,
Hardware
Port WWN 2
Peer port WWN 2-2
fc3fc3,No Desc,Hardware,Port WWN 3,Peer port WWN 3-3
Port Desc fc3
Hardware
Port WWN 3
Peer port WWN 3-3
--
 
Upvote 0
If Data in column A. Result in Column B.
VBA code:
VBA Code:
Sub JoinText()
Dim M, A
Dim Lr&, T&, Ta&, R1&, R2&
Lr = Range("A" & Rows.Count).End(xlUp).Row
A = Sheets("Sheet1").Range("A1:A" & Lr)
ReDim B(1 To Lr, 1 To 1)
M = Filter(Evaluate("Transpose(IF(lefT(A1:A" & Lr & ",2)=""fc"",Row(A1:A" & Lr & "),false))"), False, False)

For T = LBound(M) To UBound(M)
R1 = M(T): If T = UBound(M) Then R2 = Lr - 1 Else R2 = M(T + 1) - 1

If Left(A(R1 + 1, 1), 1) <> "Port Desc fc" Then A(R1 + 1, 1) = "No Desc"
    For Ta = R1 To R2
    B(R1, 1) = B(R1, 1) & "," & A(Ta, 1)
    Next Ta
If B(R1, 1) <> "" Then B(R1, 1) = Mid(B(R1, 1), 2)

Next T
Range("B:B").Clear
Range("B1").Resize(M(UBound(M)), 1) = B
End Sub
fc1fc1,No Desc,Hardware,Port WWN 1,Peer port WWN 1-1,
Port Desc fc1
Hardware
Port WWN 1
Peer port WWN 1-1
fc2fc2,No Desc,Port WWN 2,Peer port WWN 2-2,
Hardware
Port WWN 2
Peer port WWN 2-2
fc3fc3,No Desc,Hardware,Port WWN 3,Peer port WWN 3-3
Port Desc fc3
Hardware
Port WWN 3
Peer port WWN 3-3
--
Thank you so much. This worked perfectly.
 
Upvote 0
For MS365, what about just this?

VBA Code:
Sub Join_Text()
  With Range("B1:B" & Range("A" & Rows.Count).End(xlUp).Row)
    .Formula2 = "=IF(LEFT(A1,2)=""fc"",TEXTJOIN("","",,A1,IF(LEFT(A2,9)=""Port Desc"","""",""No Desc""),SUBSTITUTE(A2:INDEX(A3:A$1000,MATCH(""--"",A3:A$1000,0)-1),""Hardware"","""")),"""")"
    .Value = .Value
  End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,912
Members
452,366
Latest member
TePunaBloke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top