C
childofthe1980s
Hello:
I have four columns in an Excel spreadsheet. Column A ("Item Number") is a
list of inventory items. Column B ("Location Code") is to contain the phrase
"CH" in each cell of column B. Column C ("Order Point Qty") contains
quantities in each cell of column C. Column D ("Number of Days") contains
the number "10" in each cell of column D.
At the end of this posting is VBA code for a macro in Excel's Visual Basic
Editor that I am using to essentially create this spreadsheet. I am having
trouble with the following lines of code:
Range("B2:B" & Cells(Cells.Rows.Count, "A").End(xlUp).Row).FormulaR1C1 = "CH"
Range("C262:C" & Cells(Cells.Rows.Count, "A").End(xlUp).Row).FormulaR1C1
= "0"
Range("D2" & Cells(Cells.Rows.Count, "A").End(xlUp).Row).FormulaR1C1 =
"10"
You see, what I'm trying to accomplish is the following:
(1) Place "CH" in each cell of column B but only for as many cells as what
is filled in column A (i.e. only for as many cells as there are inventory
items in column A) and to do so only from B2 onward,
(2) Place the number "0" in cell C262 and in each succeeding cell of column
C afterward but again only for as many cells as what is filled in column A
(i.e. only for as many cells as there are inventory items in column A), and
(3) Place the number "10" in each cell of column D but only for as many
cells as what is filled in column A (i.e. only for as many cells as there are
inventory items in column A) and to do so only from D2 onward.
I thought that the three lines of code that I just mentioned woudld
accomplish this, but they did not.
Also. at the end of the code that i have attached I placed code to delete
Sheet1 of the workbook, but the macro did not do so. Why is that and how can
I fix it?
Finally, in other columns of the spreadsheet, I have done such things as
create formulas and place numeric cell formats. Similar to what I am trying
to do for the three columns that I mentioned a little while ago, what formula
do I use to say "hey, only do this for as many inventory items as what are in
column A?
Code follows:
ActiveWindow.LargeScroll ToRight:=1
ActiveWindow.SmallScroll ToRight:=-5
Range("L2:L" & Cells(Cells.Rows.Count, "J").End(xlUp).Row).FormulaR1C1 =
"=IF(RC[-2]<RC[-5], RC[-2]/RC[-5], 0)"
Columns("L:L").NumberFormat = "0%"
Range("L2:L" & Cells(Cells.Rows.Count, "J").End(xlUp).Row).Value =
Range("L2:L" & Cells(Cells.Rows.Count, "J").End(xlUp).Row).Value
Range("L1").Value = "% Below Min"
Application.CutCopyMode = False
Columns("L:L").EntireColumn.AutoFit
ActiveWindow.LargeScroll ToRight:=-1
Range("A1").Select
Selection.Subtotal GroupBy:=1, Function:=xlAverage, TotalList:=Array(4,
5, _
6, 7, 8, 9, 10, 11, 12), Replace:=True, PageBreaks:=False,
SummaryBelowData:= _
True
ActiveSheet.Outline.ShowLevels RowLevels:=2
Cells.Select
Selection.SpecialCells(xlCellTypeVisible).Select
Selection.Copy
Sheets("Sheet2").Select
Cells.Select
ActiveSheet.Paste
Selection.Columns.AutoFit
Range("A1").Select
Application.CutCopyMode = False
Range("A1:L632").Sort Key1:=Range("D2"), Order1:=xlDescending, Header:= _
xlGuess, OrderCustom:=1, MatchCase:=False,
Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
Columns("H:H").Select
Selection.Insert Shift:=xlToRight
Range("H2").Select
ActiveCell.FormulaR1C1 = "=RC[-1]*2"
Range("H2").Select
Selection.Copy
Columns("H:H").Select
ActiveSheet.Paste
Application.CutCopyMode = False
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Selection.NumberFormat = "0"
Range("H1").Select
ActiveCell.FormulaR1C1 = "Order Point Qty"
Columns("B:G").Select
Selection.Delete Shift:=xlToLeft
Columns("C:G").Select
Selection.Delete Shift:=xlToLeft
Columns("B:B").Select
Selection.Insert Shift:=xlToRight
Selection.Insert Shift:=xlToRight
Range("B2").Select
ActiveCell.FormulaR1C1 = "'=LEFT(A2,LEN(A2)-8)"
Range("B2").Select
ActiveCell.FormulaR1C1 = "=LEFT(RC[-1],LEN(RC[-1])-8)"
Range("B2").Select
Selection.Copy
Columns("B:B").Select
ActiveSheet.Paste
Application.CutCopyMode = False
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks _
:=False, Transpose:=False
Range("A1").Select
Application.CutCopyMode = False
Selection.Cut
Range("B1").Select
ActiveSheet.Paste
Columns("A:A").Select
Selection.Delete Shift:=xlToLeft
Range("B1").Select
ActiveCell.FormulaR1C1 = "Location Code"
Range("D1").Select
ActiveCell.FormulaR1C1 = "Number of Days"
Range("D1").Select
Columns("D").ColumnWidth = 15
Range("B2:B" & Cells(Cells.Rows.Count, "A").End(xlUp).Row).FormulaR1C1 =
"CH"
Range("C262:C" & Cells(Cells.Rows.Count, "A").End(xlUp).Row).FormulaR1C1
= "0"
Range("D2" & Cells(Cells.Rows.Count, "A").End(xlUp).Row).FormulaR1C1 =
"10"
Sheets("Sheet1").Select
ActiveWindow.SelectedSheets.Delete
Range("A1").Select
End Sub
I have four columns in an Excel spreadsheet. Column A ("Item Number") is a
list of inventory items. Column B ("Location Code") is to contain the phrase
"CH" in each cell of column B. Column C ("Order Point Qty") contains
quantities in each cell of column C. Column D ("Number of Days") contains
the number "10" in each cell of column D.
At the end of this posting is VBA code for a macro in Excel's Visual Basic
Editor that I am using to essentially create this spreadsheet. I am having
trouble with the following lines of code:
Range("B2:B" & Cells(Cells.Rows.Count, "A").End(xlUp).Row).FormulaR1C1 = "CH"
Range("C262:C" & Cells(Cells.Rows.Count, "A").End(xlUp).Row).FormulaR1C1
= "0"
Range("D2" & Cells(Cells.Rows.Count, "A").End(xlUp).Row).FormulaR1C1 =
"10"
You see, what I'm trying to accomplish is the following:
(1) Place "CH" in each cell of column B but only for as many cells as what
is filled in column A (i.e. only for as many cells as there are inventory
items in column A) and to do so only from B2 onward,
(2) Place the number "0" in cell C262 and in each succeeding cell of column
C afterward but again only for as many cells as what is filled in column A
(i.e. only for as many cells as there are inventory items in column A), and
(3) Place the number "10" in each cell of column D but only for as many
cells as what is filled in column A (i.e. only for as many cells as there are
inventory items in column A) and to do so only from D2 onward.
I thought that the three lines of code that I just mentioned woudld
accomplish this, but they did not.
Also. at the end of the code that i have attached I placed code to delete
Sheet1 of the workbook, but the macro did not do so. Why is that and how can
I fix it?
Finally, in other columns of the spreadsheet, I have done such things as
create formulas and place numeric cell formats. Similar to what I am trying
to do for the three columns that I mentioned a little while ago, what formula
do I use to say "hey, only do this for as many inventory items as what are in
column A?
Code follows:
ActiveWindow.LargeScroll ToRight:=1
ActiveWindow.SmallScroll ToRight:=-5
Range("L2:L" & Cells(Cells.Rows.Count, "J").End(xlUp).Row).FormulaR1C1 =
"=IF(RC[-2]<RC[-5], RC[-2]/RC[-5], 0)"
Columns("L:L").NumberFormat = "0%"
Range("L2:L" & Cells(Cells.Rows.Count, "J").End(xlUp).Row).Value =
Range("L2:L" & Cells(Cells.Rows.Count, "J").End(xlUp).Row).Value
Range("L1").Value = "% Below Min"
Application.CutCopyMode = False
Columns("L:L").EntireColumn.AutoFit
ActiveWindow.LargeScroll ToRight:=-1
Range("A1").Select
Selection.Subtotal GroupBy:=1, Function:=xlAverage, TotalList:=Array(4,
5, _
6, 7, 8, 9, 10, 11, 12), Replace:=True, PageBreaks:=False,
SummaryBelowData:= _
True
ActiveSheet.Outline.ShowLevels RowLevels:=2
Cells.Select
Selection.SpecialCells(xlCellTypeVisible).Select
Selection.Copy
Sheets("Sheet2").Select
Cells.Select
ActiveSheet.Paste
Selection.Columns.AutoFit
Range("A1").Select
Application.CutCopyMode = False
Range("A1:L632").Sort Key1:=Range("D2"), Order1:=xlDescending, Header:= _
xlGuess, OrderCustom:=1, MatchCase:=False,
Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
Columns("H:H").Select
Selection.Insert Shift:=xlToRight
Range("H2").Select
ActiveCell.FormulaR1C1 = "=RC[-1]*2"
Range("H2").Select
Selection.Copy
Columns("H:H").Select
ActiveSheet.Paste
Application.CutCopyMode = False
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Selection.NumberFormat = "0"
Range("H1").Select
ActiveCell.FormulaR1C1 = "Order Point Qty"
Columns("B:G").Select
Selection.Delete Shift:=xlToLeft
Columns("C:G").Select
Selection.Delete Shift:=xlToLeft
Columns("B:B").Select
Selection.Insert Shift:=xlToRight
Selection.Insert Shift:=xlToRight
Range("B2").Select
ActiveCell.FormulaR1C1 = "'=LEFT(A2,LEN(A2)-8)"
Range("B2").Select
ActiveCell.FormulaR1C1 = "=LEFT(RC[-1],LEN(RC[-1])-8)"
Range("B2").Select
Selection.Copy
Columns("B:B").Select
ActiveSheet.Paste
Application.CutCopyMode = False
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks _
:=False, Transpose:=False
Range("A1").Select
Application.CutCopyMode = False
Selection.Cut
Range("B1").Select
ActiveSheet.Paste
Columns("A:A").Select
Selection.Delete Shift:=xlToLeft
Range("B1").Select
ActiveCell.FormulaR1C1 = "Location Code"
Range("D1").Select
ActiveCell.FormulaR1C1 = "Number of Days"
Range("D1").Select
Columns("D").ColumnWidth = 15
Range("B2:B" & Cells(Cells.Rows.Count, "A").End(xlUp).Row).FormulaR1C1 =
"CH"
Range("C262:C" & Cells(Cells.Rows.Count, "A").End(xlUp).Row).FormulaR1C1
= "0"
Range("D2" & Cells(Cells.Rows.Count, "A").End(xlUp).Row).FormulaR1C1 =
"10"
Sheets("Sheet1").Select
ActiveWindow.SelectedSheets.Delete
Range("A1").Select
End Sub