Vba to list value and row range occupied down entire column

michaelsmith559

Well-known Member
Joined
Oct 6, 2013
Messages
881
Office Version
  1. 2013
  2. 2007
In the example, I am going down column B. Column F lists the values and Column G lists the row range for that value. I want to be able to do this for each column. The columns start in row 1 and go until row 1048576. Looking for a quick vba solution for the problem. Thanks for any help.

Book1
ABCDEFG
11015323744ValueRows
21015323745151
310153237468991
41015323747168992
5101532374833795
610153237491733796
7101532375057221
8101532375115437883
91015323752464117
101015323753
111015323754
121015323755
131015323756
141015323757
151015323758
Sheet1
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Hi Michael,

Not sure why there's such a jump in rows from 17 to 15 but assuming there's numbers in every Row of Col. B here's one possible solution:

VBA Code:
Option Explicit
Sub Macro1()

    Dim wsSrc As Worksheet
    Dim i As Long, j As Long, k As Long
    Dim dblValue As Double
    
    Application.ScreenUpdating = False
    
    Set wsSrc = ThisWorkbook.Sheets("Sheet4") '<-Change to the sheet name where the data resides
    
    With wsSrc
        j = .Range("A:E").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        For i = 1 To j
            If i = 1 Then
                k = .Range("F:G").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
                'Clear any existing entries
                If k >= 2 Then
                    .Range("F2:G" & k).ClearContents
                    k = .Range("F:G").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
                End If
                .Range("F" & k).Offset(1, 0).Value = .Range("B" & k).Value
                .Range("G" & k).Offset(1, 0).Value = i
                dblValue = .Range("B" & i).Value
            Else
                If .Range("B" & i).Value <> dblValue Then
                    k = .Range("F:G").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row + 1
                    .Range("G" & k).Value = i - 1
                    dblValue = .Range("B" & i).Value
                    .Range("F" & k).Offset(1, 0).Value = dblValue
                    .Range("G" & k).Offset(1, 0).Value = i
                End If
            End If
        Next i
    End With
    
    Application.ScreenUpdating = True

End Sub

Regards,

Robert
 
Upvote 0
Solution
Hi Michael,

Not sure why there's such a jump in rows from 17 to 15 but assuming there's numbers in every Row of Col. B here's one possible solution:

VBA Code:
Option Explicit
Sub Macro1()

    Dim wsSrc As Worksheet
    Dim i As Long, j As Long, k As Long
    Dim dblValue As Double
   
    Application.ScreenUpdating = False
   
    Set wsSrc = ThisWorkbook.Sheets("Sheet4") '<-Change to the sheet name where the data resides
   
    With wsSrc
        j = .Range("A:E").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        For i = 1 To j
            If i = 1 Then
                k = .Range("F:G").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
                'Clear any existing entries
                If k >= 2 Then
                    .Range("F2:G" & k).ClearContents
                    k = .Range("F:G").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
                End If
                .Range("F" & k).Offset(1, 0).Value = .Range("B" & k).Value
                .Range("G" & k).Offset(1, 0).Value = i
                dblValue = .Range("B" & i).Value
            Else
                If .Range("B" & i).Value <> dblValue Then
                    k = .Range("F:G").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row + 1
                    .Range("G" & k).Value = i - 1
                    dblValue = .Range("B" & i).Value
                    .Range("F" & k).Offset(1, 0).Value = dblValue
                    .Range("G" & k).Offset(1, 0).Value = i
                End If
            End If
        Next i
    End With
   
    Application.ScreenUpdating = True

End Sub

Regards,

Robert
Thanks for the help. Just tried it out. Works great. Appreciate it very much.
 
Upvote 0

Forum statistics

Threads
1,223,898
Messages
6,175,274
Members
452,628
Latest member
dd2

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