theunsigned
New Member
- Joined
- Jul 23, 2021
- Messages
- 6
- Office Version
- 365
- Platform
- Windows
Hi, I'm brand new to VBA but I have some C# experience; I have done quite a bit of research but I'm curious how I might accomplish the following:
I have a large excel file with the names of counties in column A and I want to create a list of ranges so that I can sort the data in related columns and display it in a different format. I have been able to loop through column A and pick out the cells that contain the variable county name but am struggling with the next part, which is setting the range between that starting cell and the next cell in the column that is not empty. The counties and related data are all mixed up and I want to sort it by county in another sheet.
So, in that example, if I want a list of ranges for "Adam," then I'd be looking for (A1:A2, A7:A8), right? I've looked into quite a lot but am not sure on how to loop properly or use the find methods to achieve my goal.
So far, my working code is the following (please be kind, I know I need to shape it up)
Any assistance would be greatly appreciated. Thank you! Please let me know how I can clarify more
I have a large excel file with the names of counties in column A and I want to create a list of ranges so that I can sort the data in related columns and display it in a different format. I have been able to loop through column A and pick out the cells that contain the variable county name but am struggling with the next part, which is setting the range between that starting cell and the next cell in the column that is not empty. The counties and related data are all mixed up and I want to sort it by county in another sheet.
A | |
1 | Adam |
2 | |
3 | Duke |
4 | |
5 | Duke |
6 | |
7 | Adam |
8 | |
9 | Duke |
So, in that example, if I want a list of ranges for "Adam," then I'd be looking for (A1:A2, A7:A8), right? I've looked into quite a lot but am not sure on how to loop properly or use the find methods to achieve my goal.
So far, my working code is the following (please be kind, I know I need to shape it up)
VBA Code:
Dim County As String
Dim refSheetname As String
Dim refSheet As Worksheet
Dim CoStartList As Object
Dim CoRangesList As Object
Dim nextfilled As Range
Dim chunk As Range
Sub CallSubs()
Call getInfo
End Sub
Sub getInfo()
''GET COUNTY NAME FROM SHEET NAME
County = ActiveSheet.Name
''GET NAME OF REFERENCE SHEET - need to make dynamic
refSheetname = Sheets("a" & 6).Name
''SET REFERENCE SHEET
Set refSheet = Worksheets(refSheetname)
''GET CELLS in refsheet range A:A that match County
Set CoStartList = CreateObject("System.Collections.ArrayList")
For Each cell In refSheet.Range("A:A")
If Not IsEmpty(cell) And cell.Value = County Then
CoStartList.Add cell
End If
Next
Set CoRangesList = CreateObject("System.Collections.ArrayList")
End Sub
Any assistance would be greatly appreciated. Thank you! Please let me know how I can clarify more