Macro Step to Find dash and copy cell values

wazzulu1

Board Regular
Joined
Oct 4, 2006
Messages
164
Hi;

I am trying to make a function to go through Column A, and when a "-" (dash, no quotes) is in the cell to then copy the next cell in column A to column B, but to then copy the same value to column C on the next row down, along with coping the value from that same row in column A to column D. This would need to loop through the column until it does not find another "-" (dash). Appreciate any suggestions for this. (Picture uploaded below)
 

Attachments

  • macro to find dash.jpg
    macro to find dash.jpg
    71 KB · Views: 7

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
How about
VBA Code:
Sub wazzulu()
   Dim Ar As Areas
   Dim Rng As Range
   
   With Range("A1", Range("A" & Rows.Count).End(xlUp))
      .Replace "-", "=xxx", xlWhole, , , , False, False
      Set Ar = .SpecialCells(xlConstants).Areas
      .Replace "=xxx", "-", xlWhole, , , , False, False
   End With
   For Each Rng In Ar
      Rng.Offset(, 1).Resize(1).Value = Rng.Value
      Rng.Offset(1, 2).Resize(Rng.Count - 1).Value = Rng.Resize(1).Value
      Rng.Offset(1, 3).Resize(Rng.Count - 1).Value = Rng.Offset(1).Resize(Rng.Count - 1).Value
   Next Rng
End Sub
 
Upvote 0
Whoa Fluff. That's totally different. Pretty cool. Here's another way.

TCO Transform (VBA).xlsb.xlsm
ABCD
1-
2aaaaaa
3bbbaaabbb
4cccaaaccc
5dddaaaddd
6eeeaaaeee
7-
8mnbmnb
9jhgmnbjhg
10lopmnblop
11pppmnbppp
12htgmnbhtg
13-
14yyyyyy
15bgtyyybgt
16pouyyypou
17wqeyyywqe
Sheet2


VBA Code:
Sub DASH()
Dim r As Range:         Set r = Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row)
Dim AR() As Variant:    AR = r.Value2: ReDim Preserve AR(1 To UBound(AR), 1 To 4)
Dim LR As Long:         LR = UBound(AR)
Dim b As Boolean:       b = True

For i = UBound(AR) To 1 Step -1
    If AR(i, 1) = "-" Then
        For j = i + 1 To LR
            If b Then
                AR(j, 2) = AR(j, 1)
                b = False
            Else
                AR(j, 3) = AR(i + 1, 1)
                AR(j, 4) = AR(j, 1)
            End If
        Next j
        b = True
        LR = i - 1
    End If
Next i

r.Clear
Set r = r.Resize(UBound(AR), UBound(AR, 2))
r.Value2 = AR
End Sub
 
Upvote 0
Also, a way using Power Query.

TCO Transform (VBA).xlsb.xlsm
ABCD
1Column1ColBColCColD
2-
3aaaaaa
4bbbaaabbb
5cccaaaccc
6dddaaaddd
7eeeaaaeee
8-
9mnbmnb
10jhgmnbjhg
11lopmnblop
12pppmnbppp
13htgmnbhtg
14-
15yyyyyy
16bgtyyybgt
17pouyyypou
18wqeyyywqe
Table3


Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table3"]}[Content],
    Index = Table.AddIndexColumn(Source, "Index", 0, 1, Int64.Type),
    ColB = Table.AddColumn(Index, "ColB", each try if Index[Column1]{[Index]-1} = "-" then [Column1] else null otherwise null),
    ColD = Table.AddColumn(ColB, "ColD", each if [Column1]<>"-" and [ColB]<>[Column1] then [Column1] else null),
    Duplicate = Table.FillDown(Table.DuplicateColumn(ColD, "ColB", "Duplicate"),{"Duplicate"}),
    ColC = Table.AddColumn(Duplicate, "ColC", each if [ColD] <> null then [Duplicate] else null),
    ROC = Table.SelectColumns(ColC,{"Column1", "ColB", "ColC", "ColD"})
in
    ROC
 
Upvote 0
Whoa Fluff. That's totally different. Pretty cool. Here's another way.

TCO Transform (VBA).xlsb.xlsm
ABCD
1-
2aaaaaa
3bbbaaabbb
4cccaaaccc
5dddaaaddd
6eeeaaaeee
7-
8mnbmnb
9jhgmnbjhg
10lopmnblop
11pppmnbppp
12htgmnbhtg
13-
14yyyyyy
15bgtyyybgt
16pouyyypou
17wqeyyywqe
Sheet2


VBA Code:
Sub DASH()
Dim r As Range:         Set r = Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row)
Dim AR() As Variant:    AR = r.Value2: ReDim Preserve AR(1 To UBound(AR), 1 To 4)
Dim LR As Long:         LR = UBound(AR)
Dim b As Boolean:       b = True

For i = UBound(AR) To 1 Step -1
    If AR(i, 1) = "-" Then
        For j = i + 1 To LR
            If b Then
                AR(j, 2) = AR(j, 1)
                b = False
            Else
                AR(j, 3) = AR(i + 1, 1)
                AR(j, 4) = AR(j, 1)
            End If
        Next j
        b = True
        LR = i - 1
    End If
Next i

r.Clear
Set r = r.Resize(UBound(AR), UBound(AR, 2))
r.Value2 = AR
End Sub
hi, this did not work
 
Upvote 0
How about
VBA Code:
Sub wazzulu()
   Dim Ar As Areas
   Dim Rng As Range
  
   With Range("A1", Range("A" & Rows.Count).End(xlUp))
      .Replace "-", "=xxx", xlWhole, , , , False, False
      Set Ar = .SpecialCells(xlConstants).Areas
      .Replace "=xxx", "-", xlWhole, , , , False, False
   End With
   For Each Rng In Ar
      Rng.Offset(, 1).Resize(1).Value = Rng.Value
      Rng.Offset(1, 2).Resize(Rng.Count - 1).Value = Rng.Resize(1).Value
      Rng.Offset(1, 3).Resize(Rng.Count - 1).Value = Rng.Offset(1).Resize(Rng.Count - 1).Value
   Next Rng
End Sub
I apologize, my example had the three characters, but in actuality, it's words/descriptions that are labels which exceed three alpha characters. This is copying the dash to column B, filling every cell in it with the dash, and did not copy like the example I showed.
 
Upvote 0
This is what I get after running my code.
+Fluff 1.xlsm
ABCD
1-
2aaaaaa
3bbbaaabbb
4cccaaaccc
5dddaaaddd
6eeeaaaeee
7-
8mnbmnb
9fffmnbfff
10gggmnbggg
11hhhmnbhhh
12iiimnbiii
13jjjmnbjjj
14-
15yyyyyy
16bgtyyybgt
17pouyyypou
18wqeyyywqe
19
Master


The length of text in the cells is irrelevant.
Please post an accurate sample of your data.

MrExcel has a tool called “XL2BB” that lets you post samples of your data that will allow us to copy/paste it to our Excel spreadsheets, so we can work with the same copy of data that you are. Instructions on using this tool can be found here: XL2BB Add-in

Note that there is also a "Test Here” forum on this board. This is a place where you can test using this tool (or any other posting techniques that you want to test) before trying to use those tools in your actual posts.
 
Upvote 0

Forum statistics

Threads
1,214,915
Messages
6,122,212
Members
449,074
Latest member
cancansova

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