Helo guys. Could someone help me resolve my EXCEL VBA Code. I'm really having trouble on how to make my FLASHCARD appear IN ORDER from Cell A1 down to the last cell. I have this code but it only generates a RANDOM cell or value. How can I make it non random? I know that by using the code RND, it generates randomly. But how about NON RANDOM?
Here's the code:
Private Sub NextCard()
Application.ScreenUpdating = False
Dim finalTermRow As Integer
finalTermRow = Range("a60000").End(xlUp).Row
Dim possibleRow As Integer
Dim foundTerm As Boolean
foundTerm = False
Dim tries As Integer
tries = 0
Do While foundTerm = False And tries < 1000
possibleRow = Rnd() * (finalTermRow - 2) + 2
If Cells(possibleRow, 4).Value = "" Then
If possibleRow <> previousRow Then
foundTerm = True
End If
End If
tries = tries + 2
Loop
Application.ScreenUpdating = True
If tries < 1000 Then
currentRow = possibleRow
BoxQuestion.Text = Cells(currentRow, 1).Value
BoxDefinition.Text = ""
AltBox.Text = ""
Else
MsgBox ("There are no other cards to go to--you've learned everything else! Congratulations! To study all your cards again, click reset.")
End If
End Sub
Hope you could help me guys resolve this issue. Thank you.
replace:
tries = 0
Do While foundTerm = False And tries < 1000
possibleRow = Rnd() * (finalTermRow - 2) + 2
with
tries = 0
possibleRow=0
Do While foundTerm = False And tries < 1000
possibleRow = possibleRow + 1
Related
Let's see...I'm working with macros in Excel, so I have a Macros called "Save" that is activated by a button and this macro works with a specific form: "frm_income", I create a new form: "frm_Expenses" and I want to use the same macro so, my question is how I code the conditional if the frm_Expenses is shown or activated and use the macro.
Here is part of the macro
Sub Save()
Dim i As Integer
Dim Final As Integer
frm_Expenses.btn_Save.Enabled = True
frm_Expenses.btn_Erase.Enabled = True
For i = 4 To 1000
If Sheet5.Cells(i, 2) = "" Then
Final = i
Exit For
End If
Next
sheet5.Cells(Final, 2) = frm_Expenses.txt_Code
....
And maybe I was thinking something like this
Sub Save()
Dim i As Integer
Dim Final As Integer
if frm_Sales.(something..activate...shown..) then
frm_sale.btn_Save.Enabled = True
frm_sale.btn_Erase.Enabled = True
For i = 4 To 1000
If Sheet5.Cells(i, 2) = "" Then
Final = i
Exit For
End If
Next
sheet5.Cells(Final, 2) = frm_Sales.txt_Code
else
frm_Expenses.btn_Save.Enabled = True
frm_Expenses.btn_Erase.Enabled = True
You can do this:
Sub Save(frm As Object)
Dim i As Integer
Dim Final As Integer
frm.btn_Save.Enabled = True
frm.btn_Erase.Enabled = True
For i = 4 To 1000
If Sheet5.Cells(i, 2) = "" Then
Final = i
Exit For
End If
Next
sheet5.Cells(Final, 2) = frm.txt_Code
....
Then call from your form:
Save Me
I am creating a spreadsheet for a client to manage his ALM. I developped it under Excel and VBA, request of my client.
One sheet "Data" calculates all the vba functions. If i calculate manually each cell all works fine, but if i run the macro it did not.
Do you have a solution? I can post the entire file if needed, for a better investigation.
At the beginning all the calculation where in excel cell, but i created dedicated function for each table, because the file was too big when saved.
Public Sub Main()
Dim i, nb_tableaux As Integer
Dim j, lignemax, BarWidth As Long
Dim ProgressPercentage As Double
Dim echeancier, nomtableau As String
Dim ws_data As Worksheet
Dim c As Range
Me.ProgressLabel.Caption = "Initialisation terminée. "
Set ws_data = Sheets("Data")
lignemax = ws_data.Range("DATA").Rows.Count
Application.ScreenUpdating = True
Application.EnableEvents = True
nb_tableaux = 17
For i = 1 To nb_tableaux
echeancier = tab_Tableaux(i, 0)
nomtableau = tab_Tableaux(i, 1)
Me.ProgressLabel.Caption = "En cours : " & echeancier
ws_data.Range(nomtableau).Calculate
'With Worksheets("Data")
For j = 1 To lignemax
For Each c In ws_data.Range(nomtableau).Rows(j)
formulaToCopy = c.Formula
c.ClearContents
c.Value = formulaToCopy
DoEvents
Next
Me.ProgressLabel.Caption = "En cours : " & echeancier & ", " & Format(j / lignemax, "0.0%") & " completed"
Me.Repaint
Next j
'End With
Me.Bar.Width = i * 200 / nb_tableaux
Me.Bar.Caption = Format(i / nb_tableaux, "0%") & " completed"
Next i
Application.ScreenUpdating = False
Application.EnableEvents = False
End Sub
after taking into account the recommandations you gave me for my previous answers, the code works better, but still not for some of the ranges.
My issue come from a wrong calculation of a argument in the fonction.
In fact, I use ligne=activecell.row - 8, to get the ligne of the range to calculate. But it works if i do it manually, as the actual cell is activated, but not when i call the function many times, as i can not activate each cell, it will be too long for the spreadsheet.
How can i get ligne calculated, with the correct address of the cell where the function is written?
I hope i am clear enough. Sorry for my English.
Public Function Taux_Mois(ByVal mMois As Range, ByVal sScenario As Range)
Dim ligne As Long
ligne = ActiveCell.row - 8
Select Case (Range("DATA[Flag]").Cells(ligne).Value = 0) Or (Range("DATA[frequence fixing]").Cells(ligne).Value = 0)
Case True
Taux_Mois = 0
Exit Function
Case False
Dim index_taux As Integer
Dim ajust As Long
index_taux = CInt(Range("DATA[Indexation ID]").Cells(ligne).Value)
If index_taux = 1 Then
ajust = 0
Else
Dim ajust1, dernierfixingt0, freqfixing As Integer
dernierfixingt0 = Range("DATA[Dernier fixing t0]").Cells(ligne).Value
freqfixing = Range("DATA[frequence fixing]").Cells(ligne).Value
ajust1 = (Int((mMois.Value - dernierfixingt0) / freqfixing) * freqfixing)
ajust = Worksheets("Market Data").Range("Taux_" & sScenario.Value).Offset(12 + dernierfixingt0 + ajust1, 1 + index_taux).Value
End If
Taux_Mois = Range("DATA[facteur taux (TVA, base)]").Cells(ligne).Value * (ajust + Range("DATA[Spread / Taux]").Cells(ligne).Value / 10000)
Exit Function
End Select
End Function
I have to implement this code, but I'm not sure the syntax, I have problem with "ending an If sentence", first time that I've write a macro on VB.
Please any help to write this code well. I need to put the result of this in the Cell CK6:
(EDITED CODE)
Sub Funcion()
Dim listo As Boolean
Dim min As Integer
Dim max As Integer
Dim beta As Integer
Dim tolerancia As Long
Dim deficit As Long
listo = False
min = 0
max = 1
beta = 0
tolerancia = 0.000001
While listo = False
prom = (min + mas) / 2
deficit = Worksheets("ETR").Cells(6, "CI")
If (deficit > 0) Then
If (deficit < tolerancia) Then
beta = prom
listo = True
Else: min = prom
End If
Else
If (Abs(deficit) < tolerancia) Then
beta = prom
listo = True
Else: max = prom
End If
End If
Wend
End Sub
Is there something wrong now? The macro get stucked but at least it show no errors.
I am making an app in visual basic with .NET Framework 4. I have to generate a list of prime numbers as per the user's input. So far, for my output if you put in 5 for the first five prime numbers you get 3 5 7 7 9 11 11. I am not sure if my number increment is in the wrong place. Thanks for any help you can give me. Also, I'm not sure how to include 2 as a prime number in my code.
Imports System.Math
Public Class Form1
Dim number, divisor, max, count As Integer
Dim IsPrime As Boolean
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
number = TextBox1.Text
For divisor = 2 To Sqrt(number)
If number Mod divisor = 0 Then
IsPrime = False
TextBox2.Text = ("Number is not prime")
Exit For
Else
TextBox2.Text = ("Number is prime")
End If
Next
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim Wrap As String
Wrap = Chr(13) & Chr(10)
max = TextBox3.Text
Dim count = 0
number = 2
While count <= max
IsPrime = True
For divisor = 2 To Sqrt(number)
If number Mod divisor = 0 Then
IsPrime = False
Exit For
Else
IsPrime = True
TextBox4.Text += number & Wrap
count += 1
End If
Next
number += 1
End While
End Sub
End Class
You should not use the else branch in the for loop, in this case, each time the Mod unequal to 0, you will touch the else block, take number 11 for example:
11 mod 2 <> 0, you went into the else block,
11 mod 3 <> 0, you went into the else block again!
you can divide the number by all numbers between 2 to sqrt(number), and then use isprime to check whether it is a prime like this.(I am using VBScript here)
isprime = true
for i = 2 to Int(sqr(number))
if number mod i = 0 then
isprime = false
exit for
end if
next
if isprime = true then
count = count + 1
' do something here...
end if
number = number + 1
and don't forget to truncate the square root of number.
Just to share with you this script :
Option Explicit
Dim Title,Copyright,j,fso
Dim WshShell,Affich,LogFile
Title = "Calcul Nombres Premiers"
Copyright = " (c) by Hackoo 2015"
For j = 2 to 1000
If Premier(j) = True Then
Affich = Affich & j & vbTab
End If
Next
MsgBox Affich,vbInformation,Title + Copyright
LogFile = "c:\NombresPremiers.txt"
Set fso = CreateObject("Scripting.FileSystemObject")
if fso.FileExists(LogFile) Then
fso.DeleteFile LogFile
end If
Affich = replace(Affich,vbTab,vbCrlf)
Call WriteLog(Affich,LogFile)
Set WshShell=CreateObject("wscript.shell")
WshShell.Run LogFile
'**********************************
Function Premier(Nombre)
Dim i,d
' Trois nombres ne seront pas pris en compte par le compteur,
' on s'organise pour qu'ils soient vus avant.
Select Case Nombre
Case 0
Premier = False
Exit Function
Case 1
Premier = False
Exit Function
Case 2
Premier = True
Exit Function
End Select
For i = 2 To Int(Sqr(Nombre)) + 1
d = Nombre Mod i
If d = 0 Then
Premier = False
Exit Function
End If
Next
Premier = True
End Function
'**********************************
Sub WriteLog(strText,LogFile)
Dim fs,ts
Const ForAppending = 8
Set fs = CreateObject("Scripting.FileSystemObject")
Set ts = fs.OpenTextFile(LogFile,ForAppending,True)
ts.WriteLine strText
ts.Close
End Sub
'***********************************
I have a user form in Excel VBA with a check box for each month.
Selecting one or more cause the required month to be shown on the sheet, I copy-pasted the code 12 times and it works but I'm sure there is a better way doing it with a For loop.
This is a part of my code (it goes on 12 times):
If CheckBox1.Value = True Then
ActiveSheet.PivotTables("PivotTable1").PivotFields("month").PivotItems("1").Visible = True
Else
ActiveSheet.PivotTables("PivotTable1").PivotFields("month").PivotItems("1").Visible = False
End If
If CheckBox2.Value = True Then
ActiveSheet.PivotTables("PivotTable1").PivotFields("month").PivotItems("2").Visible = True
Else
ActiveSheet.PivotTables("PivotTable1").PivotFields("month").PivotItems("2").Visible = False
End If
I tried writing:
for i in range 1 to 12
and then writing my code but there seem to be a problem when I put "i" instead of the numbers.
Assuming you aren't using Tristate checkboxes, then the .Value can only be True or False, so we should be able to get away with something like this:
(Assumes your code runs inside the UserForm, so that Controls is directly accessible)
Dim mthIdx as Long
Dim nm as String
Dim c As Control
With ActiveSheet.PivotTables("PivotTable1").PivotFields("month")
For mthIdx = 1 To 12
nm = "CheckBox" & mthIdx
Set c = Controls(nm)
.PivotItems(mthIdx).Visible = c.Value
Next
End With
(The With clause isn't strictly necessary, but it's usually a good idea to resolve nested COM references as infrequently as possible)
Try this ..
Dim i As Integer
Dim sN As String
Dim chx As MSForms.CheckBox
Dim obj As OLEObject
For i = 1 to 12
sN = format(i)
Set obj = OLEObjects("CheckBox" & sN)
Set chx = obj.Object
If chx.Value = True Then
ActiveSheet.PivotTables("PivotTable" & sN).PivotFields("month").PivotItems(sN).Visible = True
Else
ActiveSheet.PivotTables("PivotTable" & sN).PivotFields("month").PivotItems(sN).Visible = False
End If
Next
I've not checked the code but this should put you along thr right path if it's not spot on though...
For i = 1 to 12
If CheckBox(i).Value = True Then
ActiveSheet.PivotTables("PivotTable1").PivotFields("month").PivotItems(i).Visible = True
Else
ActiveSheet.PivotTables("PivotTable1").PivotFields("month").PivotItems(i).Visible = False
End If
Next i