Hello all,
I need a macro which will look at two columns, for example B and C, and if there isn't a value in the cell in Column C, clear (or change the value to zero if it's easier) the adjacent cell in column B (i.e no value in C15 would clear B15). The macro would need to do this all the way down columns B and C, and then move on to columns D and E, and do the same thing, because my data is grouped in columns of two, and I have too many columns to filter them all manually.
I hope that someone can help me with this, I had rarely used Excel before starting my dissertation and i've been thrown in the deep end with it.
Thank you very much in advance,
Jacob
Last edited by jacobhandson; 12-17-2011 at 12:52 PM.
Hi jacobhandson
possibly something like...Sub ptest() Dim xCell As Range myarray = VBA.Array("C", "3", "E", "5") For xKey = 1 To 3 Step 2 For Each xCell In Range(myarry(xKey) & "1", Cells(Rows.Count, myarray(xKey + 1)).End(xlUp)) If IsEmpty(xCell.Value) Then xCell.Offset(0, -1).Value = "" Next xCell End Sub
regards pike
If the solution helped please donate here to the RSPCA
Sites worth visiting;
J&R Solutions - royUK
AJP Excel Information - Andy Pope
Spreadsheet Toolbox
VBA for smarties - snb
Hi, thanks for replying so promptly. Unfortunately I get the error "Compile error: For without next" and it highlights the 1st line "Sub ptest ()"
do you know why this might be?
thanks,
jacob
There was a small line missing. Added it in here -
Sub ptest() Dim xCell As Range myarray = VBA.Array("C", "3", "E", "5") For xKey = 1 To 3 Step 2 For Each xCell In Range(myarry(xKey) & "1", Cells(Rows.Count, myarray(xKey + 1)).End(xlUp)) If IsEmpty(xCell.Value) Then xCell.Offset(0, -1).Value = "" Next xCell Next xKey End Sub
Cheers,
Arlette
If I helped, Don't forget to add to my reputation (click on the star below the post)
Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
Use code tags when posting your VBA code: [code] Your code here [/code]
Thanks, unfortunately this has just created another error:
runtime error 1004: method 'range' of object'_global' failed.
There is a typo in this line -Change it toFor Each xCell In Range(myarry(xKey) & "1", Cells(Rows.Count, myarray(xKey + 1)).End(xlUp))
For Each xCell In Range(myarray(xKey) & "1", Cells(Rows.Count, myarray(xKey + 1)).End(xlUp))
Cheers,
Arlette
If I helped, Don't forget to add to my reputation (click on the star below the post)
Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
Use code tags when posting your VBA code: [code] Your code here [/code]
Thanks again Arlette, but I had noticed that and changed it already. any other suggestions as to why that might not work? it is highlighting that line when the error comes up...
I guess it would be better if Pike assisted you himself, since i tried figuring it out but couldn't. You could PM him to expedite a response.
Cheers,
Arlette
If I helped, Don't forget to add to my reputation (click on the star below the post)
Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
Use code tags when posting your VBA code: [code] Your code here [/code]
Hi jacobhandson
Ive test this code , the other i just typed out
Option Explicit Sub ptest() Dim xCell As Range Dim myarray Dim xkey As Integer myarray = VBA.Array("C", 3, "E", 5) For xkey = 0 To 3 Step 2 For Each xCell In Range(myarray(xkey) & "1", Cells(Rows.Count, myarray(xkey + 1)).End(xlUp)) If IsEmpty(xCell.Value) Then xCell.Offset(0, -1).Value = "" Next xCell Next xkey End Sub
regards pike
If the solution helped please donate here to the RSPCA
Sites worth visiting;
J&R Solutions - royUK
AJP Excel Information - Andy Pope
Spreadsheet Toolbox
VBA for smarties - snb
Perhaps
Sub test() Dim oneColumn As Range For Each oneColumn In Sheet1.Range("C1,E1").Columns With oneColumn.EntireColumn On Error Resume Next Application.Intersect(.Offset(0, -1), .SpecialCells(xlCellTypeBlanks).EntireRow).ClearContents On Error GoTo 0 End With Next oneColumn End Sub
_
...How to Cross-post politely...
..Wrap code by selecting the code and clicking the # or read this. Thank you.
Thanks, the first one (post #9) does what I want for columns B to E (but only to about row 178), is there any chance that you could tell me how to modify this to make it do the same thing all the way up to column YI please, and down to row 366? I assume it's meant to do the latter but it isnt?!
post #10 doesnt seem to work for me i'm afraid. thanks for the attempt nontheless![]()
Last edited by jacobhandson; 12-14-2011 at 09:09 AM.
Are the blank cells truely blank or do they have formulas that evaluate to "" ?
(eg =IF(A1=1, "cat", "") )
_
...How to Cross-post politely...
..Wrap code by selecting the code and clicking the # or read this. Thank you.
the cells are truely blank
Thanks,
Jacob
Can anyone help me to change the range of the code from post #9: I need it to iteratively perform the function over the full range of the data without me having to change "C" and "E" to other letters hundreds of times.
I've been told you can use currentregion property to find the range size without having to us end(xlup).
and that you can work on each column by using columns.count property eg
for each col in currentregion.columns
for each row in currentregion,rows
but again, i have no clue where to start to do this.
any help would be greatly appreciated,
Thanks,
Jacob
Last edited by jacobhandson; 12-16-2011 at 07:48 PM.
problem solved:
thanks for all helpSet rng= Range("B2:YI366") ' loop through each column in range For I = 1 To rng.Columns.Count ' perhaps Columns.Count-1 so YJ isnt' included ' loop through each cell in column For Each cl in rng.Columns(I).Cells If cl.Offset(,1) = "" Then cl.Clear End If Next cl Next I
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks