I am wanting to insert columns and missing dates in row 1. Example: Starting in B1 and through to E1 (4 cells) I have Jan 1, Jan 2, Jan 5 and Jan 6. I want to insert columns and dates Jan 3 (D1) and Jan 4 (E1). This will put the 5th and 6th in cells F1 and G1 respectively. Column A is a list of names and the related rows are hours.
In this example, when I run the following code, “i” value stars with 7, tmp value starts at 0, then on the first pass through tmp it is 41,279.
Here is the code as a whole, with the problem lines noted at the end.
Public Sub InsertMissingDatesByColumn()
Const Date_Row As String = "1" '<=Row WITH DATES
Dim i As Long
Dim LastColumn As Long
Dim tmp As Long
Worksheets("Sheet1").Select
With ActiveSheet.UsedRange
LastColumn = .Columns.Count
Range("B1").Select
For i = LastColumn To 1 Step -1
If .Cells(Date_Row, i).Value <> .Cells(Date_Row, i + 1).Value And _
.Cells(Date_Row, i).Value < .Cells(Date_Row, i + 1).Value - 1 Then
tmp = .Cells(Date_Row, i + 1).Value
.Columns(i + 1).Resize(tmp - .Cells(Date_Row, i).Value - 1).Insert
.Cells(Date_Row, i).AutoFill .Cells(Date_Row, i).Resize(tmp - .Cells(Date_Row, i).Value)
End If
Next i
End With
End Sub
‘---------
‘after the following line runs, cells are placed in D1 (the date row) and D2
'(the first row containing a name and hours) and the previous info in D1 and D2
'is moved to the right one cell. Info in D3, D4, etc and below is not moved or changed.
.Columns(i + 1).Resize(tmp - .Cells(Date_Row, i).Value - 1).Insert
‘after the following line runs, the dates for the 3rd and 4th are inserted
'into cells C2 and C3 replacing the hours that were in them. The hours are
'not moved to the right in these cells, they are replaced by the dates.
.Cells(Date_Row, i).AutoFill .Cells(Date_Row, i).Resize(tmp - .Cells(Date_Row, i).Value)
‘--------------------------
Where am I going wrong?
TIA.
Bill
In this example, when I run the following code, “i” value stars with 7, tmp value starts at 0, then on the first pass through tmp it is 41,279.
Here is the code as a whole, with the problem lines noted at the end.
Public Sub InsertMissingDatesByColumn()
Const Date_Row As String = "1" '<=Row WITH DATES
Dim i As Long
Dim LastColumn As Long
Dim tmp As Long
Worksheets("Sheet1").Select
With ActiveSheet.UsedRange
LastColumn = .Columns.Count
Range("B1").Select
For i = LastColumn To 1 Step -1
If .Cells(Date_Row, i).Value <> .Cells(Date_Row, i + 1).Value And _
.Cells(Date_Row, i).Value < .Cells(Date_Row, i + 1).Value - 1 Then
tmp = .Cells(Date_Row, i + 1).Value
.Columns(i + 1).Resize(tmp - .Cells(Date_Row, i).Value - 1).Insert
.Cells(Date_Row, i).AutoFill .Cells(Date_Row, i).Resize(tmp - .Cells(Date_Row, i).Value)
End If
Next i
End With
End Sub
‘---------
‘after the following line runs, cells are placed in D1 (the date row) and D2
'(the first row containing a name and hours) and the previous info in D1 and D2
'is moved to the right one cell. Info in D3, D4, etc and below is not moved or changed.
.Columns(i + 1).Resize(tmp - .Cells(Date_Row, i).Value - 1).Insert
‘after the following line runs, the dates for the 3rd and 4th are inserted
'into cells C2 and C3 replacing the hours that were in them. The hours are
'not moved to the right in these cells, they are replaced by the dates.
.Cells(Date_Row, i).AutoFill .Cells(Date_Row, i).Resize(tmp - .Cells(Date_Row, i).Value)
‘--------------------------
Where am I going wrong?
TIA.
Bill