Strange output in simple function - Excel VBA - vba

I have this simple function in Excel VBA.
Public Function ubi() As Integer
Dim i As Integer
For i = 7 To 10
If IsNumeric(Cells(35, i).Value) Then
ubi = i
End If
Next
ubi = i
End Function
As you see, the values of i are supposed to be 7 or 8 or 9 or 10. But, when I test the function in the Excel Worksheet I find ubi()=11.
So, What could be the source of error in my code ?

Remove the ubi = i from outside the For...Next loop
The code keeps going while this is true: For i = 7 To 10 Every time you hit this Next it is incremental. When i gets increased to 11, the loop exits - but i is already set to 11.

Related

vba - Macro producing incorrect results when run, but when stepping into results are correct

I have a macro that inserts a VLOOKUP into a column. The macro has to take a number stored as text and convert it to a number, before looking up that number in another sheet.
The macro always produces the same results, such as reaching row 43 before starting to produce erroneous results however when using F8 to step through the code, these incorrect results are not produced.
The erroneous results are that the value placed into col 13 is not equal to the number stored as text. Mostly it seems as though values from rows above and below, sometimes 2 rows below are being inserted to col 13. Almost seems to me as if 2 different threads are running at 2 different speeds or something?
If anyone could have a look at the loop causing the errors I would be grateful, thanks.
For counter = 2 To NumRowsList
checker = CInt(Sheets("Sheet2").Cells(counter, 3)
Sheets("Sheet2").Cells(counter, 13).Value = checker
'Call WaitFor(0.5)
If checker < 4000 Then
Sheets("Sheet2").Cells(counter, 14) = "=VLOOKUP(M" & counter & ",Sheet4!E2:F126,2,FALSE)"
Else
Sheets("Sheet2").Cells(counter, 14) = "=VLOOKUP(M" & counter & ",Sheet5!B2:C200,2,FALSE)"
End If
Next counter
I have tried a few similar variations of this code, such as using the value stored in col 13 directly rather than using the cell reference in the VLOOKUP, always producing the same results.
I even used the waitfor function to try and create a delay hoping it may synchronise the operations, but it did not help and using a delay of more than 0.5 would cause the run time of the macro to be too big.
UPDATE:
I did not find a perfect solution, only a long hand work around. I simply combined the Vlookups onto a single sheet, and converted the numbers stored as text to numbers outside of the vba routine. This took the error away from the number calculation (just col C * 1), and then the vlookups were looking up the correct values. Thank you for the help, regardless.
you can avoid looping, checker and all those If-Then-Else, like follows
edited to account for VlookUp range depending on VlookUp value
With Worksheets("Sheet2")
.Range("N2", .Cells(NumRowsList, 14)).FormulaR1C1 = "=VLOOKUP(Value(RC3),IF(Value(RC3)<4000,Sheet4!R2C5:R126C6,Sheet4!R2C2:R200C3),2,FALSE)"
End With
The following works for me with my test data, but you'll need to see if it works for you... (also are you turning off calculation or events? I don't know if this might have an issue?)
I find it preferable to set a reference to the sheet you want to use rather than access it directly, and this may help?
Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Sheet2")
Dim VLURange As String, checker As Long
For counter = 2 To 200 ' NumRowsList
checker = CLng(ws.Cells(counter, 3).Value)
ws.Cells(counter, 13) = checker
VLURange = IIf(checker < 4000, "Sheet4!E2:F126", "Sheet5!B2:C200")
ws.Cells(counter, 14) = "=VLOOKUP(M" & counter & ", " & VLURange & ", 2, FALSE)"
Next counter

Foreach over Excel cells only returns the first row in run, but works properly when stepping through the method

I'm using an Excel spreadsheet as a data table for time period history. The structure is this:
ID Person Start End
1 Alan 5/1 5/3
2 Bobbi 5/3 5/4
3 Chuck 5/1 5/2
5 Eugenia 5/3 5/6
6 Chuck 5/10 5/12
Start and End are formatted as Date fields.
I wrote a method in a VBA module to query this table and return all rows for a given person, such as Chuck. In SQL, this is easy enough (select fields from History where Person = something). I am currently using a foreach loop and testing the value of Person. My loop reads as follows:
Public Sub UpdatePeriod(currentPeriod as timePeriod)
Dim indexCell As Range
Dim colPeriods As New Collection
Dim priorPeriod As timePeriod
For Each indexCell in ThisWorkbook.Worksheets("History").Range("A2:A" & Range("A" & Rows.Count).End(xlUp).Row).Cells
If Not (indexCell Is Nothing) And IsNumeric(indexCell) = True Then
Set priorPeriod = GetPeriodObject(indexCell)
If (priorPeriod.Person = currentPeriod.Person) Then
colPeriods.Add priorPeriod
End If
End If
Next
'Do stuff with the entries in colPeriods....
End Sub
I have set up the spreadsheet so that a certain sheet's Worksheet_Change event handler will pass a timePeriod object to this method. So far, everything works properly (though there's probably a better way).
When I test the method without breaking before the For Each, the loop only goes over row 2 (as row 1 contains the headers). But when I do break before the loop, the loop properly goes over all rows.
How do I improve this method to return all rows of interest?
(Note: The project is using VBA as a prototype, and eventually will be a proper application with a proper database. I aim to make the data interface look exactly the same as the application implementation.)
The most likely cause is that you haven't qualified the second Range call (or Rows). Use:
For Each indexCell in ThisWorkbook.Worksheets("History").Range("A2:A" & ThisWorkbook.Worksheets("History").Range("A" & ThisWorkbook.Worksheets("History").Rows.Count).End(xlUp).Row).Cells
Using a variable for the worksheet would simplify things!
With ThisWorkbook.Worksheets("History")
For Each indexCell in .Range("A2:A" & .Range("A" & Rows.Count).End(xlUp).Row).Cells
....
Next
End With

Searching for number of values in one table, resizing number of rows in another table

I'm trying to search for the number of times "Commodity" appears in the table column: Securities[Strategy]. I then want to take that number and resize a table (named: Commodity) on another worksheet accordingly. If it appears 6 times in column Securities[Strategy], the Commodity table should resize to 6 rows, and so on for any number.
I'm very new to VBA. When I run the following code nothing happens.
Sub AdjRow()
Dim Count1 As Integer
Count1 = Application.WorksheetFunction.CountIf(Range("Securities[Strategy]"), "Commodity")
Count1 = Count1 + 12
ActiveSheet.ListObjects("Commodity").Resize Range("$A$12:$J$" & Count1)
End Sub
To help with debugging you can either print key values to the immediate window using Debug.Print or to a messagebox using MsgBox. In this case though, I am curious if you get any error messages when you attempt to run the macro. Editing your code and attempting to run it, it runs fine when the result I get from the CountIf is larger than one, but aborts with an error when it is one or less. If the table you attempt to resize don't have headers, I assume the macro will run fine if CountIf is greater than zero.
Here is the the code, sample data, and output I got when attempting to debug your code. I ran the code 3 times, and had return values of 8, 1, and 2 from the CountIf-function. Note how I didn't get the third address for the listobject on the second run-through, this was because the code aborted when it tried to set the ListObject to only its headers (A2:J2).
Code
Option Explicit
Sub AdjRow()
Dim r As Range
Dim i As Long
Dim lo As ListObject
Set r = Sheet2.Range("Securities[Strategy]")
i = Application.WorksheetFunction.CountIf(r, "Test1")
i = i + 1
Set lo = Sheet1.ListObjects("Commodity")
Debug.Print i
Debug.Print r.Address
Debug.Print lo.Range.Address
lo.Resize Range("$A$2:$J$" & i)
Debug.Print lo.Range.Address
End Sub
Sheet1
Sheet2
Output to immediate window
9
$A$2:$A$10
$A$2:$J$9
$A$2:$J$9
2
$A$2:$A$10
$A$2:$J$9
3
$A$2:$A$11
$A$2:$J$9
$A$2:$J$3

Excel VBA Looping formula to change Range

I've written a code to manipulate data in L9 : DC9, But now I need to repeat this for L10 : DC10, L11 : DC11 and etc.. I've tried a For Next loop replacing the value in the range with Li:DCi and specifying (i) as 9 to 30 but I get an error. How can I make a loop for this function?
My current version of Excel is 2013
What you are looking for is a syntax like this
Sub LoopRows()
Dim i As Integer
For i = 9 To 30
ActiveSheet.Range("L" & i & ":DC" & i).Interior.Color = RGB(100, 100, 100)
Next i
End Sub
This example just formats the color of the cell in each row. Notice how I use the for-loop to create a looping range selection.
I suggest using Range("L9").Resize(21,50).Interior.Color = .. to do it in one statement.

For Loops not running inside If statement Excel VBA

I'm writing a code to update an inventory spreadsheet on the first of every month. I have very little knowledge of VBA but I understand the basics of programming. Please excuse my poor code.
The problem is that after the initial If statement check, when true it only runs the line directly below it (adding a new line) and does not execute the For Loops after it to edit the data.
Sub Auto_Open()
Dim stock(21)
If Date - Day(Date) + 1 = Date Then
Range("'Monthly Office Inventory'!A2").EntireRow.Insert
For i = 0 To 21
stock(i) = Range("Current Office Inventory'!A2").Offset(0, i).value
Next i
For x = 0 To 21
Range("'Monthly Office Inventory'!B14").Offset(0, x).value = stock(x)
Next x
End If
End Sub
Probably the easiest way to do this will be changing you condition to:
If Day(Date) = 1 Then
Now, I'm writting this on a Mac, but I think you'll get the idea (and correct it if some detail is wrong).
try to use this line Worksheets("Monthly Office Inventory").Range("B14:W14").Value = Worksheets("Current Office Inventory").Range("A2:V2").Value instead your loops
#simoco
https://stackoverflow.com/users/3067523/simoco