I have only recently started picking up VBA in excel and I have been going through various tutorials online but I have encountered a bit of a problem when getting to if statements. Based on the knowledge i have gained online and from my VB knowledge i came up with the following code.
Private Sub CommandButton1_Click()
Dim mynum As Integer, checker As String
mynum = Range("A1:A10").value
If mynum > 0 Then
checker = "check"
Else
checker = "missing"
End If
Range("B1:B10").value = checker
End Sub
The idea is that if there is a number over 0 in column a, the adjacent cell in column b is checked, if however its 0 or lower the adjacent cell says missing (if that makes any sense) theres no real point to this as I am learning the basics at the moment :)
Now my problem...basically when i attempt to debug this, I get the Type Mismatch error and i cant really see why this is so. I know that it works when i set the range as one cell rather than multiple cells so my best guess is that it has something to do with that. I have looked into it but again im getting results that only back up that this should work. I must have misread it somewhere but help would be appreciated. :)
You cannot Assign/Check values of range like this for what you are trying to do. One way would be to loop through your range. For Example
Private Sub CommandButton1_Click()
Dim Rng As Range, aCell As Range
Set Rng = Range("A1:A10")
For Each aCell In Rng
If aCell.Value > 0 Then
aCell.Offset(, 1).Value = "check"
Else
aCell.Offset(, 1).Value = "missing"
End If
Next
End Sub
EDIT
BTW, you don't need VBA for this. If you are open for a Non VBA solution then simply put this formula in Cell B1 and copy it down
=IF(A1>1,"Check","Missing")
Related
I am trying to use formulas to find a row in my google spreadsheet document, however I have got a weird problem.
I am not able to find values when a cell contains a number (without any other characters).
Consider the following case
I have got two values
A1 - 32323232323
A2 - 323-23232-323
When I use the following formula
=FILTER(A:E,REGEXMATCH(B:B,"323-23232-323"))
It works fine, it successfully finds A2 value, however when I try to use the following formula
=FILTER(A:E,REGEXMATCH(B:B,"32323232323"))
It doesn't match any row, and I also tried the following formula
ADDRESS(MATCH("32323232323",B:B,0),1)
It doesn't work either, it only works when I remove quotes like that
ADDRESS(MATCH(32323232323,B:B,0),1)
But this doesn't work with REGEXMATCH.
Is there any way I can match numbers using a regex expression (exact number, without wildcards) ?
Thanks
=FILTER(A:A,REGEXMATCH(REGEXREPLACE(TO_TEXT(A:A),"-",""), "32323232323"))
to get both 323-23232-323 and 32323232323.
=FILTER(A:E,REGEXMATCH(TO_TEXT(B:B),"32323232323"))
to get number 32323232323.
Notes:
Converting to_text is a key here.
Change columns to yours.
I can't find an example close enough to this one on StackOverflow so here goes:
I want to return a message "Type?" if cell X is blank and cell Y has any text. But I'm trying to nestle it into an existing set of IFs.
Existing :
=IF($G241="Evo";M241*L241;IF($G241="Free";M241*L241;IF($G241="GN";M241*L241))))
Nestling this into the above:
=IF(AND(NOT(ISBLANK($J234));ISBLANK(G234));"Type?";"OK")
I tried this but it returns FALSE, maybe due to the AND I'm using, which I need since I'm creating a return based on two cells two cells.
=IF($G240="Evo";M240*L240;IF(AND(NOT(ISBLANK($J240));ISBLANK(G240);"Type?";"OK");IF($G240="Free";M240*L240;IF($G240="GN";M240*L240))))
getting Error:
AND expects boolean values. But 'Type?' is a text and cannot be coerced to a boolean.
IF(and(isblank(cell x),iferror(isstring(cell y),false)),"Type?","OK")
That should do it for you I think. you will need to replace cell x and cell y with the appropriate references. The iferror statement is there to catch what happens when evaluating a blank cell y.
The problem with this formula
=IF($G240="Evo";M240*L240;IF(AND(NOT(ISBLANK($J240));ISBLANK(G240);"Type?";"OK");IF($G240="Free";M240*L240;IF($G240="GN";M240*L240))))
is you are trying to check G240 for different values when it cant. Lets simplify your formula. We will replace your empty cell check with FORMULA 1
=If($G240="EVO", Do True Condition, Do Formula 1, IF(G$240=Free, Do Free True Condition, Do Free False Condition)
The problem is since you already did something (Formula 1) when G240 = "EVO", you cant start another check on what G240 after the fact with the way you have embedded your formula. a batter way of thinking of it is how to do a second check when G240="EVO" is false. Remember the general format of an if statement is:
IF(CONDITION,True Result, False Result)
There are only 3 things that go into an if statement. you tried putting in 3.
Try rearranging to this:
=If($G240="EVO", Do True Condition, IF(SOME CHECK to determine DO FOMULA 1 or CHECK for G240 = FREE, Do Formula 1, IF(G$240=Free, Do Free True Condition, Do Free False Condition)))
Basically break down what you want to check for in G240 and do it in sequence with your IF statement. Right now with what you have written, I cant tell how you want to determine if you want to run your formula 1 or if you want to check if G240="free" since you have two different outcomes if G240="Free"/
OK I think i found the issue. The IF(AND(NOT(ISBLANK works on it's own since there are no other IFs in the formula. I do want to test two different cells for text(letters) in order to show a warning if one cell was blank while the other not. But as soon as you insert the (AND into a string of multiple IFs it doesn't work.
Simply removing the (AND was all I needed to do. Another way to achieve a test for more than one blank cell was to simply add multiple IF(ISBLANKs.
EG: =IF(ISBLANK(A1)+IF(ISBLANK(A2)>2;condition true;condition false)
ForwardEd thanks very much for your help!
Regards
Have been searching for a few hours now I'm stuck. Not really sure what I'm searching for but guessing some VBA.
What I want to do in excel is this:
Have a predefined list i.e. [a,a#,b,c,c#...g#]
Now when somebody enters value 'A' into cell A20 then A19 will be the next on the list 'A#', A18 will be 'B', A17 will be 'C' etc.
If the value C is entered into cell A20 then A19 would be 'C#' and so on...
Once the list reaches G# it shall repeat from A again. This would repeat until cell A2 is reached.
Even a point in the right direction would be great as I don't really know what to search for. Have tried 'arrays' 'loop' 'cycle' without much luck.
Thanks in advance
You would do better learning the basics of VBA than searching for odd terms. Search for "Excel VBA Tutorial". There are many to choose from so try a few then complete the one that matches your learning style. I prefer books. I visited a good library; reviewed their Excel VBA Primers and then the borrowed what I thought were the best to try at home. Finally I bought the best as a permenent reference book.
Your questions is very unclear but I think you need a For Each loop within a Do loop. You will find an introduction to loops near the start of any tutorial or primer. Try to develop this:
Sub demo()
Dim CellId As Variant
Dim LoopCount As Long
' #### Replace Tidy with the name of a worksheet in your workbook.
With Worksheets("Tidy")
Do While True
For Each CellId In Array("A20", "A19", "A18", "A17")
Debug.Print CellId & "=" & .Range(CellId).Value
If LoopCount >= 3 Then
Exit Do
End If
Next
LoopCount = LoopCount + 1
Loop
End With
End Sub
I'm working with a big excel file that has a lot of information on businesses my company works with. I just imported another large excel file and their was a difference in format. The larger file we already have has the address, state and zip code in separate columns each spaced two apart like so:
I didn't make this spreadsheet or else I wouldn't have put the columns like that, but thats how the lady that works with it likes it.
The problem is that the sheet I imported has the city, state, and zip info all in the same cell like this:
Trollville, NY 12345
I have already over the states since 99% of the new ones were all the same state which a quick find and replace all worked. I'm now left with this
Trollville 12345
I want to move that zip code four columns to the right into the proper cell. I wrote a basic regex but don't know much about excel-vba since I haven't used it in years, but this is what I've come up with. I just don't know how to tell vba to print output the matches (which I made into an array) into the appropriate column. This is what I have so far:
Function findZipCode(zipCode)
Dim regEx As New VBScript_RegExp_55.RegExp
Dim matches, s
regEx.Pattern = "\s\d{5}\W"
regEx.Global = True
s = ""
If regEx.Test(zipCode) Then
Set matches = regEx.Execute(zipCode)
For Each Match In matches
s = s & Match.Value
Next
findZipCode = s
Else
findZipCode = ""
End If
End Function
What do I need to add? I'm open to alternative methods too if there is an easier way to do this.
Thanks in advance for the advice
Can you use the in-built Excel Worksheet Functions?
Place this in target column =RIGHT(A2,5) would capture the rightmost 5 characters of your string iff they are numeric. This will work if all of your values data values have a 5-digit zip codes at the end.
Alternatively, you could wrap it with a conditional such as IF(ISNUMBER(VALUE(RIGHT(A2,5))),RIGHT(A2,5),""), whcih would add a layer of validation to the process.
Also, did you know there is an option that may do this for you automatically if your data is comma (or space) delimited Data ribbon->Text to columns
I'm hitting problem with a reasonably straightforward vba macro for Microsoft Word which is designed to get around some issues we're seeing with list indentation when we create PDFs versions from the Word doc.
The macro basically loops through each list in the document, and for each list paragraph associated with the list, it is setting the list template's bullet and text position to match what's applied at the paragraph level (code needs to be used with Word 2000 so not using list styles).
When dealing with large documents (60+ lists, ~350 list paragraphs), the macro runs through fine first time, but second time dies half way through with a "This method or property is not available because there is a memory or disk problem".
I've gone down the usual route of unsetting any object references used during the loop, so I can't see what might be holding on to the memory.
The code is quite simple and consists of a single procedure, currently stored in ThisDocument:
Option Explicit
Sub test2()
Dim i As Integer, n As Integer
Dim curList As List, curPar As Paragraph, templ As ListTemplate
Dim gapSize As Double, level As Integer
Application.ScreenUpdating = False
Application.Options.Pagination = False
For i = 1 To Lists.Count
Set curList = Lists(i)
For n = 1 To curList.ListParagraphs.Count
Set curPar = curList.ListParagraphs(n)
Set templ = curPar.Range.ListFormat.ListTemplate
level = curPar.Range.ListFormat.ListLevelNumber
gapSize = templ.ListLevels(level).TextPosition - templ.ListLevels(level).NumberPosition
templ.ListLevels(level).NumberPosition = curPar.LeftIndent - gapSize
templ.ListLevels(level).TextPosition = curPar.LeftIndent
templ.ListLevels(level).TabPosition = curPar.TabStops.After(curPar.LeftIndent - gapSize).position
Set templ = Nothing
Set curPar = Nothing
Next n
UndoClear
Set curList = Nothing
Next i
Application.ScreenUpdating = True
Application.Options.Pagination = True
End Sub
I've found a nasty, dirty solution that gets around the issue somewhat but is really a very poor fix. Basically, once we have gotten through one complete run of the macro, close and save the document and immediately reopen. This then allows the macro to be rerun immediately or at any stage because closing the document seems to eventually flush the memory properly. Obviously this can only be used if the user is happy to save as part of running the macro, but in my case it is
Your code looks OK and my sugestions will be just ideas to try doing the same another way...
Idea 1: insert a DoEvents somewhere in the innerloop, to facilitate garbage collection.
Idea 2: simplify your code by using FOR EACH constructs:
For Each curlist in Lists
For each curPar in curList.ListParagraphs
With curPar.Range.ListFormat.ListTemplate
.....
End With
Next curPar
Next curList
Besides UndoClear, you can also save the document at each loop.
It might heavily impact in your macro performance, though.
There's a similar issue here http://social.msdn.microsoft.com/Forums/en-US/vsto/thread/de7a88b4-914f-4895-a88a-659b732e8d87/
Hope this helps.