filling values up to last row in a dynamic range - excel

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

Related

Copy Data From Dynamic Column To Another Dynamic Column in the Same Workbook

I have a workbook with dynamic log sheet that performs calculations on data entered by the user. I would like specific dynamic columns copied from this log sheet to another sheet in the workbook for graphing purposes. This copy would only be for values and mainly is done to make it easier to run a final macro for producing a XY scatter plot. However, I am getting an object error and am not sure why this is happening. Thank you in advance for any and all help. Could you please help me figure out the best way to accomplish this task? Here is my current VBA:
Sub UpdateCharts()
Dim sourceSheet As Worksheet
Dim targetSheet As Worksheet
Dim sourcelRow As Long
Dim targetlRow As Long
Set sourceSheet = ThisWorkbook.Worksheets("Inventory Log")
Set targetSheet = ThisWorkbook.Worksheets("Tables")
sourcelRow = sourceSheet.Cells(Rows.Count, 1).End(xlUp).Offset(-1, 0).Row
targetlRow = targetSheet.Cells(Row.Count, 6).End(xlDown).Offset(1, 0).Row
sourceSheet.Cells(sourcelRow, 1).Copy
targetSheet.Cells(targetSheet, 6).PasteSpecial xlPasteValues
End Sub
There are several errors/typos in your code:
you should always use explicit referencing
Row and Rows are two different commands
to retrieve the last row you should always use the same function
you don't need to copy/paste values - you can write them directly to
You could e.g. use this function to retrieve the last row of a sheet and columnIndex:
Public Function getLastRow(ws As Worksheet, columnIndex As Long) As Long
With ws
getLastRow = .Cells(.Rows.Count, columnIndex).End(xlUp).Row
End With
End Function
Then your code would look like this
Sub UpdateCharts()
Dim sourceSheet As Worksheet
Dim targetSheet As Worksheet
Dim sourcelRow As Long
Dim targetlRow As Long
Set sourceSheet = ThisWorkbook.Worksheets("Inventory Log")
Set targetSheet = ThisWorkbook.Worksheets("Tables")
sourcelRow = getLastRow(sourceSheet, 1)
targetlRow = getLastRow(targetSheet, 6) + 1 'adding 1 row to have the next empty row
targetSheet.Cells(targetlRow, 6).Value = sourceSheet.Cells(sourcelRow, 1).Value
End Sub

VBA Fill Variable Cell Range with Text Value

I'm looking to do the following (in title) using VBA in an excel macro.
I have found solutions on how to fill a set range with a value, however I cannot find anything relating to if the cell range to fill is a variable.
I wish to fill a column with text, but only until the last filled row (as the number of rows varies each day).
Any ideas on how to go about this?
Try this, see comments in code for more details.
Sub fillRange()
Dim ws As Worksheet: Set ws = ActiveWorkbook.Sheets("Sheet Name") '<-- set your sheet name
Dim lRow As Long: lRow = ws.Cells(Rows.Count, 1).End(xlUp).Row 'get last row
Dim rngToFill As Range 'declare the range variable
Set rngToFill = ws.Range("A1:A" & lRow) 'set the range variable
rngToFill.Value = "new value test" 'fill the range
End Sub

VBA Copy data list with variable length

So what i am trying to do is copy a data list to another location (eventually to another tab but i can figure that out myself). The data list is variable in length but it is filled into another cell manually.
List that needs to be copied is 2 rows wide (H & I) and x columns long (from row 2 to "end")
The value in cell N6 is the amount of rows
So the range that needs to be copied is H2 to I(amount of rows +1)
Here is my code
Sub Copycells()
Dim wb As Workbook
Dim wb1 As Worksheet
Dim LastRow As Long
Set wb = ThisWorkbook
Set wb1 = wb.Sheets("Sheet1")
'check data for list length
LastRow = Worksheets("Sheet1").Cells(6, "N").Value + 1
'copy data
Range("V2:W?").Copy Range("H2:I?")
End Sub
So where i'm struggling is how to fill in the range for the copy, in a sense combining the row letter with the variable number.
I think you might have mistaken columns for rows and rows for columns in the explanation, that you give in your question.
However, I'm fairly sure i understand what you want the code to do.
Try this:
Sub Copycells()
Dim wb As Workbook
Dim wb1 As Worksheet
Dim LastRow As Long
Set wb = ThisWorkbook
Set wb1 = wb.Sheets("Sheet1")
'check data for list length
LastRow = Range("N6").Value + 1
'copy data
Range("H2:I" & LastRow).Copy
End Sub
You need to specify where the copied list needs to be pasted, but I'll let you have a try on that on your own.

Copying A Row from 1 excel sheet to another [duplicate]

This question already has answers here:
Finding first blank row, then writing to it
(8 answers)
Closed 6 years ago.
I would like to copy selected row from Sheet1 to Sheet2. Below code is overriding existing value in Sheet2 file. Please assist to stop override my existing values.
Sub CopySelection()
Dim xlSel As Excel.Range
Set xlSel = Excel.Application.Selection
xlSel.Copy Excel.Application.Sheets("All_Data").Range("A1")
End Sub
Any help will be appreciated.
The code below will copy the range selected in "Sheet1" (modify it to fit your sheet's name that holds the data to be copied), and then will paste it in Column A in the first available row in Sheet "All_Data".
Let me know if the code below (tested) works :
Sub CopySelection()
Dim Sht1 As Worksheet
Dim Sht2 As Worksheet
Dim xlSel As Range
Dim LastRow As Long
' modify "Sheet1" to your sheet source (where you make your Selection to copy)
Set Sht1 = ThisWorkbook.Sheets("Sheet1")
' sheet "All_Data" is your target sheet (Where you paste the selection)
Set Sht2 = ThisWorkbook.Sheets("All_Data")
Set xlSel = Selection
'option 1: find last row with data in Sheet "All_Data" at Column A
LastRow = Sht2.Cells(Sht2.Rows.Count, "A").End(xlUp).Row
'option 2: (less reliable) find last row with data in Sheet "All_Data" using UsedRange
'LastRow = Sht2.UsedRange.Rows(Sht2.UsedRange.Rows.Count).Row
' paste the copied range in Column A, the first row after row with data
xlSel.Copy Sht2.Range("A" & LastRow + 1)
End Sub

Copy range to the bottom of another sheet

I am writing a macro that loops through a "source" sheet and for each value in column A, copy a range from template sheet to a destination sheet. After the template range is copied, I need to change a few values in destination sheet based on the source sheet value. Right now I am trying to get the copy working. The copy is failing with error 1004 'The information cannot be pasted because the Copy area and the paste area are not the same size.'
Sub CopyRangeFromOneSheetToAnother()
Dim iLastRow As Long
Dim wb As Workbook
Dim shtSource As Worksheet
Dim shtTemplate As Worksheet
Dim shtDest As Worksheet
Dim sResourceName
Dim rngCalcTemplate As Range
Set wb = ThisWorkbook
Set shtSource = wb.Sheets(1)
Set shtTemplate = wb.Sheets("res_tpl")
Set shtDest = wb.Sheets.Add
'--set range for copying. Hard-coded for now would be nice if it would auto shrink/expand
Set rngCalcTemplate = shtTemplate.Range("A2:M7")
'Find the last row (in column A) with data.
iLastRow = shtSource.Range("A:A").Find("*", searchdirection:=xlPrevious).Row
'--loop through source sheet and copy template range to dest for each
For iSourceSheetRow = 2 To iLastRow
sResourceName = shtSource.Cells(iSourceSheetRow, 1)
rngCalcTemplate.Copy shtDest.Range("A" & Rows.Count).End(xlDown)
Next
End Sub
The problem is with the following line of your code:
rngCalcTemplate.Copy shtDest.Range("A" & Rows.Count).End(xlDown)
If you place your cursor at the very last cell in column A (i.e. at "A" & Rows.Count, possibly A1048576) and then press Ctrl-Down, you are still at the very last cell in column A.
If you then try to paste 6 rows of information starting at that cell, there won't be room to do so - there is only one row of "pastable" area to use.
You are probably wanting to find the row following the last used cell in that column, so your code should be:
rngCalcTemplate.Copy shtDest.Range("A" & shtDest.Rows.Count).End(xlUp).Offset(1, 0)

Resources