VBA Excel: copy paste named range with hidden rows - excel

Is there any smart way to copy paste values from a named range from one sheet to another not including hidden rows?

Suppose you have a named range called MyRange then you can copy visible cells only to another worksheet using:
Sub CopyNamedRange()
Range("MyRange").SpecialCells(xlCellTypeVisible).Copy Destination:=Worksheets("Sheet2").Range("A1")
End Sub

Related

Copy Cell From Range On Active Row

I'm trying to use VBA to copy a cell on the active row of a range.
A1:A10 is a range named "Panel_Length". I want to click a button to copy the cell from column "A" of the active row. The code works if I specify column "A" and active row but there is a chance that the range "Panel_Length" may get moved to another column.
Sub Plen_Click()
Dim PLength as Range
Set PLength = Sheet1.Range("Panel_Length")
Range(PLength & ActiveCell.Row).Copy
End Sub
Don't know if I understand the question correct. but if you try to copy the cell from column of the named range and the row form the selected cell you can try the code below
Sub Plen_Click()
Sheet1.Cells(Selection.Row, Sheet1.Range("Panel_Length").Column).Copy
End Sub

Copy and Paste Column Regardless of Worksheet Open

I currently have 3 worksheets in my workbook ("SheetJS", "Sheet1", and "Sheet2").
I want the macro to copy and paste the values of Sheet1 from Column K to Column L.
My code works but copies and pastes the columns on whichever worksheet is open/active.
I want the macro to code and paste the values only in "Sheet1" regardless of which one of the worksheets is open.
Any tips would help.
Thanks!
Sub Copy_K_to_L_2()
With ThisWorkbook.Sheets("Sheet1")
Columns("K:K").Copy Destination:=Columns("L:L")
End With
End Sub
Try to edit your existing line:
Columns("K:K").Copy Destination:= Thisworkbook.sheets("Sheet1").Columns("L:L")

How to copy data from one worksheet to another (using an indirect reference for the latter worksheet)

I'm trying to add some simple VBA functions to excel files at work (i work for a marketing company) and am struggling to code a button that can copy data from one sheet to another (the position it needs to copy to changes daily)
I'm using VBA to try and select the range of values i need copied (and it works) but i'm struggling to paste them in the daily-changing cells required.
I've tried writing a formula that presents a pathway to the correct cells and then introducing an indirect function to the VBA code for pasting but i can't seem to get it to work.
Sub Gross_Wrap_Click()
Sheets("AUTOM").Select
Range("C1:C5").Select
Selection.Copy
Sheets("Daily").Select
Range([indirect("'AUTOM!O7'")]).Select
Selection.Paste
End Sub
I expect the end product to be the contents of cells C1:C5 in the AUTOM sheet copied and pasted to the required cells in the DAILY sheet (the required cells are derived from a function and included in VBA with an Indirect pathway).
However, i'm just getting a load of different error messages
If I didn't messed it reading your code, this should do it:
Option Explicit
Sub Test()
Dim rng As Range 'Range object, means a cell or a range of cells
With ThisWorkbook 'here you reference the workbook, so you don't need to write Thisworkbook everytime, just "."
Set rng = .Sheets("Daily").Find(.Sheets("AUTOM").Range("O7")) 'we set the object rng to the cell in sheets Daily which has the Cell O7 from AUTOM sheet value.
.Sheets("AUTOM").Range("C1:C5").Copy 'copy the range from AUTOM
rng.PasteSpecial xlPasteValues 'and paste it's values to the range object we declared and defined above
End With
End Sub

Run VBA Function in selected sheets

I need to write VBA code that will delete column B from all worksheets in the open workbook except for Sheet1. All these worksheets are on the right from Sheet1, and their names are as well stored as range in sheet1 in column AA. Moreover in each of these worksheets (apart from Sheet1) in column A have to be insert autonumbering (1.2.3. etc) beginning from cell A2 and going down. In each worksheet column headings are the same but number of rows is different depending on data included in each sheet. I dont know how to repeat this macro in every sheet.
You need to loop using the Worksheets collection contained in the ActiveWorkbook object:
Dim sheet As Worksheet
For Each sheet In ActiveWorkbook.Worksheets
If Not sheet.Name = "Sheet1" Then
Debug.Print sheet.Name
End If
Next
When you're inside that loop the sheet object is just a normal Worksheet object and you can do anything you would normally do to the ActiveSheet.

How to copy a cell value from one excel file and paste it to another excel file?

I would like to copy a cell value from one workbook to another, but the problem is that the cell values are from different sheets of the workbook. How can I do this? Could you help me with this?
I'm guessing you want a formula, and not instructions on copy and paste.
Cell references can be made using the following format.
=[Workbook Name]SheetName!CellAddress
Example, you have two workbooks open, Book1 and Book2.
Book1, Sheet1, Cell A1 = =[Book2]Sheet1!A1
Book2, Sheet1, Cell A1 = Hello World
The value of [Book1]Sheet1!A1 will now be "Hello World" (it will always reflect the value that is in [Book2]Sheet1!A1, providing the book is open)
If you do want a copy of the actual value, and not a reference to it, you will need to just use plain copy and paste or use the above formulas and when you're done use copy+paste special (paste values) to convert the formulas to their values.
Update
Just realised it was tagged excel-vba so you'd need to use something like #DavidZemens' answer.
You could also use the macro recorder to do this, but doing it manually like David suggests will result in nicer code that's easier to re-use/modify later on.
This is the bones of what you need, you can do a lot more with looping over ranges of cells, sheets in workbooks, etc., but fundamentally you will need to specify in VBA what is the target workbook, worksheet, and cell range, and then set that .Value equal to a specific workbook/worksheet/cell .Value in another workbook.
I use Workbook and Worksheet variables in this example, you can also use range variables for the cell(s) if you desire or if you need to apply to multiple cells.
Sub TransferValues()
Dim wb1 as Workbook
Dim wb2 as Workbook
Dim ws1 as Worksheet
Dim ws2 as Worksheet
Set wb1 = Workbooks("First Workbook.xlsx") '<modify as needed'
Set ws1 = wb1.Sheets("The sheet name") '<modify as needed'
Set wb2 = Workbooks("Another Workbook.xlsx") '<modify as needed'
Set ws2 = wb2.Sheets("Another sheet name") '<modify as needed'
'This line puts the value from wb1.ws1 in wb2.ws2, cells specified:
ws2.Range("A1").Value = ws1.Range("A1").Value
End Sub

Resources