For one cell, this is much simpler: use the Cells () function by default:
Cells(1,1) = "hello world"
or use the Sheet Cells () function:
Dim sht as Worksheet Set sht = Sheets("myworksheet") ' or: = Sheets(1) sht.Cells(1,1) = "hello world"
For the range, you will need to use two parameters, as described in the other answers given here. But the advantage is that you can set a number of fields to a value. And you can work on a sheet that is not βactive,β backstage. For instance:
Const colRand = 4 Const colDiff = 5 Dim sht as Worksheet, rngHi As Range, rngRand As Range, rngDiff As Range Set sht = Sheets("myworksheet") ' or: = Sheets(1) Set rngHi = sht.Range(sht.Cells(1,1), sht.Cells(3,3) rngHi = "hello world" Set rngRand = sht.Range(sht.Cells(1,colRand), sht.Cells(8,colRand) ' column 4, rows 1-8 rngRand = "=RAND()" Set rngDiff = sht.Range(sht.Cells(2,colDiff), sht.Cells(8,colDiff) ' column 5, rows 2-8 ' using FormulaR1C1 in case the sheet isn't set to use that type of formula Set rngDiff.FormulaR1C1="=RC[-1] - R[-1]C[-1]" ' on previous columnn, diff between this row and previous row
Explanation:
The Cells function gets either:
string parameter - in which you specify the range A1_And_Colon Style
or two cell parameters - the start cell of the range and the end cell.
So, to set the range using "cells", you need to give both cells divided by a comma:
Range(Cells(1,1), Cells(1,1)) = "hello world" Range(Cells(2,2), Cells(3,4)) = "you cannot square around, but you can round a square" Sheets(1).Cells(5,5) = "=Round(Sqrt(5))"