Help with making a loop or next statement

harts

Board Regular
Joined
Apr 5, 2011
Messages
111
Office Version
  1. 365
Platform
  1. Windows
I have a list of names from Z2:Z19 and then a count formula in the cell next from AA2:AA19. The count formula counts how many times the name is left on a different worksheet. I am makeing a list on what is remaining. I have a formula to write it one by one but having issues on making 1 code to do it all. Below is what I have so far. Thanks for your time.

Private Sub CommandButton1_Click()
Range("a2:a500").Select
Selection.ClearContents
Dim arrValues() As Variant
Dim lngLastrow As Long
Dim intCol As Integer
i = Range("AA2").Value
x = Range("AA3").Value

If ActiveSheet.Range("A2") > "" Then
lngLastrow = ActiveSheet.Columns(1).Find(What:="*", After:=[A1], _
SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
Else
lngLastrow = 1
End If
ReDim arrValues(1 To 1, 1 To i)
For intCol = LBound(arrValues, 2) To UBound(arrValues, 2)
arrValues(1, intCol) = Range("Z2").Value
Next intCol
ActiveSheet.Cells(lngLastrow + 1, 1).Resize(UBound(arrValues, 2), 1).Value = Application.Transpose(arrValues)


If ActiveSheet.Range("A2") > "" Then
lngLastrow = ActiveSheet.Columns(1).Find(What:="*", After:=[A1], _
SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
Else
lngLastrow = 1
End If
ReDim arrValues(1 To 1, 1 To x)
For intCol = LBound(arrValues, 2) To UBound(arrValues, 2)
arrValues(1, intCol) = Range("Z3").Value
Next intCol
ActiveSheet.Cells(lngLastrow + 1, 1).Resize(UBound(arrValues, 2), 1).Value = Application.Transpose(arrValues)

End Sub
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
This For...Next loop would load you array with the values in Z2:Z9
Code:
For i = 1 To 8
ReDim Preserve arrValues(1 to i) 'Didn't understande the two dimension array
arrValues(i) = Range("Z" & i + 1).Value
Nest
Don't know if that is what you were trying to do.
 
Upvote 0

Forum statistics

Threads
1,214,990
Messages
6,122,625
Members
449,093
Latest member
catterz66

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top