I'm very new to VBA and SQL, and I'm currently building a table to be uploaded to SQL onto Excel and using VBA.
I want to essentially say if column I(Check Market) or J(Check m2) have a value that says #NA then go no further and don't carry out the upload or the rest of the code. I think one of the problems might be I already have an IF loop - which is successful and has no errors associated with it.
This is my code so far
'Where Marked sht.Cells(Row,15) = "x" 'FIRST IF LOOP
If sht.Cells(lRow, 15) = "X" Then
'If I or J columns say #N/A then DO NOT continue
If IsError(sht.Cells(lRow, 9).Value) Then MsgBox "Error in Column 'Check Market'"
If IsError(sht.Cells(lRow, 10).Value) Then MsgBox "Error in Column 'Check m2'"
''''At the moment it is the above part that isn't successfully running, it notifies the user of an error but doesn't stop the process.
'Change blank spaces to be Null
*******
sSQL = *******Main part of code goes here******
'execute queries
********
'Put back all the 'null' values to blank
'''''
End If 'END OF IF X LOOP
I'm not clear if there's a possibility that both columns might have an error but I've provided for that just in case.
'If I or J say #N/A then dont proceed with upload.
If iserror(sht.Cells(lRow, 9).value) and iserror(sht.Cells(lRow, 10).value) Then
MsgBox "Errors in both Columns"
ElseIf iserror(sht.Cells(lRow, 9).value) Then
MsgBox "Error in Column 'Check Market'"
ElseIf iserror(sht.Cells(lRow, 10).value) Then
MsgBox "Error in Column 'Check KPI'"
Else: 'Continue
End if
Just adding to your original Code
I(9):Check Market J(10):Check m2
'If I or J say #N/A then dont proceed with upload.
If sht.Cells(lRow, 9).Value = "#N/A" Then
MsgBox "Error in Column 'Check Market'"
Exit Sub
ElseIf sht.Cells(lRow, 10).Value = "#N/A" Then
MsgBox "Error in Column 'Check KPI'"
Exit Sub
Else: 'Continue
Code for exiting with MsgBox
Dim response
response = MsgBox("My message here.", vbYesNo, "My Title")
If response = vbNo Then
Exit Sub
End If
MsgBox ("You clicked YES.")
Related
I am trying to use an if statement to check for blanks and return a msgbox if there are blank fields. If there are no blank fields it runs another block of code. However even when you fill out all the fields the msgbox is always returned and the next block of code doesn't run. I'm pretty new to VBA and writing code in gneneral so any advice would be helpful.
Code in question:
'Check required fields
If IsEmpty(C3) Then
MsgBox ("Fill out all required fields")
ElseIf IsEmpty(C7) = True Then
MsgBox ("Fill out all required fields")
ElseIf IsEmpty(C9) = True Then
MsgBox ("Fill out all required fields")
ElseIf IsEmpty(C11) = True Then
MsgBox ("Fill out all required fields")
ElseIf IsEmpty(C13) = True Then
MsgBox ("Fill out all required fields")
ElseIf IsEmpty(C17) = True Then
MsgBox ("Fill out all required fields")
Else
You would reference a range like this:
If Len(Range("C3").Value) = 0 Then
MsgBox "Fill out all required fields"
But shorter to do something like this:
If Application.CountA(Range("C3,C7,C11,C13,C17")) < 5 Then
MsgBox "Fill out all required fields"
End if
C7 is not a Range. Nor are C9, C11, C13 or C17
[C7] is a range. A better way to write this, however, would be ActiveSheet.Range("C7") or ActiveSheet.Cells(7,3)
You can then do nifty things like use Error Handling and SpecialCells:
On Error GoTo NoErrors
Dim BlankFields AS Long
BlankFields = ActiveSheet.Range("C7,C9,C11,C13,C17").SpecialCells(xlCellTypeBlanks).Count
MsgBox "Fill out all required fields" & vbCrLf & BlankFields & "field(s) remaining"
NoErrors:
On Error GoTo 0
I have a macro that changed a tab name based on cell value (A4) which contains a formula to give the sheet a unique name, but I wanted to see if it was possible to create special case handling occurrences when there's a duplicate. So here's the code:
Sub RenameFromA4()
Dim Msg As String, i As Integer
For i = 5 To Sheets.Count
If Sheets(i).Range("A4").Value = "" Then
Msg = "Sheet " & i & "(" & Sheets(i).Name & ") has no value in A4. Fix sheet, then rerun."
MsgBox Msg, vbExclamation
Exit Sub
Else
On Error GoTo ErrSheetName
Sheets(i).Name = Sheets(i).Range("A4").Value
On Error GoTo 0
End If
Next i
Exit Sub
ErrSheetName: Msg = "Sheet " & i & "(" & Sheets(i).Name & ") could not be renamed. Check if name already used."
MsgBox Msg, vbExclamation
End Sub
The trouble I run into is sometimes duplicates can arise and error out my whole macro where it comes to a complete halt. So I want to add a sequence that when the macro encounters a duplicate add the following formula in cell B3: ="IF(AND(C4="",D4="",D3="",C3=""),TRIM((MID(A2,FIND(":",A2)+2,20))),"")&IF(IFERROR(FIND("West",A2),0)>0," W","")&" "&TRIM(RIGHT(SUBSTITUTE(A2," ",REPT(" ",255)),255))"
and pick from the error or just go back to rerunning the macro.
Any insight on how I can't structure this will be helpful.
You can explicitly check for the existence of a sheet named the same as the value in B4 by using a function like what is described here: Test or check if sheet exists. Then, you can insert something like the following between your On Error... and Sheets(i).Name...:
On Error GoTo ErrSheetName
If SheetExists(Sheets(i).Range("A4").Value) Then
Sheets(i).Range("B3").Formula = "=IF(AND(C4="",D4="",D3="",C3=""),TRIM((MID(A2,FIND(": ",A2)+2,20))),"")&IF(IFERROR(FIND("West ",A2),0)>0,"W ","")&""&TRIM(RIGHT(SUBSTITUTE(A2,"",REPT("",255)),255))"
End If
Sheets(i).Name = Sheets(i).Range("A4").Value
I'm trying to check the name of the last worksheet and see if it equals "Field_10" . If it does, I don't want it to do anything but if it doesn't, I want to add new worksheets.
Expected output when there is only one sheet titled "Sheet1":
Outputs "Adding new sheets" and adds ten new sheets titled Field_1 all the way up to Field_10.
Expected output when the sheets (Field_1,..., Field_10) have already been added:
Outputs "New sheets already added" and exits if statement.
I've included MsgBoxes to help with debugging. The problem is, whether or not I check if the last sheet name is EQUAL to "Field_10" or NOT EQUAL to "Field_10", it always outputs "New sheets already added".
If ThisWorkbook.Worksheets(Worksheet.Count).Name = "Field_10" Then
'If ThisWorkbook.Worksheets(Worksheet.Count).Name <> "Field_10" Then
MsgBox ("New sheets already added")
Else
MsgBox ("Adding new sheets")
On Error Resume Next
For h = 1 To 10
Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "Field_" & CStr(h)
Next h
End If
Any help would be appreciated.
Use below sub:
Sub AddSheets()
Dim NewSheetNo As Integer
If ThisWorkbook.Worksheets(Worksheets.Count).Name = "Field_10" Then
MsgBox ("New sheets already added")
Else
MsgBox ("New sheets are being aded...")
On Error Resume Next
For NewSheetNo = 1 To 10
Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "Field_" & NewSheetNo
Next
End If
End Sub
I'd like to get some help with the following code. I am very new to this but I think it's an easy solution I'm just unable to retrofit suggestions from other searches into my code.
The msgboxes are working fine on the first pass to check if the text box values are correct but when I am checking to see if a formula result from a sheet is correct I'm getting 5 message boxes popping up.
Hope this makes sense, let me know if you have any suggestions!
`Private Sub SpeedCommand_Click()
Dim ctl As Control
If TextBox1AM180.Value > 12000 And TextBox1AM180.Value <> "" Then
MsgBox "Rate Value is out of range for this boom. Ensure rate value is less than 12,000 lbs./acre", vbExclamation, "Main Bin Application Rate"
Me.TextBox1AM180.SetFocus
Exit Sub
End If
If (TextBox2AM180.Value > 120 Or TextBox2AM180.Value < 20) And TextBox2AM180.Value <> "" Then
MsgBox "Density Value is out of range. Ensure density value is between 20 and 120 lbs./cu ft.", vbExclamation, "Main Bin Density"
Me.TextBox2AM180.SetFocus
Exit Sub
End If
If TextBox3AM180.Value > 12000 And TextBox3AM180.Value <> "" Then
MsgBox "Rate Value is out of range for this boom. Ensure rate value is less than 12,000 lbs./acre", vbExclamation, "Granular Bin Application Rate"
Me.TextBox3AM180.SetFocus
Exit Sub
End If
If (TextBox4AM180.Value > 120 Or TextBox4AM180.Value < 20) And TextBox4AM180.Value <> "" Then
MsgBox "Density Value is out of range. Ensure density value is between 20 and 120 lbs./cu ft.", vbExclamation, "Granular Bin Density"
Me.TextBox4AM180.SetFocus
Exit Sub
End If
' Write data to worksheet
With Range("B4")
.Offset(0, 0).Value = Me.TextBox1AM180.Value
.Offset(1, 0).Value = Me.TextBox2AM180.Value
.Offset(5, 0).Value = Me.TextBox3AM180.Value
.Offset(6, 0).Value = Me.TextBox4AM180.Value
End With
If Range("MaxSpeed1").Value > 30 Then
MsgBox "Based upon rate and density, speed is restricted by machine top end application speed."
Exit Sub
End If
If Range("MaxSpeed2").Value > 30 Then
MsgBox "Based upon rate and density, speed is restricted by machine top end application speed."
Exit Sub
End If
' Hide the form
frmAirmax.Hide
Use the Application.EnableEvents property to temporarily disable events from firing and then re-enable them when you're done.
Something like this:
Application.EnableEvents = False
With Range("B4")
.Offset(0, 0).Value = Me.TextBox1AM180.Value
.Offset(1, 0).Value = Me.TextBox2AM180.Value
.Offset(5, 0).Value = Me.TextBox3AM180.Value
.Offset(6, 0).Value = Me.TextBox4AM180.Value
End With
Application.EnableEvents = True
Hi All I have 2 formulas i'm trying to run but struggling to get the COUNTIF to calculate only if the cell isn't blank.
Sheets("Home").Select
If Range("A2:A14").Count = "13" Then
MsgBox "Current Load Full Please Complete & Export", vbCritical
Exit Sub
End If
2nd Code
Sheets("Home").Select
If Range("A2:A14").Count < "13" Then
MsgBox "Shipment is short do you want to continue?", vbCritical vbYesNo
Exit Sub
End If
On the 2nd code if vbYes then run code if vbNo then exit sub.
If you're trying to do some action depending on the condition "all cells in range A2:A14 are filled or not" - then this code might be the answer.
Sub check_count()
Sheets("Home").Select
Dim myRange As Range
Set myRange = Worksheets("Home").Range("A2:A14")
'using excel's built in function CountA to check count of non-blank cells
'if the count is 13 - then msgbox
If Application.WorksheetFunction.CountA(myRange) = 13 Then
MsgBox "Current Load Full Please Complete & Export", vbCritical
Exit Sub
'if the count is less then 13 - then do following
Else:
msg1 = MsgBox("Shipment is short do you want to continue?", vbYesNo)
If msg1 = vbYes Then
MsgBox "Enter missing products in A2:A14" 'you can run some code here as well
Else: Exit Sub
End If
End If
End Sub
Hope this answers your question.
In order to count all non-blank cells in a given range, you could use:
If ActiveSheet.Range("A2:A14").SpecialCells(xlCellTypeConstants).Count < 13 Then