Locked lesson.
About this lesson
There's more than one way to change data in a spreadsheet cell. This method is handy to use when looping using For Loops.
Exercise files
Download this lesson’s related exercise files.
15 Change Existing Cells - Method 2.docx57.1 KB 15 - Change Existing Cells - Method 2 SOLUTION.docx
55.6 KB
Quick reference
Change Existing Cells - Method 2
There's more than one way to change data in a spreadsheet cell. Here's another way...
When to use
Use this method when you don't know the specific name of the cell you want to change, but do know its number.
This method is handy to use when looping using For Loops and you now you've looped a specific number of times.
Instructions
# Change A1
ws.cell(row=1, column=1).value = "This is new!"
Hints & tips
- ws.cell(row=1, column=1).value = "This is new!"
- 00:04 Okay, in this video, I want to show you another way to manipulate or
- 00:07 change cells in your Spreadsheet individually.
- 00:09 So remember in the last video we set this WS variable equal to Johnny, and
- 00:14 we just named the cell that we wanted to change.
- 00:17 And if we head over to our Spreadsheet real quick.
- 00:20 We see that is A2 Johnny.
- 00:22 And that works great if you know exactly what cell you want to change, but
- 00:26 sometimes you don't know what so
- 00:28 you just want maybe the next available cell to be changed.
- 00:31 Or you may be looping through a range of things and
- 00:34 you want the next cell to automatically change to whatever you're trying to do.
- 00:39 So how do you do this if you don't know the specific name and number of the cell?
- 00:43 Well, there's another method we can do, and
- 00:45 that's what we're going to look at in this video.
- 00:47 So what we could do is we can call WS for our worksheet, and
- 00:51 then we can call the cell function.
- 00:53 And inside of the cell function,
- 00:55 we can give this parameters we can give it a row equals, and a column equals.
- 00:59 So if we know we want for instance, let's look at our Spreadsheet.
- 01:04 Let's say we want something in this column C and we want like this C5.
- 01:10 Well, if we know C5, we could just call C5, or
- 01:15 we could call column 3, because C is the third column over and
- 01:19 it's the 1, 2, 3, 4, 5, 6th row down.
- 01:23 So we can call it column 3, row 6, and that's just another way to do it.
- 01:29 So let's give this a try.
- 01:30 So let's go a row 6 and column 3.
- 01:33 And now what do we want to set?
- 01:35 Well, we want to set the dot value equal something, and let's just type,
- 01:40 This is new.
- 01:40 Okay, so we can run this.
- 01:43 And now let's go ahead and save this as pizza2.xlsx to see if that works.
- 01:48 So let's go ahead and save this file and we've got load.py still and
- 01:52 head over to our terminal.
- 01:54 And again, let's run python load.py, okay, that seems to have worked.
- 01:59 Now we can head over here and open up our pizza2 file.
- 02:03 And when we do boom in the third column over and the sixth row down so
- 02:08 1, 2, 3, 4, 5, 6, we see this is new.
- 02:11 So just sort of to keep this second method in mind a lot of times you're just going
- 02:15 to use this original method where you just name the cells specifically.
- 02:18 But like I mentioned, there are certain times when
- 02:21 you may not know the exact name but you do know the row or the column, so
- 02:24 this is a method you can use if that's the case.
- 02:26 In the next video, we're going to look at using formulas.
Lesson notes are only available for subscribers.