Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8
Default Stock Macro Loop

Hi All,

I have a problem where I want to run a macro on all rows selected by the
user. The problem lies in, that the macro when ran can create x amount
of new rows below the current one, based on a number entered by the user.
So what I want to do, is after compleating one row, deselect the row,
and the (x) number of new rows created by processing the current row.

Here is my code:

Sub IndividualProducts()

Dim Answer As VbMsgBoxResult ' Answer to our question
Answer = MsgBox("This function assumes that the colour codes are
places in column A. If this is not the case the function will fail, and
there will be no undo function." & vbCrLf & " Do you wish to proceed?", _
vbQuestion + vbYesNo, "Confirm") ' Ask the question and get
the results
If Answer = vbNo Then
Exit Sub ' If user clicks no then exit sub
End If
Dim Product As Integer
Dim Description As Integer

Product = InputBox("Please enter the column which contains the
Product ID, i.e 1", "User input")
Description = InputBox("Please Enter the column which contains the
description field of the item", "User Input")


Dim myCell As Range ' Declaire are range
Dim myCell2 As Range ' Declaire are range
Dim myR As Range ' Declaire are range
Dim myCodes As Variant ' Colour codes
Dim myFullCodes As Variant ' Full Colour Names
Dim i As Integer ' Itterator

Set myR = Selection.Cells(1).EntireRow ' Get the selected row
Set myCell = myR.Cells(1, 1) ' Get the cell containign
colour codes
myCodes = Split(myCell.Value, ",") ' Split the cell, via
the delimiter
Set myCell2 = myR.Cells(1, 2)
myFullCodes = Split(myCell2.Value, ",")

If LBound(myCodes) < UBound(myCodes) Then
myR.Copy ' Copy the range
myR.Resize(UBound(myCodes) -
LBound(myCodes)).Offset(1).Insert ' Resize it
'myR.Resize(UBound(myCodes) -
LBound(myCodes)).Offset(UBound(myCodes)).Insert
For i = LBound(myCodes) To UBound(myCodes) ' Loop
through the colour code list
myCell(i + 1, Product).Value = myCell(i + 1,
Product).Value & "/" & myCodes(i) ' Create individual product codes
myCell2(i + 1, 4).Value = myCell2(i + 1, 4).Value &
" " & myFullCodes(i)
Next i
End If
End Sub

If anyone could help I would be forever in your debt.

Kind regards
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9,101
Default Stock Macro Loop

I haven't tested this code. but the solution is to get a count of the number
of times you need to go through the outer loop before adding any rows. then
keep a 2nd counter to indicate which row you are processing.

Sub xyz()

MyRows = Selection.Rows.Count
StartRow = Selection.Row
EndRow = StartRow + RowCount - 1

CurrentRow = Start_Row
For RowCount = 0 To (MyRows + 1)
Set myR = Cells(CurrentRow, "A").EntireRow ' Get the selected
row
' Get the cell containign colour codes
Set myCell = myR.Cells(1, 1)
' Split the cell, via the delimiter
myCodes = Split(myCell.Value, ",")
Set myCell2 = myR.Cells(1, 2)
myFullCodes = Split(myCell2.Value, ",")

If StarrtRow < EndRow Then
myR.Copy ' Copy the range
myR.Resize(UBound(myCodes) - _
LBound(myCodes)).Offset(1).Insert ' Resize it

For i = LBound(myCodes) To UBound(myCodes)
' Create individual product codes
myCell(i + 1, Product).Value = _
myCell(i + 1, Product).Value & "/" & myCodes(i)
myCell2(i + 1, 4).Value = myCell2(i + 1, 4).Value & _
" " & myFullCodes(i)
Next i
End If
Next RowCount
End Sub


"Materialised" wrote:

Hi All,

I have a problem where I want to run a macro on all rows selected by the
user. The problem lies in, that the macro when ran can create x amount
of new rows below the current one, based on a number entered by the user.
So what I want to do, is after compleating one row, deselect the row,
and the (x) number of new rows created by processing the current row.

Here is my code:

Sub IndividualProducts()

Dim Answer As VbMsgBoxResult ' Answer to our question
Answer = MsgBox("This function assumes that the colour codes are
places in column A. If this is not the case the function will fail, and
there will be no undo function." & vbCrLf & " Do you wish to proceed?", _
vbQuestion + vbYesNo, "Confirm") ' Ask the question and get
the results
If Answer = vbNo Then
Exit Sub ' If user clicks no then exit sub
End If
Dim Product As Integer
Dim Description As Integer

Product = InputBox("Please enter the column which contains the
Product ID, i.e 1", "User input")
Description = InputBox("Please Enter the column which contains the
description field of the item", "User Input")


Dim myCell As Range ' Declaire are range
Dim myCell2 As Range ' Declaire are range
Dim myR As Range ' Declaire are range
Dim myCodes As Variant ' Colour codes
Dim myFullCodes As Variant ' Full Colour Names
Dim i As Integer ' Itterator

Set myR = Selection.Cells(1).EntireRow ' Get the selected row
Set myCell = myR.Cells(1, 1) ' Get the cell containign
colour codes
myCodes = Split(myCell.Value, ",") ' Split the cell, via
the delimiter
Set myCell2 = myR.Cells(1, 2)
myFullCodes = Split(myCell2.Value, ",")

If LBound(myCodes) < UBound(myCodes) Then
myR.Copy ' Copy the range
myR.Resize(UBound(myCodes) -
LBound(myCodes)).Offset(1).Insert ' Resize it
'myR.Resize(UBound(myCodes) -
LBound(myCodes)).Offset(UBound(myCodes)).Insert
For i = LBound(myCodes) To UBound(myCodes) ' Loop
through the colour code list
myCell(i + 1, Product).Value = myCell(i + 1,
Product).Value & "/" & myCodes(i) ' Create individual product codes
myCell2(i + 1, 4).Value = myCell2(i + 1, 4).Value &
" " & myFullCodes(i)
Next i
End If
End Sub

If anyone could help I would be forever in your debt.

Kind regards

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How many days the ready stock+expected stock will last as allocate Narnimar Excel Discussion (Misc queries) 0 September 16th 08 05:57 PM
Excel - Stock Macro Help Materialised Excel Programming 2 April 19th 07 03:33 PM
Stock control formulas, counting and reporting remaining stock santaviga Excel Programming 2 October 23rd 06 01:34 PM
Web Query With Multiple Stock Symbols & Stock Scouter Manfred Excel Worksheet Functions 0 March 1st 06 09:13 PM
Office 2003 Stock Action Add-In - Stock Handling Capacity/Numbers nick Excel Worksheet Functions 0 January 2nd 06 09:22 PM


All times are GMT +1. The time now is 12:28 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"