For Loops not running inside If statement Excel VBA - 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

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

Runtime Error 1004: Unable to get the PivotTables property of the Worksheet class

I recorded this macro to update the date range of 16 charts. But, as you can see I am getting a Run-time error.
I have looked at other threads on stackoverflow that relate to this but none come close. The darn help button on excel doesn't help either. Can you please advise? Here is the code:
ActiveSheet.ChartObjects("Chart 18").Activate
ActiveChart.Axes(xlCategory).Select
ActiveSheet.ChartObjects("Chart 18").Activate
With ActiveSheet.PivotTables("PivotTable2").PivotFields("Date")
.PivotItems("Sep-15").Visible = False
.PivotItems("Aug-14").Visible = True
End With
Here's some simple code that should get you close. You will need to change "MySheet" in this code to the name of the sheet containing the pivot table in your workbook, and your Date field must truly be text formatted in the "Mmm-YY" format.
Sub ShowThirteenDatesStartingLastMonth()
Sheets("MySheet").PivotTables("PivotTable2").PivotCache.Refresh
Dim Dt As String
With Sheets("MySheet").PivotTables("PivotTable2").PivotFields("Date")
For h = 1 To .PivotItems.Count - 1
On Error Resume Next
.PivotItems(h).Visible = False
Next h
For i = 1 To 13
On Error Resume Next
Dt = Format(DateSerial(Year(Date), Month(Date) - i, 1), "Mmm-YY")
.PivotItems(Dt).Visible = True
Next i
End With
End Sub
It's hard to know without an example of your workbook, but I would try naming the sheet rather than using "activesheet". It's also typically a bad idea to activate or select anything, and instead you should allow the code to do as much work as it can without selecting anything.
Something like this might work better:
With Sheets("MySheet").PivotTables("PivotTable2").PivotFields("Date")
.PivotItems("Sep-15").Visible = False
.PivotItems("Aug-14").Visible = True
End With
As Josh said, you should reference the exact sheet name and maybe even the exact Pivot table name.
Also, you should avoid using .Select and instead declare a variable for each pivot chart, for example.

VBA deleting name throws object required

I just want to delete a few names that get created every time a querytable gets created. They are all in 3 sheets starting with 0048,0114,0715, so I would just delete all names that start with any of them. However, I get the rejection "object required" in the if clause when I use rName.Delete. Without this, the code runs fine and prints all the names. Also, if I do range(rName).delete it would delete the ranges in the workbook (not what I want, though).
Sub delNames()
Dim strStartString(0 To 2) As String
strStartString(0) = "'0048'!mta"
strStartString(1) = "'0114'!mta"
strStartString(2) = "'0715!'mta"
For Each rName In ActiveWorkbook.Names
For Each ss In strStartString
If rName.Name Like ss & "*" Then
Debug.Print rName.Name
rName.Delete
End If
Next ss
Next rName
End Sub
Any idea what I am doing wrong here?
Posting Tim's comment as solution
Modifying a collection while looping through it can cause problems. Try using a for next loop counting back through the names in reverse - For x = ActiveWorkbook.Names.Count to 1 Step -1 : ActiveWorkbook.Names(x).Delete – Tim Williams 49 mins ago

Reading in column issue

I am running into a slight problem when running my VBA code. I am trying to retrieve and check the information in a specified column in excel. It will run 16 times retrieving the information until the 17 time where it doesn't even give me the whats in the column from the workbook. Can you please help or guide me to the solution?
Dim CheckingWhatsInCell As String
Dim i As Integer
Dim j As Integer
Dim ToWorkbook As Workbook
ThisWorkbook.Activate
For i = 1 To 20
CheckingWhatsInCell = Trim(Range("K" & i).Value)
If CheckingWhatsInCell = "Albuquerque NM" Then
Set ToWorkbook = Workbooks.Open("C:\Users\mgonza-c\Documents\TerritoryAlbuquerqueNM.xlsx")
For j = 1 To 139
ToWorkbook.Worksheets("Sheet1").Cells(i, j) = ThisWorkbook.Sheets("Sheet2").Cells(i, j).Value
Next j
End If
Next i
ToWorkbook.Save
ToWorkbook.Close
End Sub
Thanks!
CheckingWhatsInCell = Trim(Range("K" & i).Value)
This code will be looking in ThisWorkbook, which is Active, and in whichever Sheet was last active. Perhaps it's looking in the wrong sheet. The situation will also change when the other workbook is opened, making it active.
Then, as Julien suggests, there may be an odd value in K17.
Also note that you are attempting to Save and Close ToWorkbook even though it may never be assigned to a Workbook.