very new to VBA and not great at any type of coding. Syntax is definitely not my strong suit, and I think that is all this error is. I am writing a program to cycle through data imported from a CSV. Each column is a sensor readout, and each row is a single timestep. I am working with this data in some variable number of "steps" that represent discrete substages of the process the data was recording. This is handled by the FirstRow and LastRow variables, and the column of interest in this case is represented by ColOfInt. Essentially, I am trying to get the Mode of the values in column ColOfInt using the WorksheetFunction.Mode function. All of this is within a loop that advances steps, and I have to call between different worksheets to pull and write values. That is why I am using Range(Cells,Cells) -- I need to be able to loop through rows and columns, so "A1" reference doesn't really work well for this.
When I write the code like this, it does do what I want, but I don't like it having to actually change worksheets using the Sheets.Activate:
Sheets("CSV Import").Activate
myRange = Range(Cells(FirstRow, ColOfInt), Cells(LastRow, ColOfInt))
TargetDens = WorksheetFunction.Mode(myRange)
Sheets("Operations").Cells(StepNumber, 27).Value = TargetDens 'this line just writes the value to the output spreadsheet
However, when I write it like this I get Runtime error 1004, Application or Object Defined Error and the first row turns yellow.
myRange = Sheets("CSV Import").Range(Cells(FirstRow, ColOfInt), Cells(LastRow, ColOfInt))
TargetDens = WorksheetFunction.Mode(myRange)
Sheets("Operations").Cells(StepNumber, 27).Value = TargetDens
I really don't understand VBA's syntax, but what is wrong with Sheets(xyz).Range(ABC)? I have been using Sheets(xyz).Cells(abc) all over this program and it works fine and can pull and write across different sheets, but doesn't seem to work with Range, which I believe I have to use to use the Mode function. Sorry the question is so long, just trying to be clear. Thank you for any help.