Hi I've tried to run this code, but I have received 424 error, I'm new to Visual Basic and I don't know how to fix this bug :( Thanks so much
Dim a, b As Integer
'compute weekly change BAS
For a = 1 To 8
For b = 1 To 9
If Range("AA4").Cells(a, b) <> 0 Then
Changed.Cells(a, b) = (Range("AB4").Cells(a, b) - Range("AA4").Cells(a, b)) / Range("AA4").Cells(a, b)
Else
Changed.Cells(a, b) = "n/a"
End If
Next b
Next a
If Changed is a Worksheet Object, you need to Dim it and Set it.
Related
Hello whenever I try to run the following code I get a runtime error 13 type mismatch and I dont know why or how to fix this.
The idea is that if a = 8 and the first digit in cell(i,c) = 6 or 4 then excel should so something.
Dim a as integer
Dim c as integer
If a = 8 And (CInt(Left(.Cells(i, c), 1)) = 6 Or CInt(Left(.Cells(i, c), 1)) = 4) Then```
If the content of the cell you are reading is either empty or don't start with a digit, CInt will throw that runtime error 13.
You could use the Val-function instead (will return 0 for non-numeric strings), or you could check for the character "6" resp. "4" instead. I also would suggest to use an intermediate variable to save the character you are checking, makes the code much more readable.
dim firstChr as string
firstChr = Left(.Cells(i, c))
If a = 8 And (val(firstChr) = 6 Or val(firstChr) = 4) Then
' or
If a = 8 And (firstChr = "6" Or firstChr = "4") Then
I am getting the error "End If Without Block If" problem in VBA. Followin is my code. Could someone help me find out the mistake I am making?
Function ProjectedProductionPlan(Coverage As Double, Sales As Variant, ProjectedStock As Double) As Double
Dim count As Integer
Dim ResidualBalance As Double
Dim ProjectedPlan As Double
Dim k As Integer
Dim x As Integer
Dim y As Single
Dim s As Single
count = Sales.count
s = 0
ResidualBalance = ProjectedStock
i = 1
If Coverage < 1 Then
ProjectedPlan = (Sales(i) * Coverage) - ResidualBalance
ElseIf Coverage = 1 Then
ProjectedPlan = Sales(i) - ResidualBalance
Else
For k = 1 To count
Do Until k - Coverage > 0
x = k
y = Coverage - x
s = Sales(k) + s
Loop
Exit For
End If
ProjectedPlan = s + (Sales(x + 1) * y)
End Function
As per my comment:
To get rid of the compile error, you'll need to change Exit For into Next to create an actual iteration whereas Exit For is simply a statement within the loop to Exit the loop. Here is MS-documentation on For...Next loops.
However, you also make use of a Do Until...Loop. As I see your code, repairing the above would immediately throw your code in an infinite loop since no variable (neither k nor coverage) get's adjusted. So my suggestion would be to include an IF statement inside your For...Next loop instead. For example like this:
For k = 1 To count
If k - Coverage > 0 Then
Exit For
Else
x = k
y = Coverage - x
s = Sales(k) + s
End If
Next
I've not checked the rest of your code to see if implementing this is actually what you needed.
Small sidenote: using Integer data type variables is only going to bite you at one point. Use Long instead.
I have searched this forum and found several articles related to the same error message. Yet, none is related to my problem.
I have a table in Word with the following properties:
wddoc.tables(1).Rows.Count : 15
wddoc.tables(1).Columns.Count : 4
I was trying to copy the table to Excel using the following code:
For m = 1 To 15
For l = 1 To 4
ActiveSheet.Cells(m, l) = wdDoc.Tables(1).Cell(m, l)
Next l
Next m
The code worked fine for the first three rows but when it came to the fourth row (m=4, l=1), it threw the titled error message. What gives?
To get the correct results from a Word table with merged or split cells, use code like:
Dim wdCell As Object
For Each wdCell In wddoc.Tables(1).Range.Cells
With wdCell
ActiveSheet.Cells(.RowIndex, .ColumnIndex) = .Range.Text
End With
Next
You can handle different numbers of cells in each row, say from splits or merges, with something like this:
For m = 1 To wdDoc.Tables(1).Rows.Count
For l = 1 To wdDoc.Tables(1).Rows(m).Cells.Count
ActiveSheet.Cells(m, l) = wdDoc.Tables(1).Cell(m, l)
Next l
Next m
And then to clean things up a little
With wdDoc.Tables(1)
For m = 1 To .Rows.Count
For l = 1 To .Rows(m).Cells.Count
ActiveSheet.Cells(m, l) = .Cell(m, l)
Next l
Next m
End With
Hope that helps
I use the follow code in Excel VBA:
If ((B - A) > 180) Then
result = 1
ElseIf ((B - A) < -180) Then
result = 2
Else
result = 0
End If
In case (B - A) = 180, VBA returns result = 1.
The debugger shows the first statement of the If as True even if (B - A) = 180
Is it VBA acting funny or more probably me missing something?
Any help will be much appreciated.
I've been researching this for several days and I can't seem to figure out how to fill in a calculated value. I've got it down to this code, but when it goes through I get a type mismatch 13 debug error. I just want to divide the Quantity of one product by the attendance to find the penetration rate. The Quantity and Attendance are entered on a separate form called SouvenirProgramForm and the text boxes are called Quantity and Attendance. The form I want to calculate it on is my "Goal" form in the "CurrentPen" text box. I've tried any possible thing I could think of and find, but nothing is working. This code is the closest I've gotten, I put it in a button that says "Calculate" right next to where I want the Penetration Rate to appear. The error is somewhere with the z = x / y.
Dim x As Variant
Dim y As Variant
Dim z As Variant
x = SouvenirProgramForm.Quantity.Value
y = SouvenirProgramForm.Attendance.Value
z = x / y
CurrentPen.Value = z
Help would be greatly appreciated.
Its possible the values are being read as text since the variables are declared as variants.
Try to declare them as long or force the long value
Does the below work for you
Dim x As Long
Dim y As Long
Dim z As Long
x = SouvenirProgramForm.Quantity.Value
y = SouvenirProgramForm.Attendance.Value
z = x / y
CurrentPen.Value = z
or
Dim x As Variant
Dim y As Variant
Dim z As Variant
x = SouvenirProgramForm.Quantity.Value
y = SouvenirProgramForm.Attendance.Value
z = CLng(x) / CLng(y)
CurrentPen.Value = z
You can check the variable type by the below (Returns as a String and Not as an Integer or Long)
Dim X As Variant
X = "123"
MsgBox TypeName(X)
After sorting through everything here, and trying to make sense of it, I finally got it to work. Thank you for your help B Hart. Here's the final formula that made it work for me.
Dim a As String
Dim b As String
Dim c As Double
Dim d As Double
Dim e As Double
a = (Replace(SouvenirProgramFormG.Quantity, ",", ""))
b = (Replace(SouvenirProgramFormG.Attendance, ",", ""))
c = CDbl(a)
d = CDbl(b)
e = c / d * 100
TextBox1 = e
I hid this code in the continue button on the SouvenirProgramFormG page and then just set the text box on the other page to equal the new TextBox1. It's not pretty, but it gets the job done. Thank you for the help!