Michael151
Board Regular
- Joined
- Sep 20, 2010
- Messages
- 247
Hello all,
Just need a little help modifying my code for a macro. I’d like the macro to only perform the function (in this case moving info between cells) if the cell contains text. If the cell is blank, don’t move or do anything. The code below allows me to move the dates in HBStartDate and HBEndDate into the Start Date and End Date columns if two sequential titles match – in this case, Title2 in the example below. However, if the cells in HBStartDate and HBEndDate are blank, it will move the blank cells into Start Date and End Date, erasing the text (if any that is already there).
If there is anyway to write in a section of the code that says something like IF “*” or blank cell is found, then do not move dates.
Help is appreciated – thank you!
Before:
<table border="0" cellpadding="0" cellspacing="0" width="377"><col style="width: 48pt;" width="64"> <col style="width: 65pt;" width="86"> <col style="width: 53pt;" width="70"> <col style="width: 70pt;" width="93"> <col style="width: 48pt;" width="64"> <tbody><tr style="height: 12.75pt;" height="17"> <td class="xl24" style="height: 12.75pt; width: 48pt;" width="64" height="17">Title</td> <td class="xl24" style="width: 65pt;" width="86">HB Start Date</td> <td class="xl24" style="width: 53pt;" width="70">HB End Date</td> <td class="xl24" style="width: 70pt;" width="93">Start Date</td> <td class="xl24" style="width: 48pt;" width="64">End Date</td> </tr> <tr style="height: 12.75pt;" height="17"> <td style="height: 12.75pt;" height="17">Title1</td> <td>
</td> <td>
</td> <td>
</td> <td>
</td> </tr> <tr style="height: 12.75pt;" height="17"> <td style="height: 12.75pt;" height="17">Title2</td> <td>date1</td> <td>date2</td> <td>
</td> <td>
</td> </tr> <tr style="height: 12.75pt;" height="17"> <td style="height: 12.75pt;" height="17">Title2</td> <td>date1</td> <td>date2</td> <td>
</td> <td>
</td> </tr> <tr style="height: 12.75pt;" height="17"> <td style="height: 12.75pt;" height="17">Title3</td> <td>
</td> <td>
</td> <td>
</td> <td>
</td> </tr> <tr style="height: 12.75pt;" height="17"> <td style="height: 12.75pt;" height="17">Title3</td> <td>
</td> <td>
</td> <td>
</td> <td>
</td> </tr> <tr style="height: 12.75pt;" height="17"> <td style="height: 12.75pt;" height="17">
</td> <td>
</td> <td>
</td> <td>
</td> <td>
</td> </tr> </tbody></table>After:
<table border="0" cellpadding="0" cellspacing="0" width="377"><col style="width: 48pt;" width="64"> <col style="width: 65pt;" width="86"> <col style="width: 53pt;" width="70"> <col style="width: 70pt;" width="93"> <col style="width: 48pt;" width="64"> <tbody><tr style="height: 12.75pt;" height="17"> <td class="xl24" style="height: 12.75pt; width: 48pt;" width="64" height="17">Title</td> <td class="xl24" style="width: 65pt;" width="86">HB Start Date</td> <td class="xl24" style="width: 53pt;" width="70">HB End Date</td> <td class="xl24" style="width: 70pt;" width="93">Start Date</td> <td class="xl24" style="width: 48pt;" width="64">End Date</td> </tr> <tr style="height: 12.75pt;" height="17"> <td style="height: 12.75pt;" height="17">Title1</td> <td>
</td> <td>
</td> <td>
</td> <td>
</td> </tr> <tr style="height: 12.75pt;" height="17"> <td style="height: 12.75pt;" height="17">Title2</td> <td>date1</td> <td>date2</td> <td>date1</td> <td>date2</td> </tr> <tr style="height: 12.75pt;" height="17"> <td style="height: 12.75pt;" height="17">Title2</td> <td>date1</td> <td>date2</td> <td>date1</td> <td>date2</td> </tr> <tr style="height: 12.75pt;" height="17"> <td style="height: 12.75pt;" height="17">Title3</td> <td>
</td> <td>
</td> <td>
</td> <td>
</td> </tr> <tr style="height: 12.75pt;" height="17"> <td style="height: 12.75pt;" height="17">Title3</td> <td>
</td> <td>
</td> <td>
</td> <td>
</td> </tr> </tbody></table>
Code:
Option Explicit
Sub test()
Dim iTitlerow As Long, iTitlecol As Integer, iHBStartDatecol As Integer, iHBEndDatecol As Integer, iStartDatecol As Integer, _
iEndDatecol As Integer, myrange, i As Long, x As Integer, y As Integer
With ActiveSheet: On Error Resume Next: Application.ScreenUpdating = False
iTitlerow = .UsedRange.Find("Title", , xlValues, xlWhole).Row: iTitlecol = .UsedRange.Find("Title", , xlValues, xlWhole).Column
iHBStartDatecol = .UsedRange.Find("HB Start Date", , xlValues, xlWhole).Column
iHBEndDatecol = .UsedRange.Find("HB End Date", , xlValues, xlWhole).Column
iStartDatecol = .UsedRange.Find("Start Date", , xlValues, xlWhole).Column
iEndDatecol = .UsedRange.Find("End Date", , xlValues, xlWhole).Column
Set myrange = Range(.Cells(iTitlerow, iTitlecol), .Cells(iTitlerow, iTitlecol).End(xlDown))
If Not myrange Is Nothing Then
For i = 2 To myrange.Cells.Count
If myrange(i).Offset(1, 0).Value = myrange(i).Value Then x = 1
If myrange(i).Offset(2, 0) = myrange(i).Value Then y = 1
If x = 1 And y = 0 Then
.Cells(myrange(i).Row, iStartDatecol) = .Cells(myrange(i).Row, iHBStartDatecol)
.Cells(myrange(i).Row, iEndDatecol) = .Cells(myrange(i).Row, iHBEndDatecol)
End If
Next
End If
End With
End Sub
Problem:
Before:
<table border="0" cellpadding="0" cellspacing="0" width="377"><col style="width: 48pt;" width="64"> <col style="width: 65pt;" width="86"> <col style="width: 53pt;" width="70"> <col style="width: 70pt;" width="93"> <col style="width: 48pt;" width="64"> <tbody><tr style="height: 12.75pt;" height="17"> <td class="xl24" style="height: 12.75pt; width: 48pt;" width="64" height="17">Title</td> <td class="xl24" style="width: 65pt;" width="86">HB Start Date</td> <td class="xl24" style="width: 53pt;" width="70">HB End Date</td> <td class="xl24" style="width: 70pt;" width="93">Start Date</td> <td class="xl24" style="width: 48pt;" width="64">End Date</td> </tr> <tr style="height: 12.75pt;" height="17"> <td style="height: 12.75pt;" height="17">Title1</td> <td>
</td> <td>
</td> <td>
</td> <td>
</td> </tr> <tr style="height: 12.75pt;" height="17"> <td style="height: 12.75pt;" height="17">Title2</td> <td>
</td> <td>
</td> <td>DateY</td> <td>DateK</td> </tr> <tr style="height: 12.75pt;" height="17"> <td style="height: 12.75pt;" height="17">Title2</td> <td>
</td> <td>
</td> <td>DateG</td> <td>DateM</td> </tr> <tr style="height: 12.75pt;" height="17"> <td style="height: 12.75pt;" height="17">Title3</td> <td>
</td> <td>
</td> <td>
</td> <td>
</td> </tr> <tr style="height: 12.75pt;" height="17"> <td style="height: 12.75pt;" height="17">Title3</td> <td>
</td> <td>
</td> <td>
</td> <td>
</td> </tr> </tbody></table>
After:
<table border="0" cellpadding="0" cellspacing="0" width="377"><col style="width: 48pt;" width="64"> <col style="width: 65pt;" width="86"> <col style="width: 53pt;" width="70"> <col style="width: 70pt;" width="93"> <col style="width: 48pt;" width="64"> <tbody><tr style="height: 12.75pt;" height="17"> <td class="xl24" style="height: 12.75pt; width: 48pt;" width="64" height="17">Title</td> <td class="xl24" style="width: 65pt;" width="86">HB Start Date</td> <td class="xl24" style="width: 53pt;" width="70">HB End Date</td> <td class="xl24" style="width: 70pt;" width="93">Start Date</td> <td class="xl24" style="width: 48pt;" width="64">End Date</td> </tr> <tr style="height: 12.75pt;" height="17"> <td style="height: 12.75pt;" height="17">Title1</td> <td>
</td> <td>
</td> <td>
</td> <td>
</td> </tr> <tr style="height: 12.75pt;" height="17"> <td style="height: 12.75pt;" height="17">Title2</td> <td>
</td> <td>
</td> <td>
</td> <td>
</td> </tr> <tr style="height: 12.75pt;" height="17"> <td style="height: 12.75pt;" height="17">Title2</td> <td>
</td> <td>
</td> <td>
</td> <td>
</td> </tr> <tr style="height: 12.75pt;" height="17"> <td style="height: 12.75pt;" height="17">Title3</td> <td>
</td> <td>
</td> <td>
</td> <td>
</td> </tr> <tr style="height: 12.75pt;" height="17"> <td style="height: 12.75pt;" height="17">Title3</td> <td>
</td> <td>
</td> <td>
</td> <td>
</td> </tr> </tbody></table>
Macro will bring blank cells over into Start and End Date columns, erasing data that is already there. If cell is empty in HBStartDate and HBEndDate columns, then do not move blank cells.
Thanks!
Just need a little help modifying my code for a macro. I’d like the macro to only perform the function (in this case moving info between cells) if the cell contains text. If the cell is blank, don’t move or do anything. The code below allows me to move the dates in HBStartDate and HBEndDate into the Start Date and End Date columns if two sequential titles match – in this case, Title2 in the example below. However, if the cells in HBStartDate and HBEndDate are blank, it will move the blank cells into Start Date and End Date, erasing the text (if any that is already there).
If there is anyway to write in a section of the code that says something like IF “*” or blank cell is found, then do not move dates.
Help is appreciated – thank you!
Before:
<table border="0" cellpadding="0" cellspacing="0" width="377"><col style="width: 48pt;" width="64"> <col style="width: 65pt;" width="86"> <col style="width: 53pt;" width="70"> <col style="width: 70pt;" width="93"> <col style="width: 48pt;" width="64"> <tbody><tr style="height: 12.75pt;" height="17"> <td class="xl24" style="height: 12.75pt; width: 48pt;" width="64" height="17">Title</td> <td class="xl24" style="width: 65pt;" width="86">HB Start Date</td> <td class="xl24" style="width: 53pt;" width="70">HB End Date</td> <td class="xl24" style="width: 70pt;" width="93">Start Date</td> <td class="xl24" style="width: 48pt;" width="64">End Date</td> </tr> <tr style="height: 12.75pt;" height="17"> <td style="height: 12.75pt;" height="17">Title1</td> <td>
</td> <td>
</td> <td>
</td> <td>
</td> </tr> <tr style="height: 12.75pt;" height="17"> <td style="height: 12.75pt;" height="17">Title2</td> <td>date1</td> <td>date2</td> <td>
</td> <td>
</td> </tr> <tr style="height: 12.75pt;" height="17"> <td style="height: 12.75pt;" height="17">Title2</td> <td>date1</td> <td>date2</td> <td>
</td> <td>
</td> </tr> <tr style="height: 12.75pt;" height="17"> <td style="height: 12.75pt;" height="17">Title3</td> <td>
</td> <td>
</td> <td>
</td> <td>
</td> </tr> <tr style="height: 12.75pt;" height="17"> <td style="height: 12.75pt;" height="17">Title3</td> <td>
</td> <td>
</td> <td>
</td> <td>
</td> </tr> <tr style="height: 12.75pt;" height="17"> <td style="height: 12.75pt;" height="17">
</td> <td>
</td> <td>
</td> <td>
</td> <td>
</td> </tr> </tbody></table>After:
<table border="0" cellpadding="0" cellspacing="0" width="377"><col style="width: 48pt;" width="64"> <col style="width: 65pt;" width="86"> <col style="width: 53pt;" width="70"> <col style="width: 70pt;" width="93"> <col style="width: 48pt;" width="64"> <tbody><tr style="height: 12.75pt;" height="17"> <td class="xl24" style="height: 12.75pt; width: 48pt;" width="64" height="17">Title</td> <td class="xl24" style="width: 65pt;" width="86">HB Start Date</td> <td class="xl24" style="width: 53pt;" width="70">HB End Date</td> <td class="xl24" style="width: 70pt;" width="93">Start Date</td> <td class="xl24" style="width: 48pt;" width="64">End Date</td> </tr> <tr style="height: 12.75pt;" height="17"> <td style="height: 12.75pt;" height="17">Title1</td> <td>
</td> <td>
</td> <td>
</td> <td>
</td> </tr> <tr style="height: 12.75pt;" height="17"> <td style="height: 12.75pt;" height="17">Title2</td> <td>date1</td> <td>date2</td> <td>date1</td> <td>date2</td> </tr> <tr style="height: 12.75pt;" height="17"> <td style="height: 12.75pt;" height="17">Title2</td> <td>date1</td> <td>date2</td> <td>date1</td> <td>date2</td> </tr> <tr style="height: 12.75pt;" height="17"> <td style="height: 12.75pt;" height="17">Title3</td> <td>
</td> <td>
</td> <td>
</td> <td>
</td> </tr> <tr style="height: 12.75pt;" height="17"> <td style="height: 12.75pt;" height="17">Title3</td> <td>
</td> <td>
</td> <td>
</td> <td>
</td> </tr> </tbody></table>
Code:
Option Explicit
Sub test()
Dim iTitlerow As Long, iTitlecol As Integer, iHBStartDatecol As Integer, iHBEndDatecol As Integer, iStartDatecol As Integer, _
iEndDatecol As Integer, myrange, i As Long, x As Integer, y As Integer
With ActiveSheet: On Error Resume Next: Application.ScreenUpdating = False
iTitlerow = .UsedRange.Find("Title", , xlValues, xlWhole).Row: iTitlecol = .UsedRange.Find("Title", , xlValues, xlWhole).Column
iHBStartDatecol = .UsedRange.Find("HB Start Date", , xlValues, xlWhole).Column
iHBEndDatecol = .UsedRange.Find("HB End Date", , xlValues, xlWhole).Column
iStartDatecol = .UsedRange.Find("Start Date", , xlValues, xlWhole).Column
iEndDatecol = .UsedRange.Find("End Date", , xlValues, xlWhole).Column
Set myrange = Range(.Cells(iTitlerow, iTitlecol), .Cells(iTitlerow, iTitlecol).End(xlDown))
If Not myrange Is Nothing Then
For i = 2 To myrange.Cells.Count
If myrange(i).Offset(1, 0).Value = myrange(i).Value Then x = 1
If myrange(i).Offset(2, 0) = myrange(i).Value Then y = 1
If x = 1 And y = 0 Then
.Cells(myrange(i).Row, iStartDatecol) = .Cells(myrange(i).Row, iHBStartDatecol)
.Cells(myrange(i).Row, iEndDatecol) = .Cells(myrange(i).Row, iHBEndDatecol)
End If
Next
End If
End With
End Sub
Problem:
Before:
<table border="0" cellpadding="0" cellspacing="0" width="377"><col style="width: 48pt;" width="64"> <col style="width: 65pt;" width="86"> <col style="width: 53pt;" width="70"> <col style="width: 70pt;" width="93"> <col style="width: 48pt;" width="64"> <tbody><tr style="height: 12.75pt;" height="17"> <td class="xl24" style="height: 12.75pt; width: 48pt;" width="64" height="17">Title</td> <td class="xl24" style="width: 65pt;" width="86">HB Start Date</td> <td class="xl24" style="width: 53pt;" width="70">HB End Date</td> <td class="xl24" style="width: 70pt;" width="93">Start Date</td> <td class="xl24" style="width: 48pt;" width="64">End Date</td> </tr> <tr style="height: 12.75pt;" height="17"> <td style="height: 12.75pt;" height="17">Title1</td> <td>
</td> <td>
</td> <td>
</td> <td>
</td> </tr> <tr style="height: 12.75pt;" height="17"> <td style="height: 12.75pt;" height="17">Title2</td> <td>
</td> <td>
</td> <td>DateY</td> <td>DateK</td> </tr> <tr style="height: 12.75pt;" height="17"> <td style="height: 12.75pt;" height="17">Title2</td> <td>
</td> <td>
</td> <td>DateG</td> <td>DateM</td> </tr> <tr style="height: 12.75pt;" height="17"> <td style="height: 12.75pt;" height="17">Title3</td> <td>
</td> <td>
</td> <td>
</td> <td>
</td> </tr> <tr style="height: 12.75pt;" height="17"> <td style="height: 12.75pt;" height="17">Title3</td> <td>
</td> <td>
</td> <td>
</td> <td>
</td> </tr> </tbody></table>
After:
<table border="0" cellpadding="0" cellspacing="0" width="377"><col style="width: 48pt;" width="64"> <col style="width: 65pt;" width="86"> <col style="width: 53pt;" width="70"> <col style="width: 70pt;" width="93"> <col style="width: 48pt;" width="64"> <tbody><tr style="height: 12.75pt;" height="17"> <td class="xl24" style="height: 12.75pt; width: 48pt;" width="64" height="17">Title</td> <td class="xl24" style="width: 65pt;" width="86">HB Start Date</td> <td class="xl24" style="width: 53pt;" width="70">HB End Date</td> <td class="xl24" style="width: 70pt;" width="93">Start Date</td> <td class="xl24" style="width: 48pt;" width="64">End Date</td> </tr> <tr style="height: 12.75pt;" height="17"> <td style="height: 12.75pt;" height="17">Title1</td> <td>
</td> <td>
</td> <td>
</td> <td>
</td> </tr> <tr style="height: 12.75pt;" height="17"> <td style="height: 12.75pt;" height="17">Title2</td> <td>
</td> <td>
</td> <td>
</td> <td>
</td> </tr> <tr style="height: 12.75pt;" height="17"> <td style="height: 12.75pt;" height="17">Title2</td> <td>
</td> <td>
</td> <td>
</td> <td>
</td> </tr> <tr style="height: 12.75pt;" height="17"> <td style="height: 12.75pt;" height="17">Title3</td> <td>
</td> <td>
</td> <td>
</td> <td>
</td> </tr> <tr style="height: 12.75pt;" height="17"> <td style="height: 12.75pt;" height="17">Title3</td> <td>
</td> <td>
</td> <td>
</td> <td>
</td> </tr> </tbody></table>
Macro will bring blank cells over into Start and End Date columns, erasing data that is already there. If cell is empty in HBStartDate and HBEndDate columns, then do not move blank cells.
Thanks!