How to set cell value in Excel?
I have the following code: Sub test(). Dim wb As Workbook, sh As Worksheet. Set wb = ThisWorkbook. Set sh = wb.Worksheets("Sheet1") Set sh = wb.Worksheets("Sheet1").ListObjects(1)
sh.ListColumns.Add ListColumns.Count, xlText
sh.ListRows.Add
sh.Cells(1, sh.Count).ListColumn = 2
With sh.Range("A1:A1") .Cells(Clear .Value = "This is a long row."
.NumberFormat = "General" .Font.Bold = True
End With. With sh.Range("B1:B1") .Cells(Value = "Short rows" .Bold = True wb.Save wb.Close End Sub. And I want to set the value of A1 and B1 equal to "This is a long row." and "Short rows" respectively. However, what I get from this is:
The expected result looks like this: I'm not sure what I'm missing. I have tried with .Formula and Range.ClearContents also with both those functions, but nothing seems to work. Can someone help me out with this? Thank you!
Use Range.Value = Value, eg: With sh.Range("A1") .
How to write a value in a cell using openpyxl?
I am using openpyxl to write in a new sheet.
I have to write one of the cell values and I am using this code workbook.cell(row = 1, column = 1).value ="some value"
But it is giving me an error: Traceback (most recent call last): File "C:UsersadminDesktopNew foldertest.py", line 26, in
File "C:UsersadminAppDataLocalProgramsPythonPython36-32libsite-packagesopenpyxlworkbookworkbook.py", line 534, in cell if hasattr(cell, field): File "C:UsersadminAppDataLocalProgramsPythonPython36-32libsite-packagesopenpyxlworksheetbase.py", line 856, in getattr raise AttributeError(attr). AttributeError: attribute 'value' of 'Workbook' object is not defined. This works for me: wb = openpyxl.loadworkbook("/Users/me/Desktop/newworkbook.xlsx")
Ws = wb.active row = ws.index(1) # row 0 is the default header row cell = ws.cell(row=row, column=1) cell.value = "value" wb.save("/Users/me/Desktop/newworkbook2.
Related Answers
What are the requirements for xlwt?
I've recently had to come across an old script that I hav...
How to read Excel in Python using openpyxl?
I am doing my first steps with Python and Python module...
How do I create an Excel spreadsheet in Python?
I have a lot of data in Pandas and it can get quite bi...