Have macro for loop, need to throttle and only move to next i when a new button is pressed - excel

Pretty much I have a loop that I want to run through, however I don't want it to go to the next "i" until a button is pressed. My code is as follows. I believe my trouble is the location of the "If GoGo" but I have tried it in many places.
Sub GoGo()
Public GoGo As Boolean
GoGo = True
End Sub
Sub Runn()
Dim lastrow As Long, i As Long
For i = 23 To 32
DoEvents
If GoGo = True Then
If Cells(i, 1) <> 0 Then
Range("B5").Value = Cells(i, 2).Value
Range("E5").Value = Cells(i, 3).Value
Range("E11").Value = Range("C33").Value
Application.Run ("Realcount")
Application.Run ("Realcount2")
End If
End If
Next i
End Sub

Is this what you are trying?
Public GoGo As Boolean
Sub GoGoProc()
GoGo = True
End Sub
Sub Runn()
Dim lastrow As Long, i As Long
i = 23
Do
DoEvents
If GoGo = True Then
If Cells(i, 1) <> 0 Then
Range("B5").Value = Cells(i, 2).Value
Range("E5").Value = Cells(i, 3).Value
Range("E11").Value = Range("C33").Value
Application.Run ("Realcount")
Application.Run ("Realcount2")
End If
i = i + 1
GoGo = False
End If
Loop
End Sub
FOLLOWUP (From Comments)
Instead of using a loop, the best way I can think of is using a modeless userform (so that you want work with the Workbook/Worksheet at the same time) with a Next button. The Next button will increment the row value and then run the code. This way you will not keep Excel busy if you had to leave say for a cup of coffee ;)
Create Userform (UNTESTED) which should look like this
Paste this code in the userform
'~~> Next Button
Private Sub CommandButton1_Click()
Range("B5").Value = Cells(rw, 2).Value
Range("E5").Value = Cells(rw, 3).Value
Range("E11").Value = Range("C33").Value
Application.Run ("Realcount")
Application.Run ("Realcount2")
rw = rw + 1
End Sub
'~~> Canecel Button
Private Sub CommandButton2_Click()
Unload Me
End Sub
Create a Module and paste this code there
Public rw As Long
Sub Launch()
rw = 23
UserForm1.Show vbModeless
End Sub
To run your code, you can either run Sub Launch() directly or create a Command Button (Form Control - I guess that is what you are using) on your worksheet and assign Sub Launch() to it.

Related

How To Replace Cells With Specific Value In Excel?

I have the following:
Private Sub Worksheet_Activate()
If [E2] <> [G1] Then Range("H9:H44").Value = "Pendente"
End Sub
I need to include in the initial check whether the cells to be changed have a specific value.
I tried the following:
Private Sub Worksheet_Activate()
If [E2] <> [G1] And Range("H9:H44").Value = "Paga" Then Range("H9:H44").Value = "Pendente"
End Sub
That is, I need it to change only the cells that have the value "Paga" in that column.
What is the correct way to do this?
I need it to change only the cells that have the value "Paga" in that column.
Use .Replace so that it can replace all text in one go.
Option Explicit
Private Sub Worksheet_Activate()
If [E2] <> [G1] Then
Range("H9:H44").Replace What:="Paga", Replacement:="Pendente", LookAt:=xlWhole
End If
End Sub
This should do it.
With Sheet1 'Change as needed
If .Cells(2, 5).Value <> .Cells(1, 7).Value Then
Dim i As Long
For i = 9 To 44
If .Cells(i, 8).Value = "Paga" Then
.Cells(i, 8).Value = "Pendente"
End If
Next i
End If
End With
Try this, obviously change SheetName to your sheetname
For i = 9 to 44
x= Worksheets("SheetName").Cells(2,5)
y= Worksheets("SheetName").Cells(1,7)
z= Worksheets("SheetName").Cells(i,8)
If x<>y and z="Paga" then Worksheets("SheetName").Cells(i,8)="Pendente"
Next i
You can put this into a button (button below is called Replace) or use a macro
Private Sub Replace_Click()
Dim c As Range
For Each c In Range("H9:H44")
If c.Value = "Paga" Then
c.Value = "Pendente"
End If
Next c
End Sub
hope this helped

VBA use Collection for entire workbook

I need to use Collection variable in Global scope. But if i declare collection as public I can use it only in module sheet or worksheet. I need to declare it for entire workbook scope to use it in workbook functions, worksheet functions and module functions.
ThisWorkbook
Public foo As New Collection
Worksheet
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Application.ScreenUpdating = False
row= Target.Row
column= Target.Column
If Cells(row, 2).Value = "" Then
Exit Sub
Else
If Cells(row, 1).Value = "" Then
foo.Add row- 5
Cells(row, 1).Value = "X"
Cells(row, 2).Select
Cells(row, 14).Value = True
Else
foo.Remove row- 5
Cells(row, 1).Value = ""
Cells(row, 2).Select
Cells(row, 14) = False
End If
End If
Application.ScreenUpdating = True
End Sub
Module:
Sub col()
MsgBox foo.Count
End Sub
In a Standard Module:
Private m_collection As Collection
Public Property Get TheCollection() As Collection
If m_collection Is Nothing Then Set m_collection = New Collection
Set TheCollection = m_collection
End Property
Then you can call it from anywhere in your code.
ModuleName.TheCollection.Add("whatever")

Requiring Listbox Selection for userform

Private Sub CommandButton1_Click()
whichSheet = ListBox1.Value
Dim n As Integer
Do
n = n + 1
ListBox1.AddItem Sheets(n).Name
Loop Until n = Worksheets.Count
Worksheets(whichSheet).Activate
Dim lastrow
lastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
lastrow = lastrow + 1
Cells(lastrow, 1) = TextBox1
answer = MsgBox("Are you sure you want to add the record?", vbYesNo + vbQuestion, "Add Record")
If answer = vbYes Then
Cells(lastrow, 1) = TextBox1.Text
Cells(lastrow, 2) = TextBox2.Text
Cells(lastrow, 3) = TextBox3.Value
Cells(lastrow, 4) = TextBox4.Text
Cells(lastrow, 5) = TextBox5.Text
Cells(lastrow, 6) = TextBox6.Text
Else
Cells(lastrow, 1) = ""
Exit Sub
End If
End Sub
Private Sub UserForm_Initialize()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
ListBox1.AddItem (ws.Name)
Next ws
End Sub
Hello guys, I am using the code above which works perfectly for my userform. The only issue I'm having is that when someone doesn't pick a choice from the listbox1 and submits the info, the "Runtime Error" window pops up. I would like to stop that from happening by making a message box appear that tells users to make a choice--->click ok on the message box---> and then resume. If the user doesn't select an option still the same procedure should occur every time. If you have any ideas, I would love to try them out. Thanks.
My preferred way of handling this situation is to disable CommandButton1 if nothing in the ListBox1 is selected. Or, in other words, enable the button when something is selected.
Private Sub UserForm_Initialize()
Dim ws As Worksheet
CommandButton1.Enabled = False ' <--- here.
For Each ws In ThisWorkbook.Worksheets
ListBox1.AddItem (ws.Name)
Next ws
End Sub
I don't have Excel open at the moment, but you want a corresponding listbox event to enable/disable the button. The following is an example and untested.
Private Sub ListBox1_Change()
CommandButton1.Enabled = ListBox1.ListIndex <> -1
End Sub
Another approach is to check to see if something has been selected when you enter the CommandButton1_Click routine and handle it there. But I prefer to prevent bad user input in the first place - less complicated.

Excel VBA Run-Time error 13

THIS IS AN UPDATE TO MY ORIGINAL QUESTION OF 12/5. IT INCLUDES THE COMPLETE CODE USED FOR THE WORKBOOK.
MANY, MANY THANKS.!!!
I created a form that has 3 text boxes for data entry. It also has 3 buttons to choose from after data entry. The below code populates the table with the information from the form plus some additional information from the header in the worksheet when an "Update" button is pressed. This worked fine until I entered the code line " Reg1.SetFocus". I did this to set the focus back to the first text box after pressing the update button. I now get "Run-Time error 13" that debugs to this line of code:
".Reg4 = Application.WorksheetFunction.VLookup(CLng(Me.Reg1), Sheet2.Range("Lookup"), 4, 0)"
This all goes away if I delete the "Reg1.SetFocus" line in the prior sub.
The complete code for the workbook is:
Option Explicit
'Private Sub AmountEntry_Exit(ByVal Cancel As MSForms.ReturnBoolean)
AmountEntry.Value = Format(AmountEntry.Value, "$#,#00.00")
End Sub
Private Sub AmountEntry_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
End Sub
Private Sub ClearButton_Click()
Dim Ctrl As MSForms.Control
F or Each Ctrl In Input_Form.Controls
Select Case TypeName(Ctrl)
Case "TextBox"
Ctrl.Text = ""
Case "OptionButton"
Ctrl.Value = False
Case "ComboBox"
Ctrl.ListIndex = -1
End Select
Next Ctrl
'DateSelect.Value = Date
End Sub
Private Sub CloseButton_Click()
Unload Input_Form
End Sub
Private Sub UpdateTableButton_Click()
Dim LastRow As Range
Dim AssistanceTable As ListObject
'Add row to bottom of Assistance table
ActiveSheet.ListObjects("Assistance").ListRows.Add
'Enter data from form into our new row
Set AssistanceTable = ActiveSheet.ListObjects("Assistance")
Set LastRow = AssistanceTable.ListRows(AssistanceTable.ListRows.Count).Range
With LastRow
.Cells(1, 1) = Range("fund").Value
.Cells(1, 2) = Reg1.Value
.Cells(1, 3) = Reg2.Value
.Cells(1, 4) = Range("mass_date").Value
.Cells(1, 5) = Reg3.Value
.Cells(1, 6) = Range("mass_time").Value
End With
Dim Ctrl As MSForms.Control
For Each Ctrl In Input_Form.Controls
Select Case TypeName(Ctrl)
Case "TextBox"
Ctrl.Text = ""
Case "OptionButton"
Ctrl.Value = False
Case "ComboBox"
Ctrl.ListIndex = -1
End Select
'Set focus to Parishioner ID
On Error Resume Next
Reg1.SetFocus
On Error GoTo 0
Next Ctrl
End Sub
Private Sub UpdateTableButton_Enter()
End Sub
Private Sub UserForm_Click()
End Sub
Private Sub Reg1_AfterUpdate()
'Check to see if value exists
If WorksheetFunction.CountIf(Sheet2.Range("a:a"), Me.Reg1.Value) = 0 Then
MsgBox "Parishioner not yet in database"
Me.Reg1.Value = ""
Exit Sub
End If
'Lookup values based on first control
With Me.Reg4 = Application.WorksheetFunction.VLookup(CLng(Me.Reg1), Sheet2.Range("Lookup"), 4, 0)
End With
End Sub
Private Sub ComboBox2_Change()
Range("b3").Value = Format(Me.ComboBox2.Value, "dd mmm,yyyy")
End Sub
Private Sub UserForm_Initialize()
Me.Reg3.AddItem "Check"
Me.Reg3.AddItem "Cash"
End Sub

VBA Excel - Function Stuck

I am new ti VBA and i would like to perform a function as follows i hope someone could help me out
I need to set a macro that starts at Cell A2 when i click my function a dialog box appears which i can enter relevant information into and it inserts into the relevant cells
inserts data into 3 fields (B2, C2, D2)
then selects B3 where i can press my button again to do the same thins again
heres my code so far
Dim StartCell As Integer
Private Sub Cancel_Click()
Unload GarageDimensions
End Sub
Private Sub LengthBox_Change()
If LengthBox.Value >= 15 Then
MsgBox "Are you sure? You do realise it is just a garage!"
Exit Sub
End If
End Sub
Private Sub Submit_Click()
'This code tells the text entered into the job reference textbox to be inserted _
into the first cell in the job reference column.
StartCell = Cells(1, 2)
Sheets("Data").Activate
If IsBlankStartCell Then
ActiveCell(1, 1) = JobRef.Text
ActiveCell.Offset(0, 1).Select
ActiveCell(1, 1) = LengthBox.Value
ActiveCell.Offset(0, 1).Select
ActiveCell(1, 1) = ListBox1.Value
ActiveCell.Offset(0, 1).Select
ActiveCell(1, 1) = ListBox1.Value * LengthBox.Value
Else
Range("A1").End(xlDown).Offset(1, 0).Select
End If
Unload GarageDimensions
End Sub
Private Sub UserForm_Initialize()
With ListBox1
.AddItem "2.2"
.AddItem "2.8"
.AddItem "3.4"
End With
ListBox1.ListIndex = 0
End Sub
Thanks for your answers in advance
Adam
You don't need the Private Sub LengthBox_Change() event. You can set the MAX characters of the TextBox LengthBox either in the Design Mode or in the UserForm_Initialize() event as I have done below.
Also if you hard-code the Startcell then every time you run the UserForm the data will start from A2 and if there is any data there, then that will be overwritten. Instead try and find the last available row where you can write.
BTW, is this what you are trying (UNTESTED)?
Option Explicit
Dim StartCell As Integer
Dim ws As Worksheet
Private Sub UserForm_Initialize()
Set ws = Sheets("Data")
With ListBox1
.AddItem "2.2"
.AddItem "2.8"
.AddItem "3.4"
.ListIndex = 0
End With
LengthBox.MaxLength = 14
End Sub
Private Sub Submit_Click()
With ws
'~~> Find the first empty row to write
StartCell = .Range("A" & Rows.Count).End(xlUp).Row + 1
.Range("A" & StartCell).Value = Val(Trim(ListBox1.Value)) _
* Val(Trim(LengthBox.Value))
.Range("B" & StartCell).Value = JobRef.Text
.Range("C" & StartCell).Value = LengthBox.Value
.Range("D" & StartCell).Value = ListBox1.Value
End With
Unload Me
End Sub
Private Sub Cancel_Click()
Set ws = Nothing
Unload Me
End Sub

Resources