VBA Excel wrong order of IF statement - excel

Good afternoon,
I have another problem related to IF statement.
I have the following code:
Sub BOMNewRow()
If Range("D38").Value <> "" Or Range("C38") <> "x" Or Range("C38") <> "0" Then
'Inserting new Core Drill row + formula autofill from the previous one
Range("A38").EntireRow.Insert
Range("A38").Value = "First Core drill into building or existing chamber (per event)"
Range("B38").Value = "Sitec"
If Range("D39").Value Like "*2*" Or Range("C39").Value = 2 Then
Range("C38").Value = 2
Else
Range("C38").Value = 1
End If
Range("E38").Value = 100.39
Range("F37").Select
Selection.AutoFill Destination:=Range("F37:F38"), Type:=xlFillDefault
'customizing new row
With Range("A38:B38,D38:F38")
.Borders(xlEdgeTop).Weight = xlThin
.Borders(xlEdgeBottom).Weight = xlThin
End With
With Range("C38")
.Borders(xlEdgeBottom).Weight = xlMedium
End With
Else
Call AuditCheck 'go straight away to the BoM audit control
'Taking a look on one of the audit cell
End If
If Range("C39").Value <> "x" Or Range("C39").Value <> "0" Then
Range("C39").Value = "x"
Range("D38").Value = Range("D39")
Call New_version2
Sheets("BoM").Activate
Else
Call AuditCheck
End If
Range("A43").Select
End Sub
What I intend to do is:
insert the new row 38 and customize it - this is done by my first IF statement.
run the second IF statement based on the cell C39, which doesn't work.
If previously I had the X value in the cell C38, now when I inserted a new row the second IF statement still executes the process for value C39, which is X.
.
It looks like I don't understand the if statement order within the macro.
How can I solve this problem?

Related

How would I use an If Statement from a userform where if the cell matches another cell it would add the results to a specific field / row?

The majority of my code is working correctly however, when I move onto validation form the userform to excel it isn't working correctly. I am using a form that allows the user to enter in a job number in column AA and Quantity in Column AC. I am keeping a list of jobs in column A. When the user uses the button it will auto fill columns AA thru AD I am looking for after this is done it will check to see if the job number exists in column A and if it does then in the same row in Column D it would add the quantity that was just entered. In the background I have a piece of code that will auto calculate and increase in column D
Private Sub CommandButton1_Click()
whichSheet = PartNoTxtBox.Value
If whichSheet = "" Then
MsgBox "You didn't specify a Part Number"
Exit Sub
End If
Worksheets(whichSheet).Activate
If Me.PartNoTxtBox = "" Then GoTo MoreInfo
If Me.AddJobNoTxtBox = "" Then GoTo MoreInfo
If Me.AddShipperNoTxtBox = "" Then GoTo MoreInfo
If Me.AddQtyTxtBox = "" Then GoTo MoreInfo
If Me.AddDate = "" Then GoTo MoreInfo
''LastRow
eRow = Cells(Rows.Count, "AA").End(xlUp).Offset(1, 27).Row
Cells(eRow, 27) = AddJobNoTxtBox.Text
Cells(eRow, 28) = AddShipperNoTxtBox.Text
Cells(eRow, 29) = AddQtyTxtBox.Text
Cells(eRow, 30) = AddDate.Text
If AddJobNoTxtBox.Value = Cells(eRow, 1) Then
Cells(eRow, 4) = AddQtyTxtBox.Text
End If
Unload UserForm1
Exit Sub
MoreInfo:
MsgBox "More information required."
End Sub
The issue is located at the If AddJobNoTxtBox.Value=Cells...
any direction would be helpful.

Replace textbox input by a specific number

In my userform, users can enter numbers in a textbox (2 maximum).
The number then gets copied in a worksheet.
I'm trying to have any number entered in this textbox to get copied but with the number 6 at the end.
If the user enter '.14'; then in the worksheet, I need to see '.16'.
If the user enter '.45'; then in the worksheet, I need to see '.46'.
And so on..
The only exception, is If the user enter '6' in the textbox, then in the worksheet, I need to ONLY see '.6', and not '.66'. (this problem was fixed thanks to an answer received earlier : VBA to ignore cell formating when adding a specific number using a userform)
I've used cell formating and entered ''.06'' In 'Type'. I thought It worked, but then when I enter let's say '.43' in the userform, the number that gets copied in the cell is not '.46' like expected, but ''.56''.
I dont know why.
EDIT :
I tried this but it's not working :
Sub ReplaceLastDigit()
Dim r As Range
Application.ScreenUpdating = False
For Each r In Range("C4", Range("C" & Rows.Count).End(xlUp))
If IsNumeric(r) And Len(r) > 1 Then
r = Left(r, Len(r) - 1) & "6"
End If
Next r
Application.ScreenUpdating = True
End Sub
EDIT 2:
Everything works, except one thing. When it's only ".6", it doesn't add ".66" (great), but it adds a zero. So I see ".60". Is it the formating?
This is the code :
If Left(TextBox3.Text, 1) <> 6 Then
Sheets("Sheet3").Range("C4").Select
Selection.Borders.Weight = xlThin
ActiveCell.Value = "." & Left(TextBox3.Text, 1) & 6
Else
Sheets("Sheet3").Range("C4").Select
Selection.Borders.Weight = xlThin
ActiveCell.Value = ".6"
End If
Here is the correction to my code that made it work :
If Left(TextBox3.Text, 1) <> 6 Then
Sheets("Sheet3").Range("C4").NumberFormat = "#"
Sheets("Sheet3").Range("C4").Select
Selection.Borders.Weight = xlThin
ActiveCell.Value = "." & Left(TextBox3.Text, 1) & 6
Else
Sheets("Sheet3").Range("C4").NumberFormat = "#"
Sheets("Sheet3").Range("C4").Select
Selection.Borders.Weight = xlThin
ActiveCell.Value = ".6"
End If

Inserting Blank Rows in Excel VBA

Hey I have been writing some code to add a part ID to a spreadsheet off of a user form in Excel VBA. I have been reading through different documentation and can not figure out why no matter what type of method of inserting a row I try it inserts a row with a repeating value instead of a blank one. If anyone knows how to specify blank, other than writing the whole row to blank and then writing my numbers I want after, that would be appreciated.
I have tried both the following lines to add a row
Cells (x+1 ,column).EntireRow.Insert Shift:= xlDown
ws1.Rows(x+1).Insert Shift:=xlDown
This is the function it is used in:
Public Sub Add(IDRange As Range)
SearchCell = Cells(x, IDRange.Column)
Cells(x, IDRange.Column).Select
Do
If SearchCell = PartID Then
MsgBox " this Company Already uses this part"
Exit Sub
ElseIf x <> StopRow Then
x = x + 1
SearchCell = Cells(x, IDRange.Column)
End If
Loop While x <> StopRow And SearchCell <> PartID
Cells(x + 1, IDRange.Column).EntireRow.Insert Shift:=xlDown
Cells(x, IDRange.Column).Value = PartID
MsgBox PartID & " has been added to Adress " & Cells(x, IDRange.Column).Address
Cells(x, IDRange.Column).Select
End Sub
Bellow is the function that calls the Add Function and where I belive it may be getting the company name from
Private Sub AddPart_Click()
AddPartCounter = 0
Company = UserForm1.CompanyBox.Value
PartID = UserForm1.PartBox.Value
If Company = "" Then
MsgBox " Please put in the company you would like the part to go under"
ElseIf PartID = "" Then
MsgBox " Please put in the Part you would like entered"
ElseIf UserForm1.Studs.Value = False And UserForm1.Spreaders.Value = False And UserForm1.Blocks.Value = False And UserForm1.Imma.Value = False Then
MsgBox "Please select the type of part you are trying to add"
Else
Dim CurrentCell
Set CurrentCell = Cells.Find(What:=Company, LookAt:=xlWhole)
If CurrentCell Is Nothing Then
MsgBox " Company Not Found "
Exit Sub
End If
x = CurrentCell.Row
Do
Set CurrentCell = CurrentCell.Offset(1, 0)
Loop While CurrentCell.Offset(1, 0) = "" And Not CurrentCell Is Nothing And CurrentCell.Offset(1, 0).Row <> thisvar.Row + 1
StopRow = CurrentCell.Row
'If they are trying to add a nut
If UserForm1.Imma.Value = True Then
Call Add(Nut_ID_Rng)
'IF they are trying to add a stud
ElseIf UserForm1.Studs.Value = True Then
Call Add(Stud_ID_Rng)
'If they are trying to add a block
ElseIf UserForm1.Blocks.Value = True Then
Call Add(Block_ID_Rng)
'If they are trying to add a spreader
ElseIf UserForm1.Spreaders.Value = True Then
Call Add(Spreader_ID_Rng)
End If
End If
AddPartCounter = 1
End Sub
I know that the repeating pattern is coming from the insert line through debugging but I can not figure out why I have tried changing variables to numbers and it still did the same thing. This what it looks like with the repeating values.
enter image description here
The problem is that you most likely have a value still stored in your clipboard when you execute the Macro. To fix that, simply add this line of dode before running the insert line:
Applcation.CutCopyMode = False
That will clear your clipboard and allow the inserted rows to be blank.

How can I compare the data in a cell to see if it is an error, "0" as text, or any other value?

I am trying to write an If statement in VBA for excel to look at the value of a cell and determine if it is "0" (cell is formatted as text), if it's an error, or if it's anything besides those two.
If it is anything besides "0" or an error I want to copy the selection and paste it in another column.
Here is my code snippet:
'Nested For Loop to Extract Values into Column G
For DCRNumOfParts = LastCellInColumn - 4 To 1 Step -1
Set SortCell = Cells(SortRow, 5)
SortCell.Select
IsCellError = IsError(Selection.Value)
'If Statement to determine if the cell value is 0 or error state
'If Selection.Value <> 0 Or VarType(ActiveCell.Value) <> vbError Then
If Selection.Value <> "0" And IsCellError <> True Then
Selection.Copy
Set CopyCell = Cells(SortRow, 7)
CopyCell.Select
ActiveSheet.Paste
End If
SortRow = SortRow + 1
Next DCRNumOfParts
Managed to solve this one myself, I handled the type-mismatch error by doing the following:
CellAsString = CStr(Selection.Value)
'If Statement to determine if the cell value is 0 or error state
'If Selection.Value <> 0 Or VarType(ActiveCell.Value) <> vbError Then
If Not IsError(Selection.Value) And CellAsString <> "0" Then

it doesnt go through all the cell, sometimes jump to another one without going in a sequel

Sub Vecinas()
For Each cell In [Data!A2.A50001]
pmNoVecias = cell.Offset(0, 0)
pmNameOrigen = cell.Offset(0, 1)
pmNameVecina = cell.Offset(0, 7)
pmVecinas = cell.Offset(0, 8)
pmIntentos = cell.Offset(0, 9)
If pmNameOrigen = "" Then
Exit For
End If
If pmVecinas = "Yes" Then
If pmIntentos < 100 Then
cell.EntireRow.Copy
Sheets("Borrar").Select
cell.EntireRow.PasteSpecial
Sheets("Data").Select
cell.EntireRow.Delete
End If
Else
If pmIntentos > 100 Then
cell.EntireRow.Copy
Sheets("Agregar").Select
cell.EntireRow.Insert
Sheets("Data").Select
cell.EntireRow.Delete
Else
cell.EntireRow.Delete
End If
End If
Next
End Sub
This program runs fine, it deletes information from one sheet and puts it in another one, the only problem is that it doesn't go through every single cell, sometimes jump 2 or 3. How can I make for this program to go to every single cell?
When a row is deleted, the row under it is not inspected, but skipped.
Eg. if you delete row 2, row 3 becomes the new row 2; but your For each loop goes on, to row 3. This means one row got skipped.
You can try using cell.EntireRow.Clear instead of Delete, and after the code is finished, just sort your table, or select blank cells in a column and delete rows.

Resources