VBA Compile Error Expected Sub, Function Or Property - excel

I keep getting the compile error resposne Expected Sub, Function or Property, when trying to run below VBA code. The problem is with the called sub procedure. Any insight as to why will be appreciated.
Result5iii = Result6i - Result4
ResultGi = Result5iii - Range("G2").Value
Range("W3").Value = ("ResultGi")
Range("X3").Value = (" Can Use: [ " & ResultGi & " ]")
Range("Y3").Value = ("2 Nxt will not be " & ResultGi)
Range("Z3").Value = (" 19|15|5ii ")
'Minus1Rule_OriginalsG'
If Result5iii = 5 And Range("G2").Value = 1 And ResultGi = 4. Then
Call Minus1Rule_5OriginalsG
The called Procedure is;
Public Sub Minus1Rule_5OriginalsG()
Dim ResultG As Double, Result5i As Double, Result1 As Double, Result1a As Double
'1. -1 Rule'
'For Originals'
Range("W2").Value = (" 0, 5 [ " & ResultG & " ]")
Range("X2").Value = (" Can Use: 0, 5 [ " & ResultG & " ]")
Range("Y2").Value = ("2 Nxt will not be " & "0, 5" & "Reason: -1 Rule For Originals")
Range("Z2").Value = (Result1 & "|" & Result1a & " 5 - 1" & "|" & "5i ")
End Sub

Related

Make Two Results within Same Block Populate Together

Provided all the conditions in the code below is met, can anyone provide me with an idea as to why ResultG populates in the excel worksheet but ResultGi does not. I have tried re-arranging and linking various parts of both codes together but to no avail.
Result6 = (23)
Result6i = (24)
If Result1 = 19 And ((Result2ai >= 7) And (Result2ai <= 14)) Then
Result6 = (23)
Result6i = (24)
If Result1a = 15 And ((Result3a >= 6) And (Result3a <= 10)) Then
Result4 = 19
Result5i = Result6 - Result4
ResultG = Result5i - Range("G2").Value
Range("W2").Value = ("ResultG")
Range("X2").Value = (" Can Use: [ " & ResultG & " ]")
Range("Y2").Value = ("2 Nxt will not be " & ResultG)
Range("Z2").Value = (" 19|15|5i ")
Result5iii = Result6i - Result4
ResultGi = Result5iii - Range("G2").Value
Range("W3").Value = ("ResultGi")
Range("X3").Value = (" Can Use: [ " & ResultGi & " ]")
Range("Y3").Value = ("2 Nxt will not be " & ResultGi)
Range("Z3").Value = (" 19|15|5ii ")
End If
End If
End Sub

Run time error 1004: method "select of object_worksheet failed

After uploading the data in Excel file and running the macro code in it to publish the report, getting the
Runtime error 1004 - Method "Select of Object"_Worksheet" Failed
MailBCC = ""
MailSubject = "Stack Ranking Result : " & Sheet3.Cells(EmployeeDetailRowNo, 2) & "(" & Sheet3.Cells(EmployeeDetailRowNo, 1) & ")"
Sheet4.Select
Sheet4.Range("A1").Select
Sheet4.Range("C4").Value = Sheet2.Cells(StackRowNo, 1)
Call SendingMail(MailTo, MailCC, MailBCC, Sheet4.Range("A1:I33"), MailSubject)
StackRowNo = StackRowNo + 1
Loop
Sheet4.Select
Sheet4.Range("A1").Select
End Sub
Take out all those Selects and it should work:
MailBCC = ""
MailSubject = "Stack Ranking Result : " & Sheet3.Cells(EmployeeDetailRowNo, 2) & _
"(" & Sheet3.Cells(EmployeeDetailRowNo, 1) & ")"
Sheet4.Range("C4").Value = Sheet2.Cells(StackRowNo, 1)
Call SendingMail(MailTo, MailCC, MailBCC, Sheet4.Range("A1:I33"), MailSubject)
StackRowNo = StackRowNo + 1
Loop
End Sub
I'll leave this here. You should definitely read it before doing further coding.

Message Box that loops and displays multiple records

I'm trying to create a message box that displays various entries for an undetermined number of columns.
I believe I want a loop, but I want the message box to display all the records available in a range and not create a new message box for each item in the range.
I want the message box to look like
Loan Summary(Price, Range, Standard Deviation):
Loan 1: (100, 5, 2)
Loan 2: (102, 4, 3)
and so on but the number of records (loans) will change each time.
I have the below code. How do I add a new line for each record in a range?
For theRep = 1 To wsv.Range("J3").Value
Average1 = Range("loanSummary").Offset(0, theRep)
Range1 = Range("loanSummary").Offset(1, theRep)
StdDev1 = Range("loanSummary").Offset(2, theRep)
MsgBox "Loan Summary (Price, Range, Standard Deviation):" & vbCrLf & vbTab & "Loan 1: " & Format(Average1, "##0.00") & ", " & Format(Range1, "##0.00") & ", " & Format(StdDev1, "##0.00")
Next
Use a string variable to hold the data then after the loop present the string in one MsgBox
Dim str As String
str = "Loan Summary (Price, Range, Standard Deviation):" & vbCrLf & vbTab
For theRep = 1 To wsv.Range("J3").Value
Average1 = Range("loanSummary").Offset(0, theRep)
Range1 = Range("loanSummary").Offset(1, theRep)
StdDev1 = Range("loanSummary").Offset(2, theRep)
str = str & "Loan " & theRep & ": (" & Format(Average1, "##0.00") & ", " & Format(Range1, "##0.00") & ", " & Format(StdDev1, "##0.00") & ")" & vbCrLf & vbTab
Next
MsgBox str

How do I adapt this program in VBA so that the original temperature is placed in a cell next to the converted temperature?

This is my code... It keeps saying
SubConvertTemp()
Centigrade=MsgBox(Do you want to convert to centigrade?"),vbYesNo)
InputNumber=Val(InputBox("Enter a temperature for conversion?"))
If Centigradde= vbYes Then
ConvertedTemp= ((9/5 *InputNumber)+32)
MsgBox((InputNumber & " degrees C converts to " & ConvertedTemp &" Degrees F."))
Else: ConvertedTemp=(5/9)*(InputNumber-32)
MsgBox(InputNumber & "degrees F converts to " & ConvertedTemp & "degrees C.")
Worksheets.("Sheet 1).Cells(InputNumber,ConvertedTemp).Offset(-1,0).Value="Name"
end sub
This code keeps saying syntax error? Not sure why
A very small number of "syntax"typos:
Sub ConvertTemp()
Centigrade = MsgBox("Do you want to convert to centigrade?", vbYesNo)
InputNumber = Val(InputBox("Enter a temperature for conversion?"))
If Centigradde = vbYes Then
ConvertedTemp = ((9 / 5 * InputNumber) + 32)
MsgBox ((InputNumber & " degrees C converts to " & ConvertedTemp & " Degrees F."))
Else
ConvertedTemp = (5 / 9) * (InputNumber - 32)
End If
MsgBox (InputNumber & " degrees F converts to " & ConvertedTemp & " degrees C.")
Worksheets("Sheet1").Cells(InputNumber, ConvertedTemp).Offset(-1, 0).Value = "Name"
End Sub
EDIT#1:
A better version:
Option Explicit
Sub ConvertTemp()
Dim Centigrade As String, InputNumber As Variant, ConvertedTemp As Double
Centigrade = MsgBox("Do you want to convert to centigrade?", vbYesNo)
InputNumber = Val(InputBox("Enter a temperature for conversion?"))
If Centigrade = vbYes Then
ConvertedTemp = ((9 / 5 * InputNumber) + 32)
MsgBox ((InputNumber & " degrees C converts to " & ConvertedTemp & " Degrees F."))
Else
ConvertedTemp = (5 / 9) * (InputNumber - 32)
End If
MsgBox (InputNumber & " degrees F converts to " & ConvertedTemp & " degrees C.")
Worksheets("Sheet1").Cells(InputNumber, ConvertedTemp).Offset(-1, 0).Value = "Name"
End Sub

Crazy issue with string formatting

I am running into an issue that is driving me crazy. I have two FOR loops in my macro that each have a counter to keep track of how many times a certain process was performed. The counters work great and at the end of the loop contain the correct numbers. The next thing I have to do is to format the counts into a five digit number with leading zeros. I have tried this using two different approaches (see below).
cCount = String(5 - Len(cTemp), "0") & cTemp
mCount = String(5 - Len(mTemp), "0") & mTemp
or
cCount = Format(cTemp, "00000")
mCount = Format(mTemp, "00000")
The problem is with the second counter. As I step through it, the first format formula works, but the second line does not, regardless of which version above that I use. Now here is the thing, if, while I am still in the macro, I go and change the name of mCount to anything else, for example mCnt, and then move the macro step back up to reprocess that line, it will correctly format the variable. But it isn't the name, because if I then run the macro again using mCnt, it will do the same thing. I can change it back to mCount and it will work.
All variables are dimmed as Integers. An example of what I am looking for would be if mTemp is 15, then mCount would be 00015. However, mCount is just coming back as 15. cCount is working fine.
The fact that everything is correct and that I can make it work if I pause the macro, change the variable name, and reprocess the line, has got me completely at a loss as to what the issue is.
Sub MakePay()
Dim strFileToOpen As String
Dim payDate, payTab, payCheckTemp, payCheck, payAccTemp As String
Dim payAcc, payAmount, payTotalC, payTotalM As String
Dim savePath As String
Dim payFileNameCLP, payFileNameMF As String
Dim payString1, payString2, payString3, payString4, payString5, payString6 As String
Dim payString7, payString8, payString9 As String
Dim rCnt, i, j, cTemp, cCount, mTemp, mCount As Integer
Dim payTotalMTemp, payAmountTemp, payTotalCTemp As Double
' Set date
payDate = Format(Now(), "yyyymmddhhmmss")
' Ask for check number and format to field length
payCheckTemp = InputBox("Please enter the check number.")
payCheck = payCheckTemp & String(15 - Len(payCheckTemp), " ")
' Create file names and open text files for writing
payFileNameCLP = "CLP_" & payDate & "_01.txt"
payFileNameMF = "MDF_" & payDate & "_01.txt"
savePath = Environ("USERPROFILE") & "\Desktop\"
Open savePath & payFileNameCLP For Output As #1
Open savePath & payFileNameMF For Output As #2
' Build header rows and print them
payString1 = "100"
payString2 = "200 C"
Print #1, payString1
Print #1, payString2
Print #2, payString1
Print #2, payString2
' reset counters for number of claims and total dollar amounts in files
cTemp = 0
mTemp = 0
payTotalCTemp = 0
payTotalMTemp = 0
For i = 1 To Sheets.Count
' Process the Clearpoint tab
If Left(Sheets(i).Name, 3) = "CLE" Then
Sheets(i).Activate
rCnt = Cells(Rows.Count, 1).End(xlUp).Row
For j = 3 To (rCnt - 1)
' Read accession # and format it for field length
payAccTemp = Cells(j, 3).Value
payAcc = payAccTemp & String(17 - Len(payAccTemp), " ")
' Read payment amount, if $0, skip
payAmountTemp = Format(Cells(j, 5).Value2, "#,###.00")
If payAmountTemp = "" Then
GoTo SkipCDL
End If
' Add payment to total Clearpoint payments
payTotalCTemp = payTotalCTemp + payAmountTemp
' Format payment by deleting decimal and then format to field length
payAmount = Format(payAmountTemp * 100, "0000000;-000000")
' Build payment strings and print them
payString3 = "400" & String(10, " ") & payAcc & payCheck
payString4 = "450" & String(10, " ") & payAcc & String(150, " ") & payAmount
payString5 = "500" & String(10, " ") & payAcc & String(73, " ") & payAmount
Print #1, payString3
Print #1, payString4
Print #1, payString5
' Increase Clearpoint patient count
cTemp = cTemp + 1
SkipCDL:
Next j
' Process Medfusion tab
ElseIf Left(Sheets(i).Name, 3) = "MED" Then
Sheets(i).Activate
rCnt = Cells(Rows.Count, 1).End(xlUp).Row
For j = 3 To (rCnt - 1)
' Read accession # and format it for field length
payAccTemp = Cells(j, 3).Value
payAcc = payAccTemp & String(17 - Len(payAccTemp), " ")
' Read payment amount, if $0, skip
payAmountTemp = Format(Cells(j, 5).Value2, "#,###.00")
If payAmountTemp = "" Then
GoTo SkipMDF
End If
' Add payment to total Medfusion payments
payTotalMTemp = payTotalMTemp + payAmountTemp
' Format payment by deleting decimal and then format to field length
payAmount = Format(payAmountTemp * 100, "0000000;-000000")
' Build payment strings and print them
payString3 = "400" & String(10, " ") & payAcc & payCheck
payString4 = "450" & String(10, " ") & payAcc & String(150, " ") & payAmount
payString5 = "500" & String(10, " ") & payAcc & String(73, " ") & payAmount
Print #2, payString3
Print #2, payString4
Print #2, payString5
' Increase Medfusion count
mTemp = mTemp + 1
SkipMDF:
Next j
End If
Next i
' Format patient counter and total payment to field length
cCount = Format(cTemp, "00000")
mCount = Format(mTemp, "00000")
payTotalC = Format(payTotalCTemp * 100, "000000000;-00000000")
payTotalM = Format(payTotalMTemp * 100, "000000000;-00000000")
' Build footer strings and print them
payString6 = "800" & String(26, " ") & "9999" & cCount & String(131, " ") & payTotalC
payString7 = "800" & String(26, " ") & "9999" & mCount & String(131, " ") & payTotalM
payString8 = "900" & String(57, " ") & "099990" & cCount & String(154, " ") & String(2, "0") & payTotalC
payString9 = "900" & String(57, " ") & "099990" & mCount & String(154, " ") & String(2, "0") & payTotalM
Print #1, payString6
Print #2, payString7
Print #1, payString8
Print #2, payString9
' Close all files
Application.DisplayAlerts = False
Close #1
Close #2
Application.DisplayAlerts = True
End Sub
The issue is with how the variables are declared.
In VBA/classic vb, all declarations should be on their own line OR have the correct data type specified, otherwise you risk accidentally creating a Variant data type, which can masquerade as any other data type, which the VB engine has rules for determining the type.
See https://msdn.microsoft.com/en-us/library/56ht941f(v=vs.90).aspx
Also, whenever coding in VBA make sure Option Explicit is declared at the top of any new code module. It will save you loads of pain in the future.
Also, you are trying to push String formatting into an Integer, which cannot happen.
So...
Option Explicit
.....
'Dim i, j as Integer 'BAD i is a variant, j is an integer
Dim i As Integer
Dim j As Integer 'GOOD both are Integers
'OR
Dim x As Integer, y as Integer 'I believe this will work too
dim displayI as String
i = 23
displayI = Format(i, "00000")
In your code why not just format inline?
payString6 = "800" & String(26, " ") & "9999" & Format(cCount,"00000") & String(131, " ") & payTotalC

Resources