Trenera
Trenera

Reputation: 1505

Put sheet.cells(address/name as string) rather than column, row

I want to put the cell's address/name as string rather than column, row.

ws - a worksheet
ows - another worksheet
cell is a string = "G5"
i and c are respectivly row and columns    

cell = ws.Cells(i, 1)

ows.Cells(cell).Value = ws.Cells(i, c)

That code brings out an error.

Upvotes: 4

Views: 36767

Answers (1)

Paul Renton
Paul Renton

Reputation: 2670

Set ws = ThisWorkbook.Sheets("Sheet1")  'Replace with your sheet name
Set ows = ThisWorkbook.Sheets("Sheet2") 'Replace with your sheet name

cell = ws.Cells(i, 1)

ows.Range(cell).Value = ws.Cells(i, c)

Range expects a string, such as "A5" or "A5:B5", where as Cells expects two numbers separated by a comma

 'Examples
 Cells(1,2) 
 Range("A5")
 Range("A5:B5")
 Range(Cells(10,4), Cells(8,3)) 'Use Cells as args for a Range

One other difference is Cells will return either one cell or all cells of a worksheet.

Upvotes: 10

Related Questions