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.


COS
COSPurchase
COS Inventorys
COS
Inventory
Inventory
Stock
Adjustments
TOTAL COS
<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> </tbody>
I Need the first colum to continue down as "COS" and stop at "TOTAL COS".

Thank you
Hanz
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
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:


COSINVENTORY
COSINVENTORY
COSINVENTORY
COSINVENTORY
COSINVENTORY
COSINVENTORY
COSTOTAL COS
SALESBONUS
SALESBONUS
BONUS
BONUS
TOTALSALE
<colgroup><col width="64" style="width: 48pt;" span="2"> <tbody> </tbody>

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:
#REF!

<colgroup><col></colgroup><tbody>
</tbody>
 
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,214,943
Messages
6,122,369
Members
449,080
Latest member
Armadillos

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