i am pretty new in using VBA for Excel, but after some searching without result i guess i need some help from you.
I am trying to format a specific cell so that, if one condition is met, it shows the value from another cell, but preceded by a specific text, like "Constantin". So, the displayed result would be, for example Constantin 80.25 .
The code that i'm trying looks like this:
If Cells(4, 1) < 0 Then
With Range("A1")
.NumberFormat = "'Constantin' 0.00"
.Value = - Cells(4, 1)
End With
End If
I know the syntax is not right, this is my problem, i guess i can't find the right syntax. I hope I'm not bothering you with this, probably, simple question, but i just couldn't find what i needed. Thanks a lot
It looks like you don't need the word as part of the actual format, in which case something like this will suffice:
If Cells(4, 1).Value < 0 Then
Range("A1").Value = "Constantin " & (Cells(4, 1).Value * -1)
End If
If you really wanted to use a With block then:
With Cells(4, 1)
If .Value < 0 Then
Range("A1").Value = "Constantin " & (.Value * -1)
End If
End With
Related
I'm used to python syntax where to check if 7 is in list1 you simply type 7 in list1 and it returns a boolean. How can I perform something like this in vba?
I'm currently looping through a large range. I want to occasionally check if a value i'm looping over is in a different range. This could get much slower if I had to nest more loops into my loops. What's the fastest way to approach this problem?
For i = 400 To 1 Step -1:
'doing other things
'here's some psuedo-code of what I want to do
If Sheets("Sheet2").Cells(i, 2).Value In Sheets("Sheet1").Range("NamedRange")
Sheets("Sheet2").Cells(i, 2).EntireRow.Delete
End If
Next i
Use a countif, if it's greater than zero then you know it exists in the list:
If Application.WorksheetFunction.CountIf(Sheets("Sheet1").Range("NamedRange"), Sheets("Sheet2").Cells(i, 2).Value) > 0 Then
Sheets("Sheet2").Cells(i, 2).EntireRow.Delete
End If
Here is how you can achieve it in just one line
If Not IsError(Application.Match(ValueToSearchFor, RangeToSearchIn, 0)) Then
// Value found
End If
Example:
Search for Blah-Blah in column A of Sheet5
If Not IsError(Application.Match("Blah-Blah", Sheets("Sheet5").Range("A:A"), 0)) Then
'The value present in that range
End If
i am using an excel formula to reference, and adjust when necessary, a very long list of scraped values. my problem is that occasionally the values start with "=+" causing them to be seen as a formula, and thus producing a #NAME? error. whenever my formula references these cells, it too produces said error.
is there anyway to account for #NAME errors within my formula? something like ISNA but for #NAME errors?
if that is not possible, could i use VBA to delete "=+" from any cells that contain it?
any help would be greatly appreciated, ive been looking for an answer to this for hours.
The easiest way is to add a ' in front of each cell's formula.
Imagining you have your scraped values in the range C1:C10, you can fix it like this:
For j = 1 To 10
Range("C" & j).Value = "'" & Range("C" & j).Formula
Next j
By doing so, you preserve the original value of your scraped object.
Of course you can fix the issue at the source, i.e. imagining you print your values like this:
rng.Value = myScrapedValue
... you can replace it like this:
rng.Value = quoteFirst(myScrapedValue)
... where
Private Function quoteFirst(ByVal strng As String) As String
quoteFirst = "'" & strng
End Function
I think you could use an =IFERROR() in your formulas directly, that would save time of developping a VBA code dedicated to this specific error.
Can you not use something like...
=ERROR.TYPE(A1) =5
This should produce a TRUE-FALSE in the formula....
If you want to delete the "=+" you can use something like this :
Dim index As Integer
For index = 1 To 10
If (Mid(Cells(index, 1), 1, 2) = "=+") Then
Cells(index, 1) = Mid(Cells(index, 1), 3)
End If
Next
It will replace the content of the cells beginning by "+=" by the same content without "+=".
I have a question, I am trying to define a cell name in excel using vba. Now to select one cell only i have been using the following code which proves to work fine:
Range("A1").Name = "zm_1"
However I need to name a number of cells in a column i.e:
Range("A1").Name = "zm_1"
Range("A2").Name = "zm_2"
Range("A3").Name = "zm_3"
Range("A4").Name = "zm_4"
Since this is quite tedious for 100 cells, i have been trying to use an array:
For i=1 to 100
Range("A(i)").Name = "zm_(i)"
next
^ this however gives an error which i have been unable to track. Any ideas/suggestions on how can it be done? Thanks!
The problem is with Range("A(i)").Name = "zm_(i)" The " indicate that something is text, so you should place your (i) out of the ".
For the first part it is quiet easy, as you can also use the row and column way of describing the cell. Therefore that becomes
Range(Cells(i,1)).Name
For the second part you need to concatenate the text and the numbers. That becomes: "zm_" & i
You don't need an array, just your loop:
For i = 1 to 100
Range("A" & i).Name = "zm_" & i
next
Try the below code and hope it helps you:
For i = 1 To 100
ThisWorkbook.Sheets(1).Range("A" & i).Name = "zm_" & i
Next
Where Sheet(1) refers to Sheet1 of the current workbook.
Thanks for taking a look at my question. Know that I am new to stackoverflow and VBA I have a long macro I've been working on. At one point in the macro I have to convert "Start Depth" (L) and "End Depth" (M) from "m" to "ft". Then I subtract the two to find the "Footage" (N). However, some of the values in the columns are originally left blank. So, after making my conversions and subtractions, I'm left with "#VALUE!" which is giving me errors later on in the macro. Originally I had changed all of the blanks to 0's before the conversions and subtractions. But, After "finishing" the macro I realize the zeros are messing with presenting the data. So, I'd like to just do the conversions and subtractions and then, change all the "#VALUES!" back to blanks. I found some stuff on this but nothing that I could (that i know of) use or specific to me:
http://www.ozgrid.com/forum/showthread.php?t=60740 and https://superuser.com/questions/715744/excel-2010-formula-how-do-i-write-this-formula-vba
Here is what i was using to change blanks into 0's
Worksheet1.Select
lastrow = Range("A666666").End(xlUp).Row
For Each cell In Range("A1:Q" & lastrow)
If Len(cell.Value) = 0 Then
cell.Value = 0
End If
Next
Here is the code resulting in errors. Note: The data starts with blanks and after using these formulas I am given errors because some of the original cells begin as null or blanks. Also, These lines aren't in this order but, they are the lines leaving errors.
ActiveCell.FormulaR1C1 = "=CONVERT(RC[2],""m"",""ft"")"
Selection.AutoFill Destination:=Range("N2:O2"), Type:=xlFillDefault
Selection.AutoFill Destination:=Range("N2:O" & lastrow)
Range("N1") = "Footage"
Range("N2").Select
ActiveCell.FormulaR1C1 = "=RC[-1]-RC[-2]"
Selection.AutoFill Destination:=Range("N2:N" & lastrow)
Any help is appreciated. Thanks guys!
It depends if the #VALUE! is being generated from a formula or from VBA code.
Formula
If it's being generated from a formula, wrap the formula in the IFERROR function. This was added in Excel 2007 I believe. So for example if your formula was:
=A1-B1
Then you could put
=IFERROR(A1-B1,"")
Which is saying, if A1-B1 is an error, return "", otherwise return the result of A1-B1.
VBA
If the value is being generated by VBA you could write a helper function that works like IFERROR
Public Function MyIfError(value As Variant, value_if_error As Variant)
If IsError(value) Then
MyIfError = value_if_error
Else
MyIfError = value
End If
End Function
And then pass your value through that.
This does not directly answer your question, but it does fix the presentation of Zero's on your worksheet. You can use the following setting to show Zeros as Blanks through VBA: ActiveWindow.DisplayZeros = False It might be a consideration instead of looping through everything looking for 0's and switching them to blanks manually.
Worksheet1.Select
lastrow = Range("A666666").End(xlUp).Row
For Each cell In Range("A1:Q" & lastrow)
If Len(cell.Value) = 0 Then
cell.Value = 0
End If
Next
For Each cell In Range("A1:Q" & lastrow)
cell.value = WorksheetFunction.Iferror(cell.value,"")
End If
Next
I'm sorry I don't have opprtunity to test it right but give it a try.
Bottom line is that I simply included a second loop that runs IFERROR function on your data.
Summary: I'm taking a row of data from one sheet and pasting it into another, however the sheet would be a daily use kind of thing where new data is just entered below old data.
Problem: On each new run, 7 is consistently added to the UsedRange.Count. For example: on one run the UsedRange.Count will be 7; the next time I run through the function the count will be 14.
What I'm Looking For: Why is this the case and is there a way to help UsedRange be more accurate
-I've included the entire Function for references' sake.
Function eftGrabber()
Dim usedRows As Integer
Dim i As Integer
ChDir "\\..."
Workbooks.Open Filename:= _
"\\...\eftGrabber.xlsm"
usedRows = Sheets("EFT").UsedRange.Count
Windows("Data").Activate
Sheets("DataSheet").Range("A11").EntireRow.Copy
Windows("eftGrabber").Activate
Sheets("EFT").Range("A" & usedRows + 1).Select
ActiveSheet.Paste
i = usedRows
Do 'THIS LOOP DELETES BLANKS AFTER POSTING NEW LINES
Range("A" & i).Select
If Range("A" & i) = "" Then
ActiveCell.EntireRow.Delete
End If
i = i - 1
Loop Until i = 1
Windows("eftGrabber").Activate
ActiveWorkbook.Save
Windows("eftGrabber").Close
End Function
Let me know if I've left out any important details. Thanks in advance!
Change: usedRows = Sheets("EFT").UsedRange.Count
To: usedRows = Sheets("EFT").Range("A" & Sheets("EFT").Rows.Count).End(xlUp).Row
Where "A" can be changed to whichever row you wish to count the total number of columns.
There is a danger in using UsedRange because it factors in such things and formatted cells with no data and other things that can give you unexpected results, like if you are expecting your data to start in Range("A1"), but it really starts in another range!
I will say, however, that If you really wish to use UsedRange, your code above is still wrong to get the rows. Use this instead UsedRange.Rows.Count or to get the last absolute cell of the UsedRange, use UsedRange.SpecialCells(xlCellTypeLastCell).Row
This two line do the magic
usedCol = ThisWorkbook.ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Column
usedRow = ThisWorkbook.ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Row
For more info visit Microsoft's site
http://msdn.microsoft.com/en-us/library/office/ff196157.aspx
Thanks for the discussion...
.UsedRange.Rows.Count and .UsedRange.Columns.Count work fine provided there is something in cell A1. Otherwise need to use the SpecialCells solution.
Hope this is helpful.
“UsedRange” works if you use it like this >>
x := Sheet.UsedRange.Row + Sheet.UsedRange.Rows.Count - 1;
y := Sheet.UsedRange.Column + Sheet.UsedRange.Columns.Count - 1;
Problem with SpecialCells is that you can't use it on a Protected Sheet.
Assuming you have contiguous sheet (i.e. no blank cells), and you sheet starts in A1, then I have found that
Range("A1").CurrentRegion.Rows.Count
gives the most reliable results.