Code not cooperating

gheyman

Well-known Member
Joined
Nov 14, 2005
Messages
2,347
Office Version
  1. 365
Platform
  1. Windows
I have spent the last hour looking at this to see where the problem is.

I need to look across row 5, starting in column 6 (F) and if the its not Balnk look at column 5 and see if its value is not null. If it is Null, then delete whats in column 5

Now column 5 typically has values further to the right than column 6 does. Those are the cells I am trying to clear

Remove unneeded Dates in Row 5
'If the cell in row 6 is blank delete data in row 5
Dim i As Long
Dim LastCol As Long

With Sheets("Monthly Allocation Report")
LastCol = .Cells(5, .Columns.Count).End(xlUp).Column
End With

With Sheets("Monthly Allocation Report")
For i = 5 To LastCol
If Len(.Cells(6, i).Value) > 1 Then
.Cells(5, i).Clear
End If
Next i
End With
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
is this right ?
Code:
LastCol = .Cells(5, .Columns.Count).End(xlUp).Column
i know xlup is good for rows ?
 
Upvote 0
Yes, it should be

Code:
LastCol = .Cells(5, .Columns.Count).End(xlToLeft).Column
 
Upvote 0
I tried changing that to End(xlToRight). But it didnt fix the problem.

I have data in Cells F5 through BB5
and then data in Cells F6 through AN6

when I run the code it is deleting data in cells N5 through AN5. Its leaving in all the data in column 5 after cell AN5.

I am baffled. And I am stuck!
 
Upvote 0
Also tried the LastCol = .Cells(5, .Columns.Count).End(xlToLeft).Column

It delets some of the data in row 5 but not all of it. it only deletes it from N through AN. I dont see a logical reason for that. the cells after AN have data in rows 5 and 6. Why this is deleteing only from that range is very very stranges to me!!
 
Upvote 0
Try looping backwards

Code:
For i = lastcol To 5 Step -1
    If Len(.Cells(6, i).Value) > 1 Then
        .Cells(5, i).Clear
    End If
Next i
 
Upvote 0

Forum statistics

Threads
1,224,616
Messages
6,179,911
Members
452,949
Latest member
beartooth91

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