I have to copy paste some data from a sheet to another in excel using VBA.
I have been able to copy-paste the first set of data from sheet A to sheet B without issues.
I am now at the point where I need to copy the same cell value in sheet A into a range in sheet B
What I tried was to define the lastcell in sheet B and the first cell in sheet B in order to define the range where the value in sheet A should be copied.
This is the code for lastrow (which is working fine)
Last_Row2 = Sheets("Records").Range("a1").End(xlDown).Row
code for starting cell
legrng = Sheets("Records").Cells(Rows.count, 4).End(xlUp).Offset(1)
code for pasting the value into the range
Range("LegRng & Last_row2").Copy Destination = "cell value in sheet A"
I am receiving an error here:
Range("LegRng & Last_row2").Copy Destination = "cell value in sheet A"
the error is : Method 'Range of object_global' failed
Could you please tell me what I am doing wrong?
thank you for the help/explanation
You have a couple of errors
First, you try to refer to the range which has name LegRng & Last_row2. Such range doesn't exist in your worksheet, it can't exist because this name is illegal.
If you want to set reference to the range containing more than one cell you do it this way:
Sheets("Records").Range(startCell, endCell)
So first you need to set references to startCell and endCell (I assume you want last cell to be also in column 4):
Set startCell = Sheets("Records").Cells(Rows.count, 4).End(xlUp).Offset(1)
Set endCell = Sheets("Records").Cells(Last_Row2, 4)
You can fill range with value using its property Value
rng.Value = "value"
So, at the end your code should look like that:
With Sheets("Records")
Last_Row2 = .Range("a1").End(xlDown).Row
Set startCell = .Cells(Rows.count, 4).End(xlUp).Offset(1)
Set endCell = .Cells(Last_Row2, 4)
.Range(startCell, endCell).value = "cell value in sheet A"
End With
If not wrapped within an If Statement that tests if startCell.Row is less then Last_Row2, the accepted answer will cause issues if re-run, the code will place the value from sheet A in the next cell each time the macro is re-run.
You should always define and assign your variables.
Using Resize allows you to not need endCell.
Removing Offset from the startCell ensures the correct count for Resize
Sub FillInBlankCellsWithValue()
Dim Last_Row2 As Long, startCell As Range
With Sheets("Records")
Last_Row2 = .Range("a1").End(xlDown).Row
Set startCell = .Cells(Rows.Count, 4).End(xlUp)
'Use the If statement to test that the startCell.Row is less then the Last_Row2
If startCell.Row < Last_Row2 Then
'Offset to the first empty cell and resize by subtracting the startCell.Row
'from Last_Row2, to set the range for the value from SheetA.
startCell.Offset(1).Resize(Last_Row2 - startCell.Row).Value = Sheets("SheetA").Range("A1")
'Change the worksheet and paste value's cell range, as needed
End If
End With
End Sub
Related
I'm still a big noobie at VBA so any advice would be so appreciated...
I am trying to copy a range from multiple worksheets with a specific name to one worksheet in the same workbook. The range is dynamic in that I know what column the range starts and ends on, but i don't know what row it will start and end on. I have the paste destination figured out, but I need help finding the row the range should start on. The range I want to copy should start after the row in which the cell value = "Open Items:" (i.e. if the row where the cell = "Open Items:" is 3, then i want my range to start at C4)
I'm having an issue with my .Find formula where it's returning nothing. I am confident that the range that I'm searching for "Open Items:" does have a cell that is equal to that. Please see below for my code:
Dim ws As Worksheet
Dim targetws As Worksheet
Dim FindRow As Range
Dim openitemrow As Long
Set targetws = Sheets("targetwsname")
'loop through each ws
For Each ws In ActiveWorkbook.Worksheets
'sets ws name constraint and then tries to find the starting row in the range
If ws.Name Like "*" & "Open" & "*" Then
Set FindRow = ws.Range("C:C").Find("Open Items:", lookin:=xlValues, lookat:=xlWhole)
openitemrow = FindRow.Row
'there needs to be values in c4 for me to copy data over but the data i need to copy is in a different location
If Not IsEmpty(ws.Cells(4, "C")) Then
ws.Range("C:F" & openitemrow).End(xlUp).Offset(1).Copy Destination:=targetws.Range("D" & Rows.Count).End(xlUp).Offset(1)
End If
End If
next ws
If the entire cell value of the cell in question is "Open Items:" and the cell is always in the same column, then you can use match and this will already return it as a number
openitemrow = Application.Match("Open Items:", ws.Range("C:C"), 0)
I want to make variable C into the selection cell
like this
Dim c= selection area
I tried this code:
ActiveCell.Value
but doesnt' working..
Selcetion cell is always change.. so can't use range(null:null)
Sub noname1()
Dim wsCopy As Worksheet
Dim wsDest As Worksheet
Dim lCopyLastRow As Long
Dim lDestLastRow As Long
Dim B As String
Dim C
B = Cells(20, 87).Value
C = Selection
'Set variables for copy and destination sheets
Set wsCopy = Workbooks("source.xlsx").Worksheets("5.588")
Set wsDest = Workbooks("dest.xlsx").Worksheets(B)
lCopyLastRow = wsCopy.Cells(wsCopy.Rows.Count, "A").End(xlUp).Row
'Offset property moves down 1 row
lDestLastRow = wsDest.Cells(wsDest.Rows.Count, "A").End(xlUp).Offset(1).Row
wsCopy.Range(C).Copy _
wsDest.Range("A" & lDestLastRow)
'Optional - Select the destination sheet
wsDest.Activate
End Sub
A "Range" can be a single cell or any number of cells together. Since all cells are on one sheet or another every range is on a sheet as well. A range can't include cells from more than one sheet.
All sheets are in workbooks. No sheet can be in more than one workbook. Therefore no range can be spread over more than one workbook.
To specify a range you need to tell the workbook, the worksheet, its first cell and its last cell. If you don't specify the workbook the ActiveWorkbook will be presumed. If you don't specify the worksheet, the ActiveSheet will be presumed. Therefore
Range("B5:D15")
' is the same as
Activeworkbook.ActiveSheet.Range("B5:D15")
You don't need to select a range in order to copy, clear, delete or modify it. VBA's Selection object supports the user's selection on the screen but otherwise largely mirrors the Range object which disregards the screen. Therefore, if you select B5:D15 and enter ? Selection.Address(0,0) in the Immediate pane the answer will be "B5:D15". You will get the same reply if you enter ? Range("B5:D15").Address(0,0).
In my example, B5 and D15 are the first and last cells of the range. Excel creates a range name from these coordinates which is presented in inverted commas because it's a string. However, you can also specify the same cells as members of the Cells collection (all cells in a range - by default the entire sheet).
Debug.Print Range("B5:D15").Address
Debug.Print Range(Cells(5, 2), Cells(15, 4)).Address
The two lines of code print the same response but the second one is much easier to create using variables, such as changing row or column numbers.
So, now you can see what the code below would do.
Range(Cells(5, 2), Cells(15, 4)).Copy Destination:=Sheet2.Cells(1, 1)
Task: I need to copy a cell value from E12 to the range E23:E25. I achieved this easily using a simple copy and paste statement in the editor. But the range isn't always fixed. I have managed to capture the start point of this range as: Set rangeStart = Range("E12").End(xlDown).Offset(6, 0). I am unable to use this as a starting point for a range selection statement as follows:
Range("E23:E" & Range("A23").End(xlDown).Row).Select
That is how I'm selecting the range to be filled with data in the next step via a paste statement. How do I edit the first half of that range call to something more dynamic? Using rangeStart instead of E23:E.
I have just started working with Excel VBA, so this might be a very basic question to ask. Any help would be appreciated.
First I'd like to address the point that using Select is in most cases highly discouraged. Since you're only looking to paste a value into other cells, you can simply use a line like the following example:
Range("A1:A10").Value = Range("C2:C11").Value
or
Range("A1:A10").Value = 1
This would fill the A1:A10 range with either the values from C2:C11 or the integer 1, respectively. Note the two ranges in the first line are of the same size.
Now, a dynamic approach to your problem could be something along the following example
'Initialise two range variables
Dim SourceRange As Range, TargetRange As Range
'And integers for target rows
Dim fRow As Long, lRow As Long
With ThisWorkbook 'Assuming the code needs to be performed on the Workbook that hosts the macro
'Determine target rows
fRow = 23 'You have not specified how the first row should be determined
lRow = .Cells(fRow, "E").End(xlDown).Row
'Populate range vars
Set SourceRange = .Range("E12")
Set TargetRange = .Range(.Cells(fRow, "E"),.Cells(lRow, "E"))
'Paste values to target range
TargetRange.Value = SourceRange.Value
End With
Note that, since you haven't specified how this should be determined instead, the code above is hard-coded to have a target range in column E that starts at row 23.
If you have
Set rangeStart = Range("E12").End(xlDown).Offset(6, 0)
You can also have
Set rangeEnd = Range(rangeStart, Range("E" & Range("A23").End(xlDown).Row)
which will form a range from the first cell to the second cell.
But read the link posted by Vitaliy.
I have an excel file like
Original File
I want to transform all the cells that filled with information into a single column. Like
To transform This
How to i do this ?
I searched in internet about that i found just only transform cells in a single row to a single cell. But i couldn't find anything like this. Can you help me about that
This is a bit of code I keep around for this kind of job. It assumes that the values in each row are contiguous, that is there are no blank cells inside the data set. It also assumes that you're on the sheet that contains the data when you trigger it, and that you want the data to be placed on a new worksheet.
Option Explicit
Sub Columnise()
Dim shtSource As Worksheet
Dim shtTarget As Worksheet
Dim rngRow As Range, rngCol As Range
Dim lCount As Long
Set shtSource = ActiveSheet 'Or specify a sheet using Sheets(<name>)
Set rngCol = Range("A1", Range("A" & Rows.Count).End(xlUp))
Set shtTarget = Sheets.Add 'Or specify a sheet using Sheets(<name>)
'Define starting row for the data
lCount = 1
'Loop through each row
For Each rngRow In rngCol
'On each row, loop through all cells until a blank is encountered
Do While rngRow.Value <> ""
'Copy the value to the target
shtTarget.Range("A" & lCount).Value = rngRow.Value
'Move one space to the right
Set rngRow = rngRow.Offset(0, 1)
'Increment counter
lCount = lCount + 1
Loop
Next rngRow
End Sub
You should end up with all the data in a single column on a new worksheet.
EDITED TO ADD: Since you mentioned your data does contain blank cells, it gets more complicated. We'll want to add a way to continue to the actual end of the data, rather than just looping until we hit a blank cell. We'll modify the Do While... condition to this:
Do While rngCell.Column <= Cells(rngCell.Row, Columns.Count).End(xlToLeft).Column
This will loop until the end of the data in the row, then move on. Give it a shot and let us know.
In reference to: Copy a row in excel if it matches a specific criteria into a new worksheet
I attempted applying the above hyperlink code to the needs of my own workbook. The only notable differences are: Object names, My data begins in "A2" instead of "A1", and my data is being copied to "L" column in a new worksheet instead of "A" column
Also... you can assume I have generated tabs in excel that correspond with each SelectCell.Value.
Sub Consolidate_Sheets()
Dim MyCell As Range
Dim MyRange As Range
Dim ws As Worksheet
Set MyRange = Sheets("Install_Input").Range("A2")
Set MyRange = Range(MyRange, MyRange.End(xlDown))
Call superSizeMe(MyCell, MyRange)
Sub superSizeMe(SelectCell As Range, SelectRange As Range)
Dim InstallInput As Worksheet
Dim strPasteToSheet As String
'New worksheet to paste into
Dim DestinationSheet As Worksheet
Dim DestinationRow As Range
'Define worksheet with input data
Set InstallInput = ThisWorkbook.Worksheets("Install_Input")
For Each SelectCell In SelectRange.Cells
InstallInput.Select
If SelectCell.Value <> "" Then
SelectCell.EntrieRow.Select ''''LOCATION OF RUN-TIME ERROR 438''''
Selection.Copy
Set DestinationSheet = Worksheets(SelectCell.Value)
Set DestinationRow = DestinationSheet.Range("L1:L" & DestinationSheet.Cells(Rows.Count, "L").End(xlUp).Row)
Range("L" & DestinationRow.Rows.Count + 1).Select
ActiveSheet.Paste
End If
Next SelectCell
InstallInput.Select
InstallInput.Cells(1, 1).Select
If IsObject(InstallInput) Then Set InstallInput = Nothing
If IsObject(SelectRange) Then Set SelectRange = Nothing
If IsObject(SelectCell) Then Set SelectCell = Nothing
If IsObject(DestinationSheet) Then Set DestinationSheet = Nothing
If IsObject(DestinationRow) Then Set DestinationRow = Nothing
End Sub
I am getting a Run-time error'438'
"Object doesn't support this property or method" on "SelectCell.EntireRow.Select"
Well your code has a typo
SelectCell.EntrieRow.Select
should say entire not Entrie. Personally I would use this method anyway, It selects the entire row based on the number you put in. FYI there is also a corresponding Columns().select if you need it in the future
sel_cell_row = SelectCell.Row
Rows(sel_cell_row).select
edit addressed to comment
The reason you get the 1004 error is like it says, the copy and paste areas don't match. Think of copying 10 rows, and trying to paste it into 2 rows, simply wouldn'y work. I'm guessing the problem actually stems from your destinationrows code. I'm not entirely sure what its trying to do, but here are two generic fixes
1)keep the copy code as it is, and modify the paste. Instead of selecting a range of cells to paste into, select the first cell (if your range was a1:a10, selecting a1 is sufficient) excel will then paste all the data starting at that first cell. so in your code do this
'comment out all this destination row stuff
'Set DestinationRow = DestinationSheet.Range("L1:L" & DestinationSheet.Cells(Rows.Count, "L").End(xlUp).Row)
'Range("L" & DestinationRow.Rows.Count + 1).Select
Range("L1").select 'only referencing the first cell to paste into
ActiveSheet.Paste
2)rather than selecting an entire row, why not select only the populated values in that row something like
sel_cell_row = SelectCell.Row
lastColumn = ActiveSheet.Cells(2, Columns.Count).End(xlToLeft).Column
range(Cells(sel_cell_row ,1),Cells(sel_cell_row ,lastColumn )).select
then do your copy as usual. the 1 is for column 1, or A. I'm assuming the data you want is in one row starting at column A and going till lastColumn. Maybe now this will match your destinationrows code.
3)Com,bine options 1 and 2. so copy only the populated cells, and paste to the first cell in the range