Browsing I didnt find nothing especific as I need indeed.
The code I'm looking for, uses "AG6" and "AK6".
AG6 is my client (44 clients) and ak6 my product (4 products).
as a loop, for AG6 = 1 and ak6 = 1 it'll create a power point presentation pasting a certain range [A1:T35], and so on. AG6 still 1 while ak6 <= 4, and everytime ak6 changes it'll create the presentation. when ak6 reaches 4, ag6 goes to 2 and when ak6 reaches 4, ag6 +1 again until 44.
I tried to be more clear I could. I've already done the ppt code but i'm stuck on that loop.
Thank you in advance !!
see ya
Is this what you are looking for? I'm not sure from your title whether your problem is with the looping code or with invoking PowerPoint - what does "use replace code" mean? Anyway, here's a simple nested loop - the "Debug" line is just to show that the loop actually works.
Sub testloop()
Dim client As Integer
Dim product As Integer
With ActiveSheet
For client = 1 To 44
.Range("AG6") = client
For product = 1 To 4
.Range("AK6") = product
'Do Something Here
Debug.Print .Range("AG6") & " - " & .Range("AK6")
Next product
Next client
End With
End Sub
It was my mistake when I was writing another question, forget it !
Exemple:
1
. client as 1 and prod as 1
client as 1 and prod as 2
client as 1 and prod as 3
client as 1 and prod as 4
when prod reaches 4, client goes to 2 and do the same thing.
between every time prod variable changes, i'll create the ppt apresentation.
Related
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
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
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
I am building an algorithm in excel vba to search for paths in a network. I am new to programming these types of problems. Please don't suggest alternate software. The problem should be very simple.
Problem Description:
Search a collection of data representing flows on arcs (3 "columns": from, to, flow)
Identify path (start at source, find flow to "to", look up that "to" in the "from" field, find flow to that "to", and so on until another "from" cannot be found) from source to end of each path.
Data looks like this:
fromnode tonode flow
1 2 4
2 3 3
3 4 2
4 5 1
7 6 1
8 7 2
Biggest hurdle:
I am using a Collection to hold this data and setting it with the code below.
Dim y As Collection
Set y = New Collection
y.Add Sheets("FlowDecomp_Solve").Range("fromtoflow").Value
The data gets pulled in, but it looks like this:
(tried a pic, but I'm a new user)
Item 1
Item 1(1)
Item 1 (1,1) 1
Item 1 (1,2) 2
Item 1 (1,3) 4
So it shows that the Collection has 1 item instead of my # of arcs. How do I access the Item1(1,2) type address for the collection. Does each special value have a unique key? How do I search through my collection and remove a specific row after I've used it in the code?
THANK YOU SO MUCH FOR ANY HELP.
After much trial and error, I've decided to make my original data an array and search it like this (d is down the array, a is across):
For d = LBound(arcflow(), 1) To UBound(arcflow(), 1)
For a = LBound(arcflow(), 2) To UBound(arcflow(), 2)
If a = 1 Then
fromnode = arcflow(d, a)
ElseIf a = 2 Then
tonode = arcflow(d, a)
ElseIf a = 3 Then
flow = arcflow(d, a)
'write into node-node matrix
Sheets("FlowDecomp_Solve").Range("nodenodemtrx").Cells(fromnode, tonode).value = 1
End If
Next a
Next d
Then to trace the path, I add the from and to nodes to a collection called Path until it can't find another arc and I write the path out, empty the collection, update the values, and start again.
Hope this helps someone else...
I am trying to write a loop that adds odd numbers up to a certain point. How can I get the integer i to go up by 2. I know there is a way, it's something like i by 2, but I don't remember the exact command. If anyone could please help me, I would appreciate it
Do you mean:
For i = 1 To 9 Step 2
Next i
Or:
For i = 1 To 10
If i Mod 2 = 1 Then
'// Odd
End If
Next i