VBA return column letters to select column range

ez08mba

Board Regular
Joined
Nov 1, 2011
Messages
224
Office Version
  1. 2016
Platform
  1. Windows
I want to select a range of columns from a workbook I have no formatting contorl over and copy them to another workbook sheet for analysis.

The code below is a test code and so far it selects the highest and lowest numbers representing a date of a month (i.e. 1, 2, 3, ... 30) depending how many days there are each month. Then it correlates the cell addresses and column numbers based upon those minimum and maximum values so I caputure the whole month. Now I want to correlate column numbers, then select those columns and the range of columns in between to copy the data.

I was able to return the column numbers for the min and max values and thought I could do

Columns(scol:lcol).copy

and it would work, but it doesn't. I've tried a number of tactics and help menu suggestions and no success. Maybe I have to return column letters, but I haven't found logical code for that.

This is my test code. It

Code:
Sub FindSmLgnmAdr()
Dim lgnm As Integer
Dim lrng As Range
Dim ladr As String
Dim lcol As String
Dim smnm As Integer
Dim srng As Range
Dim sadr As String
Dim scol As Integer
Dim colnm As Integer
'Unmerge cells for copying
Range("E1:G2").Select
With Selection
    .MergeCells = False
End With
'Finds number of days in month and copies data range
lgnm = WorksheetFunction.Max(Range("3:3").Value)
smnm = WorksheetFunction.Min(Range("3:3").Value)
Set lrng = Range("3:3").Find(lgnm)
Set srng = Range("3:3").Find(smnm)
sadr = srng.Address
scol = Range(srng.Address).Column
ladr = lrng.Address
lcol = Range(lrng.Address).Column
 
Range("E1:G2").Select
With Selection
    .MergeCells = True
End With
MsgBox smnm & " = " & sadr & " = " & scol & vbNewLine & _
    lgnm & " = " & ladr & " = " & lcol
 
End Sub
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
I figured out my own problem. I had to format it this way:

Range(Columns(scol), Columns(lcol)).Select

Sorry if I waisted anyone's time. Thanks!

So here is the corrected code:
Code:
Sub FindSmLgnmAdr()
Dim lgnm As Integer
Dim lrng As Range
Dim ladr As String
Dim lcol As Integer
Dim smnm As Integer
Dim srng As Range
Dim sadr As String
Dim scol As Integer
Dim colnm As Integer
'Unmerge cells for copying
Range("E1:G2").Select
With Selection
    .MergeCells = False
End With
'Finds number of days in month and copies data range
lgnm = WorksheetFunction.Max(Range("3:3").Value)
smnm = WorksheetFunction.Min(Range("3:3").Value)
Set lrng = Range("3:3").Find(lgnm)
Set srng = Range("3:3").Find(smnm)
sadr = srng.Address
scol = Range(srng.Address).Column
ladr = lrng.Address
lcol = Range(lrng.Address).Column
[COLOR=red][B]Range(Columns(scol), Columns(lcol)).Select[/B][/COLOR]
Range("E1:G2").Select
With Selection
    .MergeCells = True
End With
MsgBox smnm & " = " & sadr & " = " & scol & vbNewLine & _
    lgnm & " = " & ladr & " = " & lcol
    
End Sub
 
Upvote 0
Why are you selecting a range, then selecting a different range right after it?

BTW, It's very rare that you actually need to Select anything in VBA. You can work with ranges directly.

Intead of this, for example:
Code:
Range("E1:G2").Select
With Selection
    .MergeCells = True
End With

This will accomplish the same thing:

Code:
Range("E1:G2").MergeCells=True
 
Upvote 0

Forum statistics

Threads
1,214,411
Messages
6,119,346
Members
448,888
Latest member
Arle8907

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