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
Related
I Have an Issue to call Another Module (Run Time Error'424': Object Required). I have 2 Modules, Module 1 and Module 2. And Below is the code in Module 1 :
Private Sub test()
Dim Work As Worksheet: Set work= Sheets("S_BDN")
For i = 1 To 2
Set f = work.Range("A5", work.Range("A5").End(xlDown))
Set a = f.Find(i, LookIn:=xlValues)
If a.Offset(0, 10).Value = "January" Then
Call Module3.Proceed_B
End If
Next i
End Sub
And Below is the code in Module 2 :
sub Module3.Proceed_B()
If a.Offset(0, 6).Value = "A" Then
Debug.Print a.Offset(0, 4).Value
else
Debug.Print a.Offset(0, 5).Value
end if
end sub
All help is greatly appreciated. Thank you.
I can't see your "fixed" code or your sheet, so here are some generic suggestions for improvement:
Private Sub test()
Dim Work As Worksheet, f As Range, a As Range
Set work= Sheets("S_BDN")
Set f = work.Range("A5", work.Range("A5").End(xlDown)) 'take out of loop
For i = 1 To 2
Set a = Nothing
Set a = f.Find(i, LookIn:=xlValues, lookat:=xlWhole) 'provide *all* relevant parameters
If Not a Is Nothing Then 'make sure you got a match
If a.Offset(0, 10).Value = "January" Then
Proceed_B a 'pass a to the other sub
End If
End If
Next i
End Sub
sub Proceed_B(a As Range)
If a.Offset(0, 6).Value = "A" Then
Debug.Print a.Offset(0, 4).Value
else
Debug.Print a.Offset(0, 5).Value
end if
end sub
I have a code already but I want to know if this code can be altered or if there is a code that can check to see if a cell in the column E is empty and clear contents in a cell in column A if the someone exits the row
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 5 Then
If Target.Value = vbEmpty Then
Cells(Target.Row, 1).ClearContents
End If
End If
End Sub
Edited as per DirkReichel suggestion
Add this formula in A1:
=IF(E1="","",IF(LEN(A1),A1,TODAY()))
Now drag it down in your column "A" as far as you need. It will add today's date in column "A" if there is a value in column "E". Otherwise column "A" will remain empty
You are trying to get information of the "last" selection upon a change.... but there isn't a build-in solution. With a global variable, you still can do like this:
Dim oldTarget As Range
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If oldTarget Is Nothing Then GoTo e
If oldTarget.Rows.Count > 1 Then
Dim x As Range
For Each x In oldTarget.Rows
If x.Cells(1, 5).Value = "" Then x.Cells(1, 1).Value = ""
Next
Else
If oldTarget.Cells(1, 5).Value = "" Then oldTarget.Cells(1, 1).Value = ""
End If
e: Set oldTarget = Target.EntireRow
End Sub
As you see: Dim oldTarget As Range is outside of the sub. This way the set value/object stays until VBA get's stopped (closing the workbook / directly reset vba)
The first bit of your code checks for changes on column E to clear the column A.
So we just need to do the same thing again, but checking if column E is empty when changing column A.
This way, if you change the value on Column A and, at the same row, Column E is empty, it clears what you entered.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 5 Then
If Target.Value = vbEmpty Then
Cells(Target.Row, 1).ClearContents
End If
Elseif Target.Column = 1 Then
If Cells(Target.Row, 5).Value = vbEmpty Then
Cells(Target.Row, 1).ClearContents
End If
End If
End Sub
Edit: So, after your comment, here is how you should use your code
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 5 Then
If Target.Value = vbEmpty Then
Cells(Target.Row, 1).ClearContents
Else
'Insert here whatever code you got on the single click event (Except the sub and end sub)
End If
Elseif Target.Column = 1 Then
If Cells(Target.Row, 5).Value = vbEmpty Then
Cells(Target.Row, 1).ClearContents
End If
End If
End Sub
I am trying to populate my userform combobox with a list of part numbers after the user has selected the workstation on the previous user form.
My idea was to loop through the column, identify when the string matches the workstation and then add the cell in the column to the right (which turns out to be the product number)
My code looks like this so far:
If station = "MILL" Then
With ComboBox1
.AddItem "350SC109e.1"
.AddItem "350 SC166"
.AddItem "350 SC193"
.AddItem "350 SC195"
End With
End If
If station = "BRAKE" Then
For i = 2 To ws1.Range("A265").End(xlUp).Row
If ws1.Cells(i, 1) = "Brake" Then
ComboBox1.AddItem ws1.Cells(i, 2)
End If
Next i
End If
The MILL is an example of a manually doing the thing I want to accomplish with the loop in the BRAKE if statement.
Here i whipped this up real quick to demonstrate that I could get this to work in a loop. Youll have to adjust to fit your needs though. It did work flawlessly
Private Sub CommandButton1_Click()
Dim txtVal As String
If IsNull(TextBox1.Value) = False Then
txtVal = TextBox1.Value
Else
txtVal = ""
End If
Dim rng As Range
Set rng = ThisWorkbook.Sheets("Sheet1").Range("A1:A21")
Dim rcell As Range
For Each rcell In rng.Cells
If rcell.Value = txtVal Then
With ComboBox1
.AddItem rcell.Offset(0, 1).Value
End With
End If
Next rcell
End Sub
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.
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