How do I set a range using integer variables?

JoeBobJenkins

New Member
Joined
Feb 15, 2019
Messages
4
I've tried to do this about a dozen different ways but I can't seem to get the range to set to the values I need. Any thoughts why?

Code:
Public Sub Add2Excel(varDataArray() As Variant)

Dim xlappApp As Excel.Application
'Set xlappApp = GetObject("Excel.Application")
Set xlappApp = CreateObject("Excel.Application")
Dim wkbkWkbk As Excel.Workbook
Set wkbkWkbk = xlappApp.Workbooks.Add

Dim wkshtData As Excel.WorkSheet
Set wkshtData = wkbkWkbk.ActiveSheet
wkshtData.Name = "All Data"
Dim intRowCount, intColCount As Integer
For intColCount = LBound(varDataArray) To UBound(varDataArray)
    For intRowCount = LBound(varDataArray, 2) To UBound(varDataArray, 2)
        wkshtData.Cells(intRowCount + 1, intColCount + 1).Value = varDataArray(intColCount, intRowCount)
    Next intRowCount
Next intColCount
'Deskttop Path
Dim DesktopPath As String
DesktopPath = Environ("USERPROFILE") & "\Desktop"
'Write to Text file for records
Dim FilePath As String
FilePath = DesktopPath & "\OccTestRuns.xlsx"
wkshtData.Columns("A:AZ").EntireColumn.AutoFit
Dim rng2Sort As Range
    Set rng2Sort = wkshtData.Range(Cells(1, 1), Cells(intRowCount, intColCount)).Select
    ActiveWorkbook.Worksheets("All Data").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("All Data").Sort.SortFields.Add Key:=Range( _
        "B2:B691"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    With ActiveWorkbook.Worksheets("All Data").Sort
        .SetRange Range("A1:I691")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    
wkbkWkbk.SaveAs (FilePath)
End Sub
 
Last edited by a moderator:

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Welcome to the forum.

You need to specify the sheet for the Cells properties as well as the Range one:

Rich (BB code):
Set rng2Sort = wkshtData.Range(wkshtData.Cells(1, 1), wkshtData.Cells(intRowCount, intColCount)).Select

A couple of other points:
1. Use Long not Integer for row variables.
2. This:
Rich (BB code):
Dim intRowCount, intColCount As Integer
only declares intColCount as an Integer. intRowCount is actually a Variant. You have to specify the type for each variable like this:

Rich (BB code):
Dim intRowCount As Integer, intColCount As Integer

But, again, use Long, not Integer. ;)
 
Last edited:
Upvote 0
Welcome to the forum.

You need to specify the sheet for the Cells properties as well as the Range one:

Rich (BB code):
Set rng2Sort = wkshtData.Range(wkshtData.Cells(1, 1), wkshtData.Cells(intRowCount, intColCount)).Select

A couple of other points:
1. Use Long not Integer for row variables.
2. This:
Rich (BB code):
Dim intRowCount, intColCount As Integer
only declares intColCount as an Integer. intRowCount is actually a Variant. You have to specify the type for each variable like this:

Rich (BB code):
Dim intRowCount As Integer, intColCount As Integer

But, again, use Long, not Integer. ;)

Thanks!, and didn't know that about declaring variables being variants. Need to do a lot of redeclaring elsewhere.
 
Upvote 0

Forum statistics

Threads
1,215,601
Messages
6,125,758
Members
449,259
Latest member
rehanahmadawan

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