How do I clear content in rows based on cell value? - excel

I copy a lot of information (1000 rows and at least 24 columns) from one sheet to another. A lot of the cells contains "". This makes my other formulas(for example: A1-B1) to show an value error if either of these cells contains "".
I believe I can solve the problem by never pasting "" but a "0" instead. But I would like to delete these "0" afterwards.
There could be values in the first 3 rows but the other 997 rows have "".
I would think I need to tell my macro to (Cell A1 in the "sheet1" sheet displays "G5:H12". the cells I need to delete):
Rowstodelete = Sheets("sheet1").Range("A1").Value
Sheets("sheet1").Range("rowstodelete").clearcontent
This does not work. anyone know how to do this?
Summary(new example)
If cell A1 = "B1:B2" I want to clear the content of B1 and B2, but if A1 now = B4:B6, that is the cells that should be cleared.

Try this one:
With Worksheets(1).Range( _'PLACE YOUR RANGE
)
Set c = .Find(0, lookin:=xlValues)
If Not c Is Nothing Then
firstAddress = c.Address
Do
c.Value = ""
Set c = .FindNext(c)
Loop While Not c Is Nothing
End If
End With
Hope it helps
Anyhow, I think that will be simpler to place a condition in your operation formula: =IF(OR(A1="",B1=""),Make when there is an unexpected value,A1-B1)

Related

VBA Excel - Offset with merged cells

Yes, you maybe think, why the hell did he even use merged cells. Let me explain:
We receive the insurance periods from our insured persons from various companies around the world in Excel format. I have to read this data automatically, but I have the problem that these Excel files contain merged cells. These are always merged differently. However, what is always the same is that the next cells to the right of the starting cell contain the desired information. So I would need a code to always determine the data to the right of the starting cell, but considered that they are merged cells. Can .offset do this?
Example:
A5:C5 merged, D5 not merged, E5:H5 merged, I5:P5 merged
--> I need the data from D5, E5 and I5 (cells to the right of it)
For the next insured, the same data is formatted as follows:
A5:B5 merged, C5:F5 merged, G5:J5 merged, K5:O5 merged
--> I need the data from C5, G5 and K5 (cells to the right of it)
So it's always the 3 cells to the right of it, but right in terms of merged cells.
Can someone help me? Thanks!
Update: This is what i tried, from start c is AN87 and the NewAddress gives me AM87 even though AK87:AM87 is merged.
Dim c As Range
Dim firstAddress As String
With Workbooks(Left(myworksheet.Range("E10").Value, Len(myworksheet.Range("E10").Value) - 4) & ".xlsx").Worksheets("Sheet1").Range("A1:AY1000")
Set c = .Find(myworksheet.Range("E11").Value, LookIn:=xlValues, Lookat:=xlWhole)
If Not c Is Nothing Then
ElzPeter = c.Address
MsgBox ElzPeter
End If
End With
Dim MA As Range, NewAddress As String
Set MA = c.MergeArea
NewAddress = MA.Offset(, -1).Resize(MA.Rows.Count).Address
MsgBox NewAddress
If you have a merged cell, .Offset(0, 1) will always give you the first cell to the right of the merged area. So if cells "A5:C5" are merged, Range("A5").Offset(0, 1) will give you "D5".
Assuming that you start at "A5", the following should do the trick for you:
With ActiveSheet ' Specify the sheet you want to work with
Set r = .Range("A5")
For i = 1 To 3
Set r = r.Offset(0, 1)
Debug.Print r.Address, r.MergeArea.Address, r.Value
Next
End With
Update
If you want to go from right to left: Offset(0, -1) will give you the last cell of the merged area. From there, you can get the value of the merged cells with MergeArea.Cells(1, 1)
Set r = .Range("AN87")
For i = 1 To 3
Set r = r.Offset(0, -1)
Debug.Print r.Address, r.MergeArea.Address, r.MergeArea.Cells(1, 1).Value
Next

Filtering linked but empty cells

Sub Filter_empty_Rows()
Dim Row_nr As Integer
With Worksheets("Boutenlijst Kist B")
For Row_nr = 3 To 1009
If Cells(Row_nr, 4).Value = "" Then
Cells(Row_nr, 4).EntireRow.Hidden = True
End If
Next Row_nr
End With
End Sub
This should hide all rows containing empty cells in column D. But it doesn´t. Because the empty cells still link to an other sheet. So there's is no real value but it is not empty as well.
Anyone got a workaround?
If a cell holds a reference to another cell and that cell has no value then the value of that cell's value is set to 0 by excel. So if you want one way to do it would be to do
If Cells(Row_nr, 4).Value = "" Or Cells(Row_nr, 4).Value = 0 Then
If some of your data is going to be genuinely zero then it might be a little trickier. You can use .Range.HasFormula to see if it contains a formula or not and use that to decide if it is a bad piece of data or not.
If that isn't good enough then you can use .Range.Formula to get the actual formula in a cell and then use that to perform the analysis that you need.

Excel VBA Clear Contents

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

Check if cell has content and if so, input something in a different cell

I found a VBScript that opens an certain spreadsheet in Excel 2013, adds a column and puts a name in the first row of the column. I'm looking to see if someone can help me with a script that can do the following:
The script should check all cells in column B starting at B2.
If B2 has something in it, then put a "1" (no quotes) in G2
Same thing, then if B3 has something in it, put a "1" in G3
And so on until it reaches and empty cell.
Once it hits the empty cell, the script is done.
I have the following script below. I have no idea where to start and I tried a few different things but get errors. This is the last revision where I was just trying to say "if (b2 is less than or greater than blank then J2 is = to 1) but it's probably not the correct format.
Const xlToRight = -4161
const xlColumns = &H2
const xlLinear = -4132
const xlDay = 1
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
Set objWB = objExcel.Workbooks.Open("T:\Science Data\SampleFile.xls")
Set objSheet = objwb.Sheets("sheet")
objSheet.Columns("J:J").Insert xlToRight
objSheet.Cells(1, 10).Value = "GetBestMatch"
objSheet.Cells(2, 10).Value = 1
If objSheet.Cells(2,2).Value <> "" Then objSheet.Cells(2, 10).Value = 1
set Range = objSheet.Range("J1:J" & objSheet.UsedRange.Rows.Count)
Range.DataSeries xlColumns, xlLinear,xlDay, 1, , False
objwb.Save
objwb.close
objExcel.quit
Thanks a lot for the suggestions. I made the changes to the script. Removed the objSheet.Cells(2,10).Value = 1 line (highlighted in bold) as it was more of a duplicate. I'm not sure if i can incorporate the following code below. The line you gave me works great and i can type the same line repeatedly with different cell values for each row.
Wanted to see if within the If statement you recommended, it could add a statement to keep checking down the B row. For example, check B3 for data and make J3 a 1 if B3 has data, then B4 and so on, and then stop adding ones to the respective J row once it hits an empty B cell. I couldn't get anything to work with your If statement so I searched and tried a Do statement that I found but made some changes to it (below).
Hopefully the code below is somewhat correct. I tried it but it doesn't fill in anything. was trying to make check B2 for data and move over 8 cells to J2 if B2 was empty, add a 1, then go down one row and back 8 to B3, then start the Do statement again from there.
Sub Test2()
Range("B2").Select
Do Until IsEmpty(ActiveCell)
ActiveCell.Offset(0,8).select
ActiveCell.value = 1
ActiveCell.Offset(1,-8).select
Loop
End Sub
if(B2<>"") then(J2="1")
In the above line B2 and J2 are (undefined) variables, when you actually want to refer to the cells B2 and J2. Use something like this for the latter:
If objSheet.Range("B2").Value <> "" Then objSheet.Range("J2").Value = 1
Note, however, that Range("B2") might be relative to the active cell. I find it usually safer to use the Cells collection:
If objSheet.Cells(2, 2).Value <> "" Then objSheet.Cells(2, 10).Value = 1
Also, this:
set Range = objSheet.Range("J1:J2"&objSheet.UsedRange.Rows.Count)
should probably look like this:
set Range = objSheet.Range("J1:J" & objSheet.UsedRange.Rows.Count)
With that said, I strongly recommend you immerse yourself in a good book about VBA. Once you have a good understanding of how VBA works, using it from VBScript won't be too hard.

Search for a Cell in Excel using VBA

Problem:
I would like to find a value of a cell next to or below the cell content a text value for a workbook.
Example:
In Sheet2, I have two cells stand random (assume its index is unknown and total is not a defined name)
I want to search for the value "200" stands next to the total and put it in sheet 2 (an active cell). In case, there are multiple cell that contains the word "Total" list all of them and if possible, put the name of the sheet that contains the cell that I am looking for. Value 200 Sheet2
My Approach:
1. User input
Go to each cell and search for it. This will take time if search for the whole limitation of cell in excel. So the search only limit to 100 columns x 10000 rows.
After find its index, offset to 1 columns to get the value
Write the result ActiveCell.Value = Search_Value. Then continue to search for the rest of sheets. Offset 1 coloum and 1 row to write the second value...
Searching is a very difficult concept, and I truly have no idea how to do the search part. Please help
With Worksheets(1).Range("a1:a500")
counter=0
Set c = .Find("Total", lookin:=xlValues)
If Not c Is Nothing Then
firstAddress = c.Address
Do
counter=counter+1
Worksheets(2).range("A1").offset(counter,0)=c.offset(0,1)
Set c = .FindNext(c)
Loop While Not c Is Nothing And c.Address <> firstAddress
End If
End With
firstaddress holds the location of the first cell found (so we know when to stop); firstaddress.offset(0,1) will give you the value you are trying to save, so setting worksheet(2).range("a1").offset(counter,0) will list all the values it finds on the 2nd tab, from a1 down to however many it finds in the range

Resources