Hello Please consider my silly question, I am stuck here since a long time
ElseIf Cells(m1, a) >= 1 And Cells(m1, a) <= 98 Then
Cells(m1, a).Font.Bold = True
here only the values without decimal point (eg. 4,56,90)etc are getting bold, values with decimal point (4.5,56.5,90.54) despite being in the rqnge are not getting filtered.
Please suggest possible problem
I think the problem is with French numbering system as the data coming is from France.
Thank you
Is there any way to consider different numbering system, with the US system....???
You should use Cells(m1,a).value as well as Cells(m1,a).value
The code below will examine the list of cell are selected before the macro is run. For each cell, if it's value is [1..98] then I take the state and toggle it.
Sub toggleBoldInSelection()
Dim cellValue
For Each curCell In Selection
cellValue = curCell.Value
If (cellValue >= 1) And (cellValue <= 98) Then
curCell.Font.Bold = Not curCell.Font.Bold 'True
End If
Next curCell
End Sub
Try this
ElseIf Val(Cells(m1, a).Value) >= 1 And Val(Cells(m1, a).Value) <= 98 Then
If you are not running this code from the sheet code area then do not forget to fully qualify the cells object. For example
ThisWorkbook.Sheets("Sheet1").Cells(m1, a).Value
Related
Hi I am writing Excel VBA code. I just wanted to do subtraction for this kind of table
But I can't figure out how to do that using VBA. Is the code below correct?
Sub OperationO()
Dim OE As Integer
Dim i As Integer
For i = 3 To Range("F6")
Cells(6, i).Value = Cells(2, i).Value - Cells(4, i)
Next i
End Sub
You should really use formulas for something like this, but if you insists on VBA, here it goes.
Whenever you write code for VBA or any similar languages, read what you are writing, what it should/needs to do, as you read it out loud many times the errors pop up. Think of the comments below as "me reading as I write."
Sub OperationO()
'Initiate a Variable type Integer for number storage
Dim OE as Integer
'Initiate another variable same type to use in the loop
Dim i as Integer
'Start a loop from 3 to 6 (because these are the columns you are working with)
For i = 3 to 6
'Set the value in Column "i" on Row 6 to the value in Row 2 minus Row 4 in the same column
'Now here is the thing, when you subtract a negative number, you are adding it, crazy math rules i know, so if the number is negative, you need to Add instead.
Cells(6, i).Value = (Cells(2, i).Value + Cells(4, i).Value)
'If both cells don't contain a number, this will fail, additional checks may help, like IsNumber
Next i
'Its the end of the Sub and you never used the Variable OE, it was declared for nothing.
End Sub
I apologize if this is an easy question, I just started macro programming yesterday and have not written in BASIC since qBasic (or any language for a while). I'm tring to read if a variable is equal to a specific value and, if so, write a different cell into a spreadsheet if it contains data. I keep getting an error of next without a for. It runs fine without the first if statement, can someone help me out? Bonus if you can help on writing the value rather than content, but I can figure that out without bothering you fine folks. Thanks for the help getting me to this point, this site has been invaluable. You'll probably recognize some of the coding.
Sub B920LOI()
x = 5
iMaxRow = 3000
For iRow = 3 To iMaxRow
If Sheets("Sheet2").Cells(iRow, "B") = "B920" Or Sheets("Sheet2").Cells(iRow, "B") = " B920" Then
If Sheets("Sheet2").Cells(iRow, "K") > 35 And Sheets("Sheet2").Cells(iRow, "K") < 55 Then
' Check that cell is not empty.
' Copy the cell to the destination
With Worksheets("Sheet2").Cells(iRow, "K")
.Copy Destination:=Worksheets("920 LOI").Cells(x, "B")
End With
x = x + 1
Else
'Nothing in this cell.
'Do nothing.
End If
Else
'Nothing in this cell.
'Do nothing.
End If
Next iRow
End Sub
This is the "working version" Thank you.
Your nests have to match... the "End With" needs to be inside the If block where the With is.
I have the following question. I want to check with macros if a certain cell within range of cells in Excel contains date values (short date i.e. 12.3.2012) or specific word.
I will try to explain myself with the following example code:
Private Sub typedata()
Dim x, y
For x = 12 To 13
For y = 16 To 71
If isDate(Cells(x, y)) <> True then
MsgBox "Please enter correct data"
Exit Sub
elseIF Cells(x,y) <> "certain word" Then
MsgBox "Please enter correct data"
Exit Sub
End If
Next y
Next x
End Sub
I will appreciate any help to find the correct code.
First, I suspect (but am not certain) that you have your x's and your y's around the wrong way. The Cells function uses the arguments (Row, Column) and it would be rare to have a really short data range (rows 12 to 13) but a really wide one (columns 16 to 71). (Your code may be right, I'm just noting that it's unusual and something that you may want to check.)
What you're really looking for is the And operator, though one other thing to note is that the certain word test will be case sensitive. I normally therefore compare by converting both to upper case.
Finally, you may want to tell the user where exactly the problem is.
Also I do recommend declaring your variables as a type to avoid the variant to integer conversion cost. I always use Longs rather than integers in VBA (not VB.Net) because Ints get internally converted to longs anyway.
This is quick, dirty and only marginally tested but should point you in the right direction:
Private Sub typedata()
Dim x As Long, y As Long
For x = 12 To 13
For y = 16 To 71
If IsDate(Cells(x, y)) <> True And UCase(Cells(x, y)) <> UCase("certain word") Then
MsgBox "Please enter correct data into cell " & Cells(x, y).Address
Exit Sub
End If
Next y
Next x
End Sub
SO this started as me trying to help someone else, got stumped. So basically i have values in columns B, C, and D. if have my criteria in H2 and I2 and when my criteria in H2 and I2 matches in B and C then have the corresponding answer in D to populate J2. basically a vlookup with 2 criteria.
i have something like this.
Sub test()
Dim rngCrit1 As Range
Dim rngCrit2 As Range
Dim rngAnswer As Range
Dim strTarget As String
Set rngCrit1 = Range("H2")
Set rngCrit2 = Range("I2")
Set rngAnswer = Range("J2")
Range("B2").Select
strTarget = ActiveCell.Value
Do While strTarget <> ""
With ActiveCell
If strTarget = rngCrit1 Then
If .Offset(0, 1).Value = rngCrit2 Then
rngAnswer.Value = .Offset(0, 2)
Else
.Offset(1, 0).Select
strTarget = ActiveCell.Value
End If
End If
End With
Loop
End Sub
Now this thing just crashes, no debugging or anything. I am self taught so i'm sure i screwed the pooch here somewhere.
*Note this is just to satisfy my own interest not really important, so if it takes you more than 5 min please help someone else that needs it more than I.
Val1 Val2 Val3 Crit1 Crit2 Answer
a r 12 g v 22
b r 14
c s 15
d s 16
e t 18
f t 19
g y 20
g v 22
sample data
It's great that you're trying to improve your VBA skills. The first thing I'd suggest, which will improve any macro you write, is to avoid using .Select. Work directly with the range objects. For instance:
Range("B2").Select
strTarget = ActiveCell.Value
becomes
strTarget = Range("B2").Value
Also, in general, use vbNullString or Len(variable)=0 when checking for "empty" values instead of "". As for why your program is crashing, it may be your use of With. Like Select, it should be avoided in most cases (definitely in this one). Although you update ActiveCell, it's within the scope of the With statement, so once you close it (End With), those changes to ActiveCell are undone (I would suggest stepping through the macro and watch the values of strTarget and ActiveCell). This may not be the case, but I know it holds for other variables, which is why I avoid With (and avoid reassigning values in a With statement)
Anyway, I'd add the following code and rewrite the loop as follows:
Dim r as range
set r = Range("B2") 'keep in mind this range is on the ActiveSheet, so you're better
'off explicitly naming the Sheet e.g. Sheet1.Range("B2")
strTarget1 = Range("B2").Value
strTarget2 = Range("C2").Value
Do While Len(strTarget) <> 0
If strTarget1 = rngCrit1 Then
If strTarget2 = rngCrit2 Then
rngAnswer.Value = r.Offset(0,2)
Exit Do
End If
End If
set r = r.Offset(1,0)
strTarget1 = r.Value
strTarget2 = r.Offset(0,1).Value
Loop
Keep in mind you could also loop with a Long counter i for the row, then call Sheet1.Cells(i,1).Value, Sheet1.Cells(i,2).Value and so on for the values of the different columns of that row (instead of using a range object and .Offset
EDIT: After running your code, the reason for the crash is due to your If statements. You want to go to the next cell regardless. Remove the Else and put the End If statements before the Select. Add an Exit Do after your assignment statement in the 2nd If, since you want to stop looping if your two columns meet the criteria. I've updated my code to show this, as well.
INDEX and MATCH, or SUMPRODUCT tend to work well for this. An example of the former:
http://support.microsoft.com/kb/59482
if you can guarantee val1 and val2 will be unique (e.g. when searching for g & v, there is only 1 line with g and v) then you can use sumifs
I put val1,val2 and val3 in columns A,B, & C, and the search into E,F and the answer in G, and came up with this formula
=SUMIFS(C2:C9,A2:A9,E2,B2:B9,F2)
of course, this fails if val3 is not numeric, or there are more than 1 line with the letters you are looking for
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I know only little how to make macros in Excel.
You can find many examples through Google.
The very first result is a post from David Gainer's blog that uses Conway’s Game of Life to teach about circular reference formulas and iteration (no VBA involved):
http://blogs.office.com/2007/11/02/iteration-conways-game-of-life
Should have done this a long time ago but here's my version of Conway's Life in excel.
Here's a hack of the code. By no means a perfect solution (didn't spend an age on this) but you might be able to pick some bits out.
Private arrGrid(100, 100) As Boolean
Private arrGridNextGeneration(100, 100) As Boolean
Private Sub PopulateParentArrayData()
For k = 1 To Sheet1.Range("C2:AM20").Cells.Count
If Sheet1.Range("C2:AM20").Cells(k).Interior.Color = Sheet1.Range("A1").Interior.Color Then
arrGrid(Sheet1.Range("C2:AM20").Cells(k).Row, Sheet1.Range("C2:AM20").Cells(k).Column) = True
Else
arrGrid(Sheet1.Range("C2:AM20").Cells(k).Row, Sheet1.Range("C2:AM20").Cells(k).Column) = False
End If
DoEvents
Next
End Sub
Private Sub ApplyParentArrayData()
For k = 1 To Sheet1.Range("C2:AM20").Cells.Count
If arrGrid(Sheet1.Range("C2:AM20").Cells(k).Row, Sheet1.Range("C2:AM20").Cells(k).Column) Then
Sheet1.Range("C2:AM20").Cells(k).Interior.Color = Sheet1.Range("A1").Interior.Color
Else
Sheet1.Range("C2:AM20").Cells(k).Interior.Color = Sheet1.Range("B1").Interior.Color
End If
DoEvents
Next
End Sub
Private Sub ApplyNextGenerationArrayData()
For k = 1 To Sheet1.Range("C2:AM20").Cells.Count
If arrGridNextGeneration(Sheet1.Range("C2:AM20").Cells(k).Row, Sheet1.Range("C2:AM20").Cells(k).Column) Then
Sheet1.Range("C2:AM20").Cells(k).Interior.Color = Sheet1.Range("A1").Interior.Color
Else
Sheet1.Range("C2:AM20").Cells(k).Interior.Color = Sheet1.Range("B1").Interior.Color
End If
DoEvents
Next
End Sub
Private Function GetNeighbourCount(ByVal pintRow As Integer, ByVal pintColumn As Integer) As Integer
Dim intCount As Integer
intCount = 0
For r = pintRow - 1 To pintRow + 1
For c = pintColumn - 1 To pintColumn + 1
If r <> pintRow Or c <> pintColumn Then
If arrGrid(r, c) Then
intCount = intCount + 1
End If
End If
Next c
Next r
GetNeighbourCount = intCount
End Function
Private Sub PopulateNextGenerationArray()
Dim intNeighbours As Integer
For r = 0 To 100
For c = 0 To 100
If r > Sheet1.Range("C2:AM20").Rows(0).Row Then
If r <= Sheet1.Range("C2:AM20").Rows(Sheet1.Range("C2:AM20").Rows.Count).Row Then
If c > Sheet1.Range("C2:AM20").Columns(0).Column Then
If c <= Sheet1.Range("C2:AM20").Columns(Sheet1.Range("C2:AM20").Columns.Count).Column Then
intNeighbours = GetNeighbourCount(r, c)
If arrGrid(r, c) Then
'A1 cell
If intNeighbours < 2 Or intNeighbours > 3 Then
arrGridNextGeneration(r, c) = False
Else
arrGridNextGeneration(r, c) = True
End If
Else
'B1 cell
If intNeighbours = 3 Then
arrGridNextGeneration(r, c) = True
Else
arrGridNextGeneration(r, c) = False
End If
End If
End If
End If
End If
End If
DoEvents
Next c
Next r
End Sub
Private Sub ActionLogic()
'Application.ScreenUpdating = False
PopulateParentArrayData
PopulateNextGenerationArray
ApplyNextGenerationArrayData
'Application.ScreenUpdating = True
End Sub
To get this to work just make the background of cell A1 black, the background of cell B1 white and then add some black backgrounds in the range C2:AM20 and run the ActionLogic method.
You will need two macros. The first one should format the game sheet so the cells are square.
Have the user run this macro. After that she should enter a 1 for each cell that is alive. Use conditional formatting to turn the cell completely black (background = black if value != 0)
Now have a second macro which calculates the next step in a background sheet (another sheet). Use relative cell positioning (relative to ActiveCell) and two nested loops. When this is done, copy all values from the background sheet to the game sheet.
Search for it and look at their code. Plenty of people have made it a hobby to make full games in Excel.
Ex: http://www.geocities.jp/nchikada/pac/
Why do you say Excel is the wrong choice?
I think Excel is the best way to solve this:
Excel solves this with 1 line:
IF(OR(SUM(B2:D4)-C3=3,AND(SUM(B2:D4)-C3=2,C3=1)),1,0)
*where the above is an expression that returns the next generation value for the cell C3.
Here's the demo:
https://docs.google.com/open?id=0B4FcWULw3iQidlZSdG9GRDh0TXM
If you're in a situation where you have to implement this sort of things from scratch, then functional programming is the best way to go. Otherwise, Excel works really well. Why? Because Excel is a system that forces you to enter only pure functions. You see, the key to simulating this Game of Life is to realize that each state of the cells is a PURE FUNCTION of the previous state. Excel naturally forces you to think this way.
another tutorial on circular references in excel can be found here: http://chandoo.org/wp/2009/01/08/timestamps-excel-formula-help/
this one explains how you can insert timestamps using circular references.
Excel is definitely the wrong choice for this kind of a problem. As to how it would be possible: First learn about the game of life and then visual basic to use in Excel.