0

I am running a loop that calculates the occupied cells per row.

this is the code

Option Explicit
Option Base 1

Sub test()
    Dim i As Integer, shtOne As Object, shtTwo As Object
    Dim rowColumnCount1 As Variant, nr1 As Integer, nc1 As Integer
    Set shtOne = Workbooks("test.xlsm").Worksheets("Sheet1")
    Set shtTwo = Workbooks("test.xlsm").Worksheets("Sheet2")

    nr1 = shtOne.UsedRange.Rows.Count
    nc1 = shtOne.UsedRange.Columns.Count

    ReDim rowColumnCount1(nr1)
    For i = 1 To nr1
        rowColumnCount1(i) = WorksheetFunction.CountA(shtOne.Range(Cells(i, 1), Cells(i, nc1)))
    Next i

    Dim j As Integer
    Dim rowColumnCount2 As Variant, nr2 As Integer, nc2 As Integer
    nr2 = shtTwo.UsedRange.Rows.Count
    nc2 = shtTwo.UsedRange.Columns.Count

    ReDim rowColumnCount2(nr2)
    For i = 1 To nr2
        rowColumnCount2(i) = WorksheetFunction.CountA(shtTwo.Range(Cells(i, 1), Cells(i, nc2)))
    Next i
End Sub

the data in sheet1 is

enter image description here

the data in sheet2 is

enter image description here

in the sheet I when using the function across the row I get the right numbers but when running the code I am getting error 1004. I tried separating the sub to two separate subs still got the same error. I am using 365 and Win10

Thanks for the help Boaz

PeterT
  • 8,232
  • 1
  • 17
  • 38

0 Answers0