I m trying to create a button and fill the value for column on.5 based on the value in column no.4.
here is the code:
Private Sub CommandButton1_Click()
Dim x As Integer
For x = 3 To 1000
If Cells(x, 4).Value = "Regular" Then
Cells(x, 5).Value = Today()
ElseIf Cells(x, 4).Value = "Temporary" And Cells(x, 5).Value = "" Then
Cells(x, 5).Value = "To be assigned"
Else
Cells(x, 5).Value = ""
End If
End Sub
This gives me the errors that sub or function is not defined.
I am new to VBA and trying to solve this, but could not do it.
Thanks.
VBA is ignoring the End Sub, because you haven't finished closing all of the clauses before it:
Private Sub CommandButton1_Click()
Dim x As Integer
For x = 3 To 1000
If Cells(x, 4).Value = "Regular" Then
Cells(x, 5).Value = Today() 'This should be Now()
ElseIf Cells(x, 4).Value = "Temporary" And Cells(x, 5).Value = "" Then
Cells(x, 5).Value = "To be assigned"
Else
Cells(x, 5).Value = ""
End If
'You are missing a "Next x" here
End Sub
Since the Sub is not complete, it does not count it as valid, so it cannot find it at compile time.
Related
I am trying to edit my excel table with VBA but an error appears while compiling. It doesnt recognize line 2 and line 10.
Sub IfThenElse()
Dim i As Integer = 23
While Not IsNull(Cells(i, 35).Value)
If Cells(i, 35).Value > 1E+16 Then
Cells(i, 4).Value = Cells(i, 35).Value / 10
Else
Cells(i, 4).Value = Cells(i, 35).Value
i = i + 1
End If
End While
End Sub
You cannot declare a variable and set a value at the same time Dim i As Integer = 23
Row counts are of type Long not Integer, Excel has more rows than Integer can handle.
Dim i As Long
i = 23
While … End While is no valid syntax, you need to use Do While … Loop (see Do...Loop statement).
It is very unlikely that a cell value is Null if you are looking for an empty cell use IsEmpty or check for vbNullString
Do While Not IsEmpty(Cells(i, 35).Value) 'or Do While Not Cells(i, 35).Value = vbNullString
If Cells(i, 35).Value > 1E+16 Then
Cells(i, 4).Value = Cells(i, 35).Value / 10
Else
Cells(i, 4).Value = Cells(i, 35).Value
i = i + 1
End If
Loop
Not sure what exactly you are doing but i = i + 1 might need to come after End If.
I am very new to vba coding .
In the worksheet I am trying to add an additional column using macros-column (Q)by checking conditions in simultaneous columns J,K,O).So if certain conditions pass in each of the columns I want a value to be entered in Q column for the respective row. This is the piece of code I put together .
Option Explicit
Sub Button2_Click()
Sheet1.Activate
Dim i As Long
For i = 2 To .Cells(.Rows.Count, "B").End(xlUp).Row
'Check so that we only process non-empty cells
'(just in case there is an empty cell part-way through the data)
If Cells(i, 10).Value = "No" And Cells(i, 15) <= 0 Then
Cells(i, 17) = "Pending with employee"
Else
If Cells(i, 10).Value = "No" And Cells(i, 15) >= 0 And Cells(i, 11) = "No Action Pending" Then
Cells(i, 17) = "Pending with employee"
Else
If Cells(i, 10).Value = "No" And Cells(i, 15) >= 0 And Cells(i, 11) = "Pending With Manager" Then
Cells(i, 17) = "Pending with Manager"
Else
If Cells(i, 10).Value = "Yes" And Cells(i, 15) >= 0 And Cells(i, 11) = "No Action Pending" Then
Cells(i, 17) = "All Done"
'If Not IsEmpty(.Cells(i, "B").Value) Then
' If .Cells(i, "E").Value = "NA" Then'
'ThisWorkbook.Worksheets("CTCto TCC Conversion").Cells(i, "F").Value = "NA" '
End If
End If
End If
End If
Next i
End With
MsgBox "Column Created"
End Sub
It throws me an error Invalid or unqualified reference .Please help me if there any errors that need to be rectified for the code to run .
Thanks
Using the With Statement
BigBen has already answered your question. Here's an example with a little extra.
Check the two lines containing .Cells(i, 15).Value. One of them should probably have the equal sign removed.
If you write several conditions in one line of code, all of them will be evaluated even if the first one is already True (or False) making it less efficient than writing each condition in a new line. In practice, you most often won't feel a difference. I cannot decide which one is more readable or maintainable for you though.
Option Explicit
Sub Button2_Click()
With Sheet1
Dim i As Long
For i = 2 To .Cells(.Rows.Count, "B").End(xlUp).Row
If .Cells(i, 10).Value = "No" Then
If .Cells(i, 15).Value <= 0 Then
.Cells(i, 17).Value = "Pending with employee"
Else
If .Cells(i, 11).Value = "No Action Pending" Then
.Cells(i, 17).Value = "Pending with employee"
ElseIf .Cells(i, 11).Value = "Pending With Manager" Then
.Cells(i, 17).Value = "Pending with Manager"
End If
End If
ElseIf .Cells(i, 10).Value = "Yes" Then
If .Cells(i, 15).Value >= 0 Then
If .Cells(i, 11).Value = "No Action Pending" Then
.Cells(i, 17).Value = "All Done"
End If
End If
End If
Next i
End With
MsgBox "Column Created"
End Sub
I have the following code
Dim i As Integer
For i = 3 To 10
If Range("H3").Value = Cells(i, 2).Value And Range("I3").Value < Cells(i, 4).Value And _
Range("I3").Value >= Cells(i, 3).Value Then
Range("J3").Value = Cells(i, 5).Value
End If
Next i
I want the value of J3 to represent the sum of all the iterations and not just the last iteration if i. Can it be done?
While there are certainly better methods of adding up cells, for your particular method this should work.
Dim i As long, lTotal as long
For i = 3 To 10
If Range("H3").Value = Cells(i, 2).Value And Range("I3").Value < Cells(i, 4).Value And _
Range("I3").Value >= Cells(i, 3).Value Then
lTotal = Cells(i, 5).Value + lTotal
End If
Next i
Range("J3").Value = lTotal
Keep a running total of of your loop, then use the running total as your cell's value after you've finished the loop
Change this line
Range("J3").Value = Cells(i, 5).Value
To:
Range("J3").Value = Range("J3").Value + Cells(i, 5).Value
I am using the following code to enter data from Userform to Excel sheet and works fine.
The problem is that it overwrites the same row of data. But if I change:
.Cells(RowCount, 4).Value = Me.DepSectDrop.Value to contain a 1 --> .Cells(RowCount, 1).Value = Me.DepSectDrop.Value, and likewise for the rest (2 fore SiteFacOpen, 3 for CaseStartOpen, etc), it does not overwrite.
Private Sub cmdAdd_Click()
'Copy input values to sheet.
Dim RowCount As Long
Dim ws As Worksheet
Set ws = Worksheets("TRACK")
RowCount = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
With ws
.Cells(RowCount, 4).Value = Me.DepSectDrop.Value
.Cells(RowCount, 5).Value = Me.SiteFacOpen.Value
.Cells(RowCount, 6).Value = Me.CaseStartOpen.Value
.Cells(RowCount, 7).Value = Me.TypeDrop.Value
.Cells(RowCount, 8).Value = Me.ProcessDrop.Value
.Cells(RowCount, 9).Value = Me.CompNameOpen.Value
.Cells(RowCount, 10).Value = Me.CompEIDOpen.Value
.Cells(RowCount, 11).Value = Me.RespNameOpen.Value
.Cells(RowCount, 12).Value = Me.RespEIDOpen.Value
.Cells(RowCount, 13).Value = Me.DescOpen.Value
End With
'Clear input controls.
Me.DepSectDrop.Value = ""
Me.SiteFacOpen.Value = ""
Me.CaseStartOpen.Value = ""
Me.TypeDrop.Value = ""
Me.ProcessDrop.Value = ""
Me.CompNameOpen.Value = ""
Me.CompEIDOpen.Value = ""
Me.RespNameOpen.Value = ""
Me.RespEIDOpen.Value = ""
Me.DescOpen.Value = ""
End Sub
What do I need to do to so I maintain the right columns for it all to be entered, but not be overwritten? Thank you
You need to change all lines that start
.Cells(RowCount, 5).Value ...
To
.Cells(RowCount + 1, 5).Value
The '+1' bit means you're using the next blank line.
Also, as Samuel pointed out, you should also change to
RowCount = ws.Cells (Rows.Count, 4).End (xlUp).Offset (1,0).Row
so that you're testing a column that's guaranteed to have data in it!
Sorry, I missed the offset bit ... No need to '+1' if you're offsetting by 1 ... It amounts to the same thing.
I am trying to write a code that will take one cell and then iterate through another column to find a match, once it has found a match it will then match two other cells in that same row and return the value of a 5th and 6th cell. However, it is not working! any suggestions??
Sub rates()
Dim i As Integer
For i = 2 To 2187
If Cells(i, 1).Value = Cells(i, 11).Value Then
If Cells(i, 2).Value = Cells(i, 12).Value Then
Cells(i, 20) = Cells(i, 1).Value
Cells(i, 21) = Cells(i, 11).Value
Cells(i, 22) = Cells(i, 4).Value
Cells(i, 23) = Cells(i, 16).Value
Else
Cells(i, 24) = "No match"
End If
End If
Next i
End Sub
Try fully qualifying your cell objects i.e. sheet1.cells(i,1).value etc or encase within a with statement i.e.
with sheet1
if .cells(i,X) = .cells(i,Y) then
'...etc
end with
I think the default property for a range is "Value" but try putting .Value on to the end of all those Cell lines too... like you have for half of them :)
[EDIT/Addition:]
... failing that, you're not actually searching a whole column at any point: try something like:
Sub rates()
Dim i As Integer
Dim rgSearch As Range
Dim rgMatch As Range
Dim stAddress As String
Dim blMatch As Boolean
With wsSheet
Set rgSearch = .Range(.Cells(x1, y1), .Cells(x2, y2)) ' Replace where appropriate (y = 1 or 11 i guess, x = start and end row)
End With
For i = 2 To 2187
Set rgMatch = rgSearch.Find(wsSheet.Cells(i, y)) ' y = 1 or 11 (opposite of above!)
blMatch = False
If Not rgMatch Is Nothing Then
stAddress = rgMatch.Address
Do Until rgMatch Is Nothing Or rgMatch.Address = stAddress
If rgMatch.Offset(0, y).Value = Cells(i, 12).Value Then
Cells(i, 20) = Cells(i, 1).Value
Cells(i, 21) = Cells(i, 11).Value
Cells(i, 22) = Cells(i, 4).Value
Cells(i, 23) = Cells(i, 16).Value
blMatch = True
Else
End If
Set rgMatch = rgSearch.FindNext(rgMatch)
Loop
End If
If Not blMatch Then
Cells(i, 24) = "No match"
End If
Next i
End Sub
I've made a lot of assumptions in there and there's a few variables you'll have to replace. You could also probably use application.worksheetfunction.match but .find is quicker and more awesome