Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBA sheet.cells(string) - HOW?

Tags:

vba

cells

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.
Cheers, Vihar

like image 927
Trenera Avatar asked Jan 13 '23 20:01

Trenera


1 Answers

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.

like image 150
Paul Renton Avatar answered Feb 08 '23 15:02

Paul Renton