help some code

Forum home » Delegate support and help forum » Microsoft Excel VBA Training and help » Help with some code!

Help with some code!

resolvedResolved · Medium Priority · Version 2013

David has attended:
Excel Advanced course
Excel VBA Introduction course
Excel VBA Intermediate course
Excel VBA Advanced course

Help with some code!

Hello,

I have written some code but it is not doing what I hoped it would do.

First let me explain what I am trying to do:

I have 10 regions and for each region I have a number of items I want to pick.

The name of the region is located in cells AX17 to BG17 and the number of items for each region is located in AX38 to BG38.

Once this is determined I want to go to my data table which is located in cells A18:Q (the row changes).

In column A i have the name of the items. In column D i have the Region they belong to and in column Q i have the rank.

If the item matches the region and the number of items is equal to or less than the rank (ie if the number is 3 then any item with a rank of 1, 2 or 3) then I want to copy the name of the item to the new sheet.

I hope this makes sense. So here is the code I have written, any help would be appreciated:

Dim i As Integer
Dim r As Integer
Dim region As String
Dim StockCount As Integer
Dim lastrow As Integer
Dim Ticker As String
Dim destCell As range
lastrow = Cells(Rows.Count, "A").End(xlUp).Row

For i = 50 To 59
For r = 18 To lastrow
StockCount = Cells(38, i).Value
If StockCount = 0 Then Exit For
region = Cells(17, i)
If Cells(1, r).Offset(0, 4) = region Then
If Cells(1, r).Offset(0, 17) <= StockCount Then
Ticker = Cells(1, r).Value
Set destCell = Worksheets("Stage 3").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
destCell.Value = Ticker
Else
Exit For
End If
End If
Next r
Next i

End Sub

 

Training courses

 

Training information:

Welcome. Please choose your application (eg. Excel) and then post your question.

Our Microsoft Qualified trainers will then respond within 24 hours (working days).

Frequently Asked Questions
What does 'Resolved' mean?

Any suggestions, questions or comments? Please post in the Improve the forum thread.


 

Excel tip:

Quickly select a block of data

To quickly select a block of data make sure your active cell is somewhere whithin the block of data and then press Ctrl+* or Ctrl+Shift+8.

View all Excel hints and tips


Server loaded in 0.06 secs.