I have a column in excel sheet that contains the IF condition i.e.
=If(Cond 1 is TRUE, X, Y)
Now, after using this condition, i get certain values in the column. Following format can be considered (these are actual values):
4L
4L
4L
4L
Note: The two empty cells in the above col are an outcome of the TRUE condition(entry 4 and 5, i entered total 6 entries, two are empty cells ). Therefore, they are valid. (let me call the above col "A" for future reference)
Now, these empty cells actually contains formulas (the if condition). I want to CLEARCONTENT them using VBA but it is not working.
And I'm trying the below code:
If ActiveSheet.Cells(row_no, col_no) = "" Then
ActiveSheet.Cells(row_no, col_no).ClearContents
End If
But this does not work. I just want to CLEAR CONTENT those empty cells by running a loop over the whole column. The cells of the column where TEXT exist (i.e. 4L), that should be skipped but as soon the code encounters the EMPTY CELL (which actually have an IF condition), it should CLEAR CONTENT it and complete the loop. So the final result would be again the same as column "A", the only difference would be that the empty cells will be BLANK now i.e. they will not have any IF condition.
I do not have problem running the loops but i am not getting how to tell VBA about that empty cell and clear contenting it. Hopefully i was able to post a clear query. Thanking in advance.
Regards
Nayyar
Please try the below sample code it should work..
Sub test()
'Assuming your data in column A from A2
Set Rng = Range("A2", Cells(Rows.Count, 1).End(xlUp))
For Each cell In Rng
If cell.Value = "" Then
cell.ClearContents
End If
Next
End Sub
And in your formula =If(Cond 1 is TRUE, X, Y) your not giving any output like "" which will give you blank. Please update it and then try :)
Try this as well
If (Range("A35").Value = "") Then
Range("A35").Formula = ""
End If
Related
I am trying to input a value into the next empty row in a column.
I tried using rows.count and off setting it like you would with a paste but it doesn't work. I was able to get it to change the value in the first cell but not to look for the next empty and change its value.
Private Sub CommandButton1_Click()
Dim wbCount As Workbook
Set wbEntry = ThisWorkbook
wbEntry.ActiveSheet.Range("B3").Copy
Set wbCount = Workbooks.Open("U:\MO# Count.xlsm")
Worksheets("Golf Cart").Paste _
Destination:=Worksheets("Golf Cart").Range("V5")
Worksheets("Golf Cart").Range("C2").Value = 1
wbEntry.Activate
Application.CutCopyMode = False
wbEntry.ActiveSheet.Range("O1").Select
End Sub
What I have works except for not searching for the next empty cell in "C" and changing the value to "1".
I'm confused, why would you think it searches for an empty cell?
Leaving the rest of the macro alone for now, this line:
Worksheets("Golf Cart").Range("C2").Value = 1
Does not search for an empty cell, it just writes a "1" into Cell C2. If you would like it to find the first empty cell in column C and then write a 1 into it, then this should do the trick:
Worksheets("Golf Cart").Cells(Sheets("Golf Cart").Cells(Rows.Count, 3).End(xlUp).Row + 1, 3) = 1
I'm not sure thats what you want tho, please clarify. This finds the last used row in column C and then goes to the next one down to write a "1" into it. It doesnt work if you want to find empty cells in an otherwise value-filled column. But you wrote something about rows.count or something, so I thought I'd post that way?
I'm a rookie at vba.
I created a solution that works on a micro basis, but can't get the same code to run when I add additional parameters to that code.
I created a multiple choice quiz with answer cells which test against the answers on a separate (hidden) sheet. An adjacent cell shows Yes or No response. That functionality which I researched on the web works well. I'm trying to clear.contents for all the cells so that the user can complete the quiz, clear all responses to give it another go.
I have 395 questions which I created code to clear "Range("B2").ClearContents"
My subroutine has 395 such commands and takes 45-60 seconds. I'm looking for a more efficient solution. I don't want to clear the entire column only the 395 non-adjacent cells which have user input.
Sub Test_Clear()
Range("B2,B5,B7,B9,B11").ClearContents
End Sub
My list of cells is on Sheet2 D1:D395 Each cell in D1:D395 lists a cell on Sheet1 which I want to clear (i.e. B6, B11, B17, B22, B35 etc.) How can I reference those cells on Sheet2 and perform the ClearContents?
While you could just hard code your list in VBA instead of your Sheet2 range, here is how you could reference that list of cells in sheet2 and clear the contents all at once:
Sub clearall()
Dim rngCell, listCells As String
For Each rngCell In Sheet2.Range("D1:D395").Cells
If listCell <> "" Then listCell = listCell & "," & rngCell.Value Else listCell = rngCell.Value
Next
Sheet2.Range(listCell).ClearContents
End Sub
If you just want to clear the cells in column A which have something in them you could use this
Sheet1.Range("A:A").SpecialCells(xlCellTypeConstants).ClearContents
'or
Sheet1.Range("A1:A395").SpecialCells(xlCellTypeConstants).ClearContents
The VBA Join function can be used to join values into string, and the Excel Transpose function is needed to "flip" the column values 2D array to a 1D row values array:
stringAdress = Join([Transpose(Sheet2!D1:D395)], ",")
ThisWorkbook.Worksheets("Sheet1").Range(stringAdress).ClearContents
If any of the cells in the Sheet2!D1:D395 range are blank, the above will result in error.
In Excel 2016, the TextJoin function can be used to ignore empty cells (not tested):
stringAdress = [TextJoin(",", True, Sheet2!D1:D395)]
ThisWorkbook.Worksheets("Sheet1").Range(stringAdress).ClearContents
I have a list of data with various different columns.
What I am trying to achieve is for excel to look through one of the columns and find the first value that occurs, copy that cell and then paste it in another cell. The data starts in row three (Rows 1 and 2 are headers). Let's say I want to paste the first value in G3.
Then, I need excel to find the next value that is different from the first in the same column and perform the same action as before: copy the value, and paste it in the next row in column G.
I have tried coding this but I'm not getting anywhere with it, and I haven't found a way to find a cell and then the next cell if the value within the cell is not defined (as the .Find function requires a value to search for). I know how to code the copy/paste functions but I cannot figure out how to get it to find the cell in the first place.
Any help would be much appreciated. Many thanks in advance.
In this sample, we are looking for values in column A:
Sub FillG()
Dim i As Long
i = Rows.Count
Range("A3:A" & i).Copy Range("G3:G" & i)
ActiveSheet.Range("G3:G" & i).RemoveDuplicates Columns:=1, Header:=xlNo
If Range("G3").Value = "" Then Range("G3").Delete Shift:=xlUp
End Sub
The method is to copy all of column A to G, then remove duplicates, then remove an empty in G3 if it exists.:
Hi guys this is my first post, I'm wondering if you can possibly assist me.
I'd like to write a macro / script that will allow me to put a formula into the column to the right of the currently selected one (for all active rows of the current column) based on what column I've selected. The issue I'm having is that I don't always know the current column letter (as my selection changes from worksheet to worksheet).
To give you an example:
One of my columns currently contains dates, that dates are entered in different formats though, some are separated with ".", some with "-", some with spaces and so on. I have a formula that will deal with this so I need to put this formula in the column to the right of the selected column (which has the dates).
I have been able to do this when I specify the column letter, but not if it changes.
Please can you help?
Give this a go,
Sub SomethingNeat()
Dim rng As Range, x
x = Selection.Column
On Error Resume Next
Set rng = Columns(x).SpecialCells(xlCellTypeConstants, 23)
If Not rng Is Nothing Then rng.Offset(, 1) = "'=MyFormula"
End Sub
You can use ActiveCell.Offset(0,1).Value = Variable
That means that whetever your current cell is you can move and "select" to put a value to the right cell of the one you have activated. You can move the selection using a loop.
Do
Workbooks("Yur workbook name").Worksheets(1).Range(Adress you want to start adding).Offset(0, 1).formula = "=FORMULA"
i = i + 1
ActiveCell.Offset(1, 0).Activate
Loop While i <= max_row
Edit: 2nd
Put the formula in a cell lets say C1
'Select a range
Set take = Worksheets(1).Range("C1")
take.Copy 'copy the formula
Worksheets(1).Paste Destination:=Worksheets(1).Range("B1:B10")
That will copy your function whenever you want it to
I'm using VBA to do some further formatting to a generated CSV file that's always in the same format. I have a problem with my For Each Loop. the loop deletes an entire row if there is more than one blank row which can be determined from the first column alone.
Dim rowCount As Integer
For Each cell In Columns("A").Cells
rowCount = rowCount + 1
'
' Delete blank row
'
If cell = "" And cell.Offset(1, 0) = "" Then
Rows(rowCount + 1).EntireRow.Delete
spaceCount = 0
End If
Next
At some point the value in the loop one of the calls does not have a value of "", it's just empty and causes it to crash. To solve this I think that changing the type of the cell to text before that compare would work but I can't figure out how (no intellisense!!!)
So how do you convert a cell type in VBA or how else would I solve the problem?
Thanks.
Use cell.Value instead of the cell.Text as it will evaluate the value of the cell regardless of the formating. Press F1 over .Value and .Text to read more about both.
Be carefull with the statement For Each cell In Columns("A").Cells as you will test every row in the sheet (over a million in Excel 2010) and it could make Excel to crash.
Edit:
Consider also the funcion TRIM. It removes every empty space before and after a string. If in the cell there is a white space " "; it will look like empty for the human eye, but it has a space inside therefore is different than "". If you want to treat it like an empty cell, then try:
If Trim(cell.value) = "" then
As #andy (https://stackoverflow.com/users/1248931/andy-holaday) said in a comment, For Each is definitely the way to go. This even allows for there to be spaces in between lines.
Example code:
Sub ListFirstCol()
Worksheets("Sheet1").Activate
Range("A1").Activate
For Each cell In Application.Intersect(Range("A:A"), Worksheets("Sheet1").UsedRange)
MsgBox (cell)
Next
End Sub
Thanks Andy!