merging/concatenating column letter and row number

drangel

New Member
Joined
Aug 29, 2014
Messages
18
Hi all-

I am attempting to combine column letter with row number to create a range. Basically, I am trying to find last row and last column and assign that as my "DataRange" at the beginning of this Sub so that I can sort/subtotal based on that range. Below is the beginning of my code:


Sub BillingSutotals()
'
' BillingSutotals Macro
' The macro sorts and subtotals by the Number, Memo, and Sales Price
'


Sub Subtotal()


Dim EndRow As Integer, EndCol As String
Dim DataRange As String



Range("A1").Select
Selection.End(xlDown).Select
EndRow = ActiveCell.Row
Range("A1").Select
Selection.End(xlToRight).Select
EndCol = Split(ActiveCell(1).Address(1, 0), "$")(0)


DataRange = "A1:" & EndCol & EndRow

After reading a few articles, I tried the code below but had no success:

DataRange = "A1" & ":" & EndCol & CStr(EndRow)
DataRange = "A1" & ":" & LTrim(EndCol & Str(EndRow))


Any ideas as to how to set my DataRange with the information I currently have?
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Hi all-

I am attempting to combine column letter with row number to create a range. Basically, I am trying to find last row and last column and assign that as my "DataRange" at the beginning of this Sub so that I can sort/subtotal based on that range. Below is the beginning of my code:


Sub BillingSutotals()
'
' BillingSutotals Macro
' The macro sorts and subtotals by the Number, Memo, and Sales Price
'


Sub Subtotal()


Dim EndRow As Integer, EndCol As String
Dim DataRange As String



Range("A1").Select
Selection.End(xlDown).Select
EndRow = ActiveCell.Row
Range("A1").Select
Selection.End(xlToRight).Select
EndCol = Split(ActiveCell(1).Address(1, 0), "$")(0)


DataRange = "A1:" & EndCol & EndRow

After reading a few articles, I tried the code below but had no success:

DataRange = "A1" & ":" & EndCol & CStr(EndRow)
DataRange = "A1" & ":" & LTrim(EndCol & Str(EndRow))


Any ideas as to how to set my DataRange with the information I currently have?
Hi drangel,

Try this instead:

Rich (BB code):
Sub TEST()
' Defines DataRange as a range, not a string
Dim DataRange As Range
' Defines LastRow as last row of column A containing data
Lastrow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row
' Defines LastCol as last column of row 1 containing data
LastCol = ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column
' Sets DataRange as from A1: LastRow & LastCol address
Set DataRange = Range("A1", Cells(Lastrow, LastCol))
' Selects the DataRange just as proof that it works
DataRange.Select
End Sub
 
Upvote 0
Code:
Sub myMacro()
     headerRow = 1
     lastColumn = Cells(headerRow, Columns.Count).End(xlToLeft).Column
     lastRow = 0
     c = 1
     Do Until c > lastColumn

          lastRowTemp = Cells(Rows.Count, c).End(xlUp).Row 'If this line of code does not work, delete this line of code and uncomment out the code below.

          'cL = CLng(c)
          'cL = ColumnLetter(cL)
          'lastRowTemp = Range(cL & Rows.Count).End(xlUp).Row

          If lastRowTemp > lastRow Then
               lastRow = lastRowTemp
          End If
          c = c + 1
     Loop
     
     lastColumn = CLng(lastColumn)
     lastColumnLetter = ColumnNumber(lastColumn)

     'Your result is in the variable named dataRange.
     dataRange = "A" & headerRow & ":" & lastColumnLetter & lastRow

End Sub

Function ColumnLetter(ColumnNumber As Long) As String
    Dim n As Long
    Dim c As Byte
    Dim s As String

    n = ColumnNumber
    Do
        c = ((n - 1) Mod 26)
        s = Chr(c + 65) & s
        n = (n - c) \ 26
    Loop While n > 0
    ColumnLetter = s
End Function
 
Upvote 0
Thank you Fishboy and WarPigl3t a ton! I appreciate both responses and code.

Follow-up question. One of the reason why I initially started with letter route rather than value/# on the column is because when I sort later on in the code (see below), I was going to concatenate the letters (EndCol/LastColumn) with EndRow/LastRow to sort. If I go the column number route, how/what would you suggest putting in the Range" "_ below?

ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("B2:B3253" _
), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortTextAsNumbers
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("D2:D3253" _
), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("F2:F3253" _
), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet1").Sort
.SetRange Range(DataRange)
.HEADER = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply



Also, I will be doing multiple subtotals layers (as seen below) after sorting. After I sort, I can go right into subtotaling since its selected and subtotal (2 or 3 times after) without missing any of the values, right (Sort, subtotal, subtotal,...,)? I will not be missing any values in the DataRange (orginal values + additional rows after subtotaling)?

Selection.Subtotal GroupBy:=NumColno, Function:=xlSum, TotalList:=Array(CrColno), _
Replace:=True, PageBreaks:=False, SummaryBelowData:=True
Selection.Subtotal GroupBy:=MemoColno, Function:=xlSum, TotalList:=Array(QtyColno, CrColno), _
Replace:=False, PageBreaks:=False, SummaryBelowData:=True
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,917
Members
452,366
Latest member
TePunaBloke

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