I have table like this, but bigger.
My code:
Sub copy()
Range("A2").Copy
End Sub
I want the button to adapt to the cell next to it as I copy it. From say, A2 to A3 to A4 etc.
This does essentially what you are trying to do:
Sub Test()
Dim rw As Long
With ThisWorkbook.Sheets("Sheet1") 'Change accordingly
rw = .Shapes(Application.Caller).TopLeftCell.Row
.Cells(rw, 1).Copy
End With
End Sub
Assign this macro to all the buttons you have created in the cells on column B
If you need this on more buttons for example column D and F, and you want to copy respectively C and E, you could use a more generic piece of code:
Sub Test()
Dim rw As Long, cl As Long
With ThisWorkbook.Sheets("Sheet1")
rw = .Shapes(Application.Caller).TopLeftCell.Row
cl = .Shapes(Application.Caller).TopLeftCell.Column
.Cells(rw, cl - 1).Copy
End With
End Sub
Your question is quite unclear.
If you are looking for a macro that would allow your button to copy the cell to its left,
you make your button as a range.
When you select, obtain the column and row of your selection.
Sub copy()
Dim TargetCell As Range
Set TargetCell = Selection
TargetCell(ActiveCell.column - 1, ActiveCell.row).Copy
End Sub
I hope this might be helpful to you in any way.
Related
Hope you can help me.
I want to copy and paste a cells value based on when you click a hyperlink on that cell.
So for example, I have a sheet called Form1, I want to click on an ID in column A it will then copy the value of that cell and paste it to B2 in sheet1 and then take me to sheet 1.
Currently I have a macro that allows me to click on an active cell and then press a button which then does what is mentioned above. I just think a hyperlink press would be more user friendly and would result in less errors.
Here is the code I have at the moment:
Sub Rectangle13_Click()
ActiveCell.Copy Destination:=Sheets(“Sheet1”).range(“B2”)
Worksheets(“Sheet1”).Activate
End Sub
Any help would be appreciated! Thank you
Worksheet FollowHyperLink
Copy this code to the sheet module of worksheet Form1. From there, run the second sub to convert the A column to hyperlinks.
Click away.
Option Explicit
Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
If Not Intersect(Columns("A"), Target.Range) Is Nothing Then
Me.Parent.Worksheets("Sheet1").Range("B2").Value = Target.Range.Value
End If
End Sub
' Run this to create hyperlinks out of the values in column 'A'
' which will point to cell 'B2' in 'Sheet1'. You can then reformat
' the cells (remove underline, change font color, ...).
Private Sub CreateHyperlinks()
Dim lRow As Long: lRow = Range("A" & Rows.Count).End(xlUp).Row
Dim cell As Range
For Each cell In Range("A2:A" & lRow).Cells
cell.Hyperlinks.Add cell, "", "Sheet1!B2", , CStr(cell.Value)
Next cell
End Sub
It is inconvenient to use Hyperlik, I think. If you try changing the cell value, you cannot simple click it and write something... But if you want that, you can create such a hyperlink in the next way:
Sub testAddHyperlink()
Dim DestSh As Worksheet
Set DestSh = Sheets("Sheet1") 'you can use any sheet name here
ActiveSheet.Hyperlinks.Add Range("A1"), Address:="", SubAddress:="'" & DestSh.name & "'" & "!A1"
'it will keep the existing cell value
End Sub
Then, please copy the next event code in the sheet code module (where the hyperlink exists):
Option Explicit
Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
Dim rngCopy As Range, shDest As Worksheet
Set shDest = Sheets("Sheet1") 'you may use here any sheet name
shDest.Range("B2").value = Target.Parent.Value 'the sheet where the hyperlink targets, is already activated...
End Sub
If you want to use the BeforeDoubleClick approach, paste this code into your Form1 worksheet object in the VBA editor ...
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Target.Cells.Count = 1 And Target.Cells(1, 1).Column = 1 Then
Cancel = True
With ThisWorkbook.Worksheets("Sheet1")
.Range("B2") = Target.Value
.Activate
End With
End If
End Sub
... naturally, this is a base example and you may need to modify it accordingly. For example, when you double click, you may want to ignore the first row if it's a header and not invoke the core parts of the logic.
That will then do what you want.
I have a list of fruit in A1:A20, & then the user of my excel file will give the check mark ("V") in B1:B20 if they want to choose one of fruit in the list of column A.
The problem is I want automatically to summarise in sheet2 a list of fruit which the user give the check mark in column B. Do you guys any idea how to solve it?
With Microsoft365, Excel for iPad, etc., you have access to the FILTER function:
FILTER
You set the data range you want to use as a result set, and then set the criteria to filter by, in your case Sheet1!A1:A20 and Sheet1!B1:B20="v" respectively.
Place this in A1 on Sheet2, and the list is created and then automatically updated.
=FILTER(Sheet1!A1:A20,Sheet1!B1:B20="v")
Try using a PivotTable
..........
This uses a "helper column". In Sheet1 cell C1 enter:
=IF(B1<>"",1,0)
In Sheet1 cell C2 enter:
=IF(B2="","",1+MAX(C1:$C$1))
and copy downwards. A typical example:
Note that column C marks the selected fruits with a simple sequential inter sequence.
Finally in `Sheet2 cell A1 enter:
=IF(ROWS($1:1)>MAX(Sheet1!C$1:C$20),"",INDEX(Sheet1!A$1:A$20,MATCH(ROWS($1:1),Sheet1!C$1:C$20),0))
and copy downwards.
If you need VBA for another reason then:
Sub qwerty()
Dim i As Long, rng As Range, r As Range
Dim s1 As Worksheet, s2 As Worksheet
Set s1 = Sheets("Sheet1")
Set s2 = Sheets("Sheet2")
Set rng = s1.Range("B1:B20")
i = 1
For Each r In rng
If r.Value <> "" Then
s2.Cells(i, 1).Value = r.Offset(0, -1).Value
i = i + 1
End If
Next r
End Sub
you could place this in Sheet1 code pane (rightclik on Sheet1 tab and select "View Code")
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Range("B1:B20")) Is Nothing Then Exit Sub
With Sheet2
.Range("A1:A20").ClearContents
Dim cel As Range
For Each cel In Range("A1:A20")
If cel.Offset(, 1) = "v" Then .Cells(.Rows.Count, 1).End(xlUp).Offset(1).Value = cel.Value2
Next
End With
End Sub
this way, everytime the user operates on Sheet1 B1:B20 cells, the Sheet2 list is automaticaly updated
I'm having an Excel Spreadsheet with 3 sheets inside and I need to copy certain cell range from Sheet1 and copy it to Sheet2.
And I'm trying to get the range of cells to be copied as an input in a cell that is available in Sheet 3. Like the cell would have value A4:X6 in it. But I'm unable to get the input values passed on to the Range function in my Macro code.
Below is my code and when I execute, it just enters an empty row in the Sheet 2
Sub CopyData()
Sheet3.Select
Set Range1 = Range(Cells(3, 3).Value)
Sheet1.Select
Range1.Copy
Sheet2.Select
Range("A2").Select
Range("A2").EntireRow.Insert Shift:=xlShiftDown
End Sub
I want the contents of cell denoted in Range1 to be copied from Sheet1 and pasted in Sheet2.
Thank you in advance!
John Coleman is right you can avoid using Select for the whole subroutine. But, your problem here is when you define the range it is defining it specifically for Sheet3 and not Sheet1. One alternative is you could store the address in a string that gets passed to the Range() function, but specify which sheet you want your range to reflect. The rest of the code can be handled much the same without using Select.
Sub CopyData()
Dim range1 as Range
dim strRange as String
strRange = Sheet3.Cells(3, 3).Value
Set range1 = Sheet1.Range(strRange)
range1.Copy Sheet2.Range("A2")
Sheet2.Range("A2").EntireRow.Insert Shift:=xlShiftDown
End Sub
Use Set Range1 = Sheet3.Range(Cells(3, 3).Value) instead of Set Range1 = Range(Cells(3, 3).Value) or the range get selected from sheet1 because of Sheet1.Select
when i execute, it just enters an empty row in the Sheet 2 Of course it does. Your code does exactly that. Line Range("A2").EntireRow.Insert Shift:=xlShiftDown creates the row. There is nothing in your code that pastes the content of range A4:X6 ot whatever input you got in the cell.
Actually, if you delete your code and leave it like this:
Sub CopyData()
Range("A2").EntireRow.Insert Shift:=xlShiftDown
End Sub
You will get the same, a new row inserted.
I want the contents of cell denoted in Range1 to be copied from Sheet1 and pasted in Sheet2
I guess you are trying to copy a specific range, not a whole row and paste it, you need something like this:
Sub CopyData()
Dim Range1 As Range
Set Range1 = Sheet1.Range(Sheet3.Cells(3, 3).Value)
Range1.Copy
Sheet2.Range("A2").PasteSpecial (xlPasteAll) 'this command will paste the contents
End Sub
This example shows how to insert a line above line 2, copied to the format of the line down (line 3) and from the header line
Range("2:2").Insert CopyOrigin:=xlFormatFromRightOrBelow
As you understood, .Insert will always insert blank row.
I guess that you would like to paste a range in your sheet and not insert a new row for this you should do like this :
Sheets("SheetName").Range("A2").PasteSpecial (xlPasteAll)
Also note that xlPasteAll is an XlPasteType as xlPasteFormats , xlPasteValues and so on.
xlPasteAll will paste all
xlPasteFormats will paste the source format
xlPasteValues will paste the value
So your code would be as below :
Sub CopyData()
Dim Range1 As Range
Dim Range2 As Range
Set Range1 = Sheet1.Range(Sheet3.Cells(3, 3).Value)'Will define the range you want to copy
Range1.Copy 'here you copy the range
Set Range2 = Sheet2.Range("A2") 'Set the range where you want to paste data
Range2.PasteSpecial (xlPasteValues) 'then you will paste your range
End Sub
Click here to get the list of those XlPasteType
BONUS
Sheet2.Select
Range("A2").Select
is the same as
Set Range2 = Sheet2.Range("A2")
But the last way is better because it avoid Select which can slow down your performances !
Is there a specific requirement for inserting the copied data at the top or would you be happy adding it to the end of the "list" instead? If so, you could find the last used row and add it at the bottom instead like this:
Sub CopyFromSheet1toSheet2()
Dim thisBook As Workbook: Set thisBook = ThisWorkbook
Dim sheetOne As Worksheet: Set sheetOne = thisBook.Worksheets("Sheet1")
Dim sheetTwo As Worksheet: Set sheetTwo = thisBook.Worksheets("Sheet2")
Dim copyFromRange As Range: Set copyFromRange = sheetOne.Range("A4:X6")
Dim lastRow As Long: lastRow = sheetTwo.Cells(Rows.Count, 1).End(xlUp).Row
Dim pasteToRange As Range: Set pasteToRange = sheetTwo.Range("A" & lastRow)
copyFromRange.Copy Destination:=pasteToRange
End Sub
"lastRow" returns the numeric value of the last used row in a given column. If you have data in A1:A4 then this code would add the next lot of data copied to A5 and below.
I'm new to vba and I can't manage to do what I want although it's very simple.
I need to automatically add textbox values from a userform to my second sheet. For example in each textboxes I provide an integer and I want to put this values 1 by 1 on the range C5:C52.
I have the following code that loop trough my textbox collection and range C5:C52
Sub remplissageTab()
Dim rng As Range
Dim cell As Range
Set rng = Sheets("Câbles").Range("C5:C52")
For Each txtBox In clcTxt
For Each cell In rng
cell.Value = CInt(txtBox)
Next cell
Next txtBox
Unload Me
End Sub
However the results is not what I expect. It only prints the last textbox value through all ranges. But I want all values in the same order textboxes are created.
I hope I made clear explanations.
What do you think ?
Thanks a lot for your help. Lyess
Like so:
Sub remplissageTab()
Dim rng As Range
Dim Rw as Long
Set rng = Sheets("Câbles").Range("C5:C52")
For Each txtBox In clcTxt
rng.Offset(Rw).Value = CInt(txtBox)
Rw = Rw + 1
Next txtBox
Unload Me
End Sub
I have two workbooks the first one is called classeur1 and the second is called classeur2.
I cannot copy / paste merged rows from a table from Sheet1 (from Workbook1) to Sheet2 (from Workbook2).
I would like to know how to do it.
In fact I tried but no result. Here is my code:
Sub test()
Dim finalrow As Long
Workbooks("workbook1").Worksheets("sheet1").Range("D1:D" & finalrow).Value = Workbooks("workbook2").Worksheets("sheet2").Range("A2:A" & finalrow).Value
End Sub
One of your ranges starts at D1 and the other at A2 but both go to finalRow. This makes them different size ranges. I'll assume that you have actually assigned a row number to finalRow but I won't use it.
Option Explicit
Sub test()
With Workbooks("Classeur2").Worksheets("Feuil2")
With .Range(.Cells(2, "A"), .Cells(.Rows.Count, "A").End(xlUp))
Workbooks("Classeur1").Worksheets("Feuil1").Range("D1").Resize(.Rows.Count, .Columns.Count) = .Value
End With
End With
End Sub
You can try to define the name for your table by selecting the range, and Insert -> Tables -> Table. Let's say the name is "Table1"
Option Explicit
Sub CopyTable()
'Select the defined Table1 from classeur1 workbook and copy it to classeur2 starting at cell D1
Workbooks("classeur1").Worksheets("Sheet1").Range("Table1[#All]").Copy _
Workbooks("classeur2").Worksheets("Sheet1").Range("D1")
End Sub