excel - filling values up to last row in a dynamic range
问题描述
sorry guys I am conscious this is really simple but its been very frustrating trying to figure this out last hour. I would like to have the value "427" (Column D) go all the way down to the end of the last line of range. (Row 32) As it is a dynamic range, the range will have new entries over time. I used the following code to do it
Sub EnterRemainingValues()
Dim Workbook As Workbook
Dim lastrow As Long
Set Workbook = Workbooks("Workbook 1") 'name of workbook
lastrow = Worksheets("Sheets1").Cells(Rows.Count, 1).End(xlUp).Row
Worksheets("Sheet1").Range("D14:D" & lastrow).Value = "427"
The issue I believe i'm having is because the range starts from A13 rather than A1, so the values 427 only fill up the first 3 rows which corresponds to the height of the worksheet title (A1:A3). The code thinks thats the lastrow. What I would like is to fill column D with "427" all the way down to the last row starting from row 14. Thank you
EDIT: I forgot to mention - I am controlling "workbook1" which contains the range pictured from a different workbook ("workbookfinal")
解决方案
Try this:
Option Explicit
Sub EnterRemainingValues()
' Define workbook.
Dim wb As Workbook ' You might need to use '.xlsm'
Set wb = Workbooks("Workbook 1") ' name of workbook
' Define worksheet.
Dim ws As Worksheet
Set ws = wb.Worksheets("Sheet1")
' Calculate last row.
Dim lastrow As Long
lastrow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
' Write values.
ws.Range("D14:D" & lastrow).Value = "427"
End Sub
推荐阅读
- python - 我错过了什么?API Python 问题
- firebase - Firebase messages.getToken() 失败并出现 MISSING_INSTANCEID_SERVICE 错误
- python - 如何使用python在已经找到的另一个单元格旁边的excel单元格中查找值?
- javascript - 检测数字输入的箭头已被点击 React
- c# - ASP.NET MVC 表单提交不起作用:没有错误
- r - R 中的 Web Scraping (rvest, RSelenium):如何在需要 SSL 证书的网站中进行身份验证
- node.js - 如何获取 npm 包所有者的电子邮件地址?
- javascript - 错误消息说 onclick javascript 函数不存在
- java - 了解开源项目的控制流程以及为什么有这么多类?
- laravel - 碳检查时间