Copy range and store them on a different sheet - excel

I think this is not a too difficult task to do, but the problem is that I actually don't know anything about programming and I need to do this on my current job. This is my issue:
The problem is that I have to develop a Macro, and assign it to a button, that copies range E3:K14 from Page 1(sheet1) and paste it on A1 on Page 2(sheet2). This first task is rather easy, but the problem comes when if I hit again the button's assigned Macro it has to copy the same range from Page 1 and paste it on Page 2 but if the first has to check if there already something pasted on A1, if there is, then it must copy it on cell I1 and if I click the button again to Q1 and so on.
When the range is pasted it must be pasted with "Paste Vales" option.
If someone could just put me the exact code (with some comments if possible) for me just to paste it would be really helpful.
Any help would be really appreciated!

Try below code :
Sub sample()
With Sheets("Sheet2")
Sheets("sheet1").Range("E3:K14").Copy .Cells(.Range("A" & .Rows.End(xlUp).Row) + 1, 1)
End With
End Sub
Explanation :
Have used the Range Copy Method and provided a destination where to
paste.
.Range("A" & .Rows.End(xlUp).Row) + 1 checks for last used cell in sheet2 column A and adds 1 so that data is pasted on last used row.(Assuming the column E does not have blank cells)

Dude you can use this code to get the last column with data, this is the key for solve your problem.
Sub SelectLast()
Dim LastColumn As Long
With ActiveSheet
LastColumn = .Range("A1").SpecialCells(xlCellTypeLastCell).Column
End With
MsgBox LastColumn
Cells(18, LastColumn + 2).Select
End Sub

Related

Copy Data from Active cells to Another Workbook

I'm a total beginner here. I saw this example online to copy data from a fixed range of a workbook to another workbook.
Could someone please guide me on how I can copy from A2 cell onward to the last active cell with data?
.
In this given example, I wanted to copy those cells highlighted in blue to another workbook. The data varies every time, hence a fixed range doesn't work for me. I try using ".UsedRange" but it copies the footer as well, which is something I don't need.
Sub Copy_Method()
Workbooks("New-Data.xlsx").Worksheets("Export").Range("A2:D9").Copy _
Workbooks("Reports.xlsm").Worksheets("Data").Range("A2")
End Sub
Ref: https://www.excelcampus.com/vba/copy-paste-another-workbook/
as per the posted screenshot:
the "footer" appears to be placed in column A
data fill all columns from A to E
hence you can use column B to get the last used cell row from:
Sub Copy_Method()
With Workbooks("New-Data.xlsx").Worksheets("Export")
.Range("A2:E" & .Cells(.Rows.Count, 2).End(xlUp).Row).Copy _
Workbooks("Reports.xlsm").Worksheets("Data").Range("A2")
End With
End Sub
Given your particular set up, you can use the CurrentRegion property.
Sub Copy_Method()
Dim r As Range
Set r = Workbooks("New-Data.xlsx").Worksheets("Export").Range("A1").CurrentRegion
Set r = r.Offset(1).Resize(r.Rows.Count - 1) 'exclude the header
r.Copy Workbooks("Reports.xlsm").Worksheets("Data").Range("A2")
End Sub

Excel Macro copy paste in right till data in left cell

I have two excel columns in a worksheet, consider as A(left) and B(right) and I have recorded a macro where it will calculate a formula and copy/paste it to all the right side columns till where the left side column has data. but when next time some extra data is added to the left column and when I run a macro to copy/paste then it is only considering the previous range but not extending to the newly added cells.
example : A1:A5 is left side and B1:B5 is the right side and my formula in B range which is right range calculate based on A1:A5 and my macro works fine and restricted only to B1:B5 even when I added new data like A1:A10 only copying B1:B5. what is the method I can use my macro automatically till the data range of A side column?
Better next time, you provide a screenshot of your data & also VBA code in question.
You are using a static range, while you require is dynamic range.
try this
Sub test()
Dim i As Integer
i = WorksheetFunction.CountA(Range("A:A"))
Range("B1:b" & i).Select 'instead of selecting you can provide your formula to whole range.
End Sub
For any other issue, feel free to comment
As per your description just try below.
Sub FillFormula()
Dim i As Long
i = Range("A1").End(xlDown).Row
Range("B1").FormulaR1C1 = "=RC[-1]+5"
Range("B1").AutoFill Destination:=Range("B1:B" & i)
End Sub

Excel VBA Button - Fast way to change reference cell

Uh hello stackoverflow, I just started learning excel VBA for works and I find a problem that is I have to manually change the reference cell of a button manually for 500 buttons I wanted to make. For simplification sake:
Say in Column A I have several information normally hidden.
I put a button in each cell of Column B which will display the information in Column Ax (x being the cell number) to Cell Cx. Say:
Range("C1").Value = Range("A1").Value
The button I put in B1 would do the above, that is showing the value of C1 to be what is put in A1.
Now, I have 500 rows of data like this. Is it possible to make a dynamic buttons that knows the cell it is positioned in, and when moved/copy pasted to another cell, refer to its new location to dynamically change the reference cell in its code (Ax and Cx in that example)? So if I put the button in cel B67, the reference cell in the button code would change to C67 and A67 respectively.
[Before answering, here's my take on the things:]
I think the question you're trying to ask here is "how to find the last non-empty row".
The reason why I'm not flagging it as one is because, you may not be aware that you actually should be asking that question.
In general (and without meaning to sound condescending) you probably should study up on the programming basics first before asking questions like this here. Take some basic course somewhere (there are many) and only then begin experiment with some coding, because if you're missing coding fundamentals, then you'll never understand the bigger pictue behind it
What you're trying to describe here is a basic loop in programming language.
How does the loop work?
Let's pretend that we're a computer and we could also speak (in human language, not binary 1s and 0es).
I want to go through all the cells in a dynamic range
I need to know where does the dynamic range start and end (fetch me ending point)
I'll begin at starting point and end at ending point (duhh, silly humans should know that...)
For each and every cell in the range I'll do a certain action.
And that's it, fairly easy. Was a matter of seconds.
If we were trying to exactly represent what I described here:
Option Explicit ' < prevents you from typos, undeclared variables and so on...
Private Sub eachActiveCell()
Dim actionrange as Range
Dim lastrow as Long
lastrow = Sheets("Your Sheet Name").Cells(Rows.Count, "A").End(xlUp).Row
'finds last (active) row ^
Set actionrange = Sheets("Your Sheet Name").Range(Cells(1, "A"), Cells(lastrow, "A"))
' this sets our range to (A1:A <lastrow>) ^
Dim cell as Range
For each cell in actionrange
cell.Offset(0, 2) = cell.Value2 ' utilizes offset, A1 offset by 2 columns = C1
Next cell
End Sub
Other alternative, as it may be easier to comprehend with rows, rather than Ranges.
Option Explicit
Private Sub foriinA()
Dim ws as WorkSheet: Set ws = Sheets("Your Sheet Name")
Dim lastrow As Long
lastrow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
Dim i As Long
For i = 1 To lastrow
ws.Cells(i, "C") = ws.Cells(i, "A")
Next i
End Sub

Selecting dynamic, filtered range in VBA

I am attempting to select a dynamic range of filtered data that spans from col. A: col. J without selecting the header (in row 1). From there I need to copy and paste it into a new sheet where I will manipulate it further, but I cannot come up with an efficient or functional way to do this. Based on some code I found on another forum I was able to select all of the "visable cells" in a single column, but I am running into issues trying to select the whole range. I am still very new to vba so forgive my syntax, but my code posted below was an attempt to itterate through Rows.Count and i which was an integer 1-10. If you have any advice on how to do this better and more efficiently I would really appreciate it.
Sub SelectVisibleInColD()
Dim lRow As Long, i As Integer
Set i = 1
Do While i <= 10
With ActiveSheet
lRow = .Cells(.Rows.Count, i).End(xlUp).Row
If lRow < 3 Then Exit Sub
.Cells(1, 1).Offset(1, 0).Resize(lRow - 1).SpecialCells(xlCellTypeVisible).Select
End With
i = i + 1
Loop
End Sub
You can select a range by using Range property of ActiveSheet. You already have the last row and you know that the header is in the first row, so your range starts from position A2 and goes to the last row of column J
ActiveSheet.Range("A2:J"&lRow).SpecialCells(xlCellTypeVisible)
If you want to copy this range, use Copy function like
yourRangeAsAbove.Copy
This function only moves the selection to memory, to paste it, build your destination range and call PasteSpecial function.
I came across this answer googling my issue for: deleting of filtered selection in vba.
However trying your answer &lRow gives me an runtime error 1004, application-defineed or object-defined error
I got around it with this
ActiveSheet.Range("A2:G" & Range("A" & Rows.Count).End(xlUp).Row).SpecialCells(xlCellTypeVisible).Delete
For those that may also get the same issue.

Using the left-function [VBA]

I'm trying to create a simple macro that copys the two first numbers in each cell in a column and printing them in a different column. This is in a excel-document with more than 1 worksheet. I've tried for a while to write the code, but with no prior experience, I have a hard time figuring out what to do. I know the "left()"-function is able to do this, but I don't know how I define which column to draw data from and to which column it will be printed. Any help will be greatly appreciated.
With no prior experience to writing VBA code, I am going to reccommend you stick to the formula method of doing. Honestly, even if you were familiar with VBA, you might still opt to use the formula.
A formula is put into the actual cell you want the value to be copied.
=LEFT(sourceCell, #of characters you want)
This is how it would look:
=LEFT(Sheet1!A1, 2)
Think of it as saying "this cell shall equal the first n characters in cell OO, starting from the left".
Once you are done with your formula, if you don't need it to be binded to the source anymore (if the sourceCell changes, so does the cell with the LEFT formula), you can highlight the cells, Ctrl + C to copy, then right-click and select Paste Special. Then select VALUE and hit OK and now the cells are hard-coded with the value they were showing, as if you typed it yourself.
Once you master using formulas, the next step is VBA. Don't go confusing yourself by jumping into VBA and writing code for ranges, etc. if you aren't comfortable with using =LEFT yet. One step at a time, and you'll be a pro before you know it. :)
Here is a quick sample sub to get you started:
Public Sub LeftSub()
Dim SourceRange As Range, DestinationRange As Range, i As Integer
'Define our source range as A1:A10 of Sheet1
Set SourceRange = Sheet1.Range("A1:A10")
'Define our target range where we will print.
'Note that this is expected to be of same shape as source
Set DestinationRange = Sheet1.Range("B1:B10")
'Iterate through each source cell and print left 2 bits in target cell
For i = 1 To SourceRange.Count
DestinationRange(i, 1).Value = Left(SourceRange(i, 1).Value, 2)
Next i
End Sub
How about
Sub foo()
Dim cell As Range
Dim sourceRange As Range
'//define the source column - looks for contiguous downward data from A1;
Set sourceRange = Range(Sheets("Sheet1").Range("A1"), Selection.End(xlDown))
'//iterate each cell
For Each cell In sourceRange
If IsEmpty(cell.Value) Then Exit For
'//example to place the value in corresponding row of column B in sheet 2
Sheets("Sheet2").Range("B" & cell.Row).Value = Left$(cell.Value,2)
Next
End Sub
Or an equivalent formula (in the destination cell)
=LEFT(Sheet1!A1,2)

Resources