VBA Code: Copy Column down to match with text in the next column

hanz753

Board Regular
Joined
Aug 9, 2017
Messages
53
Hello, I am not sure if I can use a formula or a VBA code to do this task.


[TABLE="width: 507"]
<colgroup><col width="64" style="width: 48pt;"> <col width="325" style="width: 244pt; mso-width-source: userset; mso-width-alt: 11885;"> <col width="287" style="width: 215pt; mso-width-source: userset; mso-width-alt: 10496;"> <tbody>[TR]
[TD="width: 64, bgcolor: transparent"]COS
[/TD]
[TD="width: 325, bgcolor: transparent"][/TD]
[TD="width: 287, bgcolor: transparent"][/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"]COS[/TD]
[TD="bgcolor: transparent"][/TD]
[TD="bgcolor: transparent"]Purchase
[/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"]COS[/TD]
[TD="bgcolor: transparent"][/TD]
[TD="bgcolor: transparent"] Inventorys
[/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"]COS
[/TD]
[TD="bgcolor: transparent"][/TD]
[TD="bgcolor: transparent"]Inventory
[/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"][/TD]
[TD="bgcolor: transparent"][/TD]
[TD="bgcolor: transparent"] Inventory
[/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"][/TD]
[TD="bgcolor: transparent"][/TD]
[TD="bgcolor: transparent"] Stock
[/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"][/TD]
[TD="bgcolor: transparent"][/TD]
[TD="bgcolor: transparent"]Adjustments
[/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"][/TD]
[TD="bgcolor: transparent"][/TD]
[TD="bgcolor: transparent"]TOTAL COS
[/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"][/TD]
[TD="bgcolor: transparent"][/TD]
[TD="bgcolor: transparent"][/TD]
[/TR]
</tbody>[/TABLE]
I Need the first colum to continue down as "COS" and stop at "TOTAL COS".

Thank you
Hanz
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Try this:

Assuming we are dealing with column A and B

Code:
Sub Fill_Down()
'Modified 8/8/2018 5:55 AM  EDT
Application.ScreenUpdating = False
Dim Lastrow As Long
Lastrow = Cells(Rows.Count, "B").End(xlUp).Row
Range("A1").Resize(Lastrow).Value = "COS"
Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0
Thank you for responding to my email.

I forgot to meniton in my previous email, once "COS" is in line with "TOTAL COS". The second criteria would be to match "Sales" with "Total Sales".
 
Upvote 0
sorry for not being clear. Please see examle below:


[TABLE="width: 96"]
<colgroup><col width="64" style="width: 48pt;" span="2"> <tbody>[TR]
[TD="width: 64, bgcolor: transparent"]COS[/TD]
[TD="width: 64, bgcolor: transparent"]INVENTORY[/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"]COS[/TD]
[TD="bgcolor: transparent"]INVENTORY[/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"]COS[/TD]
[TD="bgcolor: transparent"]INVENTORY[/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"]COS[/TD]
[TD="bgcolor: transparent"]INVENTORY[/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"]COS[/TD]
[TD="bgcolor: transparent"]INVENTORY[/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"]COS[/TD]
[TD="bgcolor: transparent"]INVENTORY[/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"]COS[/TD]
[TD="bgcolor: transparent"]TOTAL COS[/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"]SALES[/TD]
[TD="bgcolor: transparent"]BONUS[/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"]SALES[/TD]
[TD="bgcolor: transparent"]BONUS
[/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"][/TD]
[TD="bgcolor: transparent"]BONUS[/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"][/TD]
[TD="bgcolor: transparent"]BONUS[/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"][/TD]
[TD="bgcolor: transparent"]TOTALSALE
[/TD]
[/TR]
</tbody>[/TABLE]

Once "COS" is in line with "TOTAL COS". The next step is to have "SALES" line with "TOTALSALE".

Thanks
Hanz
 
Upvote 0
Try this:
Code:
Sub Copy_Range()
'Modified 8/8/2018 8:20 AM  EDT
Application.ScreenUpdating = False
Dim i As Long
Dim Lastrow As Long
Lastrow = Cells(Rows.Count, "B").End(xlUp).Row
For i = 1 To Lastrow
If Cells(i, 2).Value = "INVENTORY" Then Cells(i, 2).Offset(, -1).Value = "COS"
If Cells(i, 2).Value = "TOTAL COS" Then Cells(i, 2).Offset(, -1).Value = "COS"
If Cells(i, 2).Value = "BONUS" Then Cells(i, 2).Offset(, -1).Value = "SALES"
If Cells(i, 2).Value = "TOTALSALE" Then Cells(i, 2).Offset(, -1).Value = "SALES"
Next
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Another option
Code:
Sub FillBlanks()
With Range("A:A")
   .SpecialCells(xlBlanks).Formula = "=r[-1]c"
   .Value = .Value
End With
End Sub
 
Upvote 0
Another option
Code:
Sub FillBlanks()
With Range("A:A")
   .SpecialCells(xlBlanks).Formula = "=r[-1]c"
   .Value = .Value
End With
End Sub

When I try this script all I get in column A is:
[TABLE="width: 150"]
<colgroup><col></colgroup><tbody>[TR]
[TD="align: center"]#REF![/TD]
[/TR]
</tbody>[/TABLE]
 
Upvote 0
Did you have a value in A1?
 
Upvote 0
Did you have a value in A1?


When I put COS in Range("A1")
It fills all 1.5 million rows in column A with COS

I really never like using script I do not understand.
I have no ideal what:
.SpecialCells(xlBlanks).Formula = "=r[-1]c"
.Value = .Value

I like using Excel Vba simple scripting language.
 
Upvote 0

Forum statistics

Threads
1,223,246
Messages
6,170,996
Members
452,373
Latest member
TimReeks

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