I'm a very beginner at vba. I'm trying to write a macro that helps me to loop through all workbooks in a folder going through each cell of that workbook. If the cell is empty then paste it onto the cell of a master workbook with the same position as the original one (these workbooks share the same format). this is what I have and I know that it's wrong. Please help me out. Really appreciate your advice. Thanks.
Sub copycat()
Dim x As Range
Set x = ActiveCell
For Each Cell In
Workbooks("work2.xlsm").Worksheets("sheet1").Range("a1:j10")
If IsEmpty(Cell.Value) = False Then
Workbooks("work2.xlsm").Worksheets("sheet1").Range("x").Copy
Workbooks("work2.xlsm").Worksheets("sheet1").Range("x")
End If
Next Cell
End Sub
The code below will copy all the none-empty cells in your Range.
You have not specified the destination Workbook you want to paste these values.
Sub copycat()
Dim Cell As Range
For Each Cell In Workbooks("work2.xlsm").Worksheets("sheet1").Range("A1:J10")
If Trim(Cell.Value) <> "" Then ' if cell is not empty
Cell.Copy ' <-- copy the cell
' not sure where you want to paste it ??
End If
Next Cell
End Sub
Related
I am trying to copy all the used cells in an excel worksheet from a closed workbook into an already created worksheet in my current workbook using VBA.
Have seen lots of examples on copy the sheet as a new sheet but not just the used contents into an existing sheet.
Thanks!
Mike
Open the source workbook, using:
set wb = Workbooks.Open FileName:="fullPathAndFileName.xlsx", UpdateLinks:=0, ReadOnly:=True, AddToMru:=False
Using a reference to the sheet you want to copy, eg., wb.Sheets(1) (refers to the first sheet in the workbook), and a reference to your destination sheet, eg. destWorkSheet run a loop like this:
For Each cel in wb.Sheets(1).UsedRange.Cells
destWorkSheet.Cells(cel.Row, cel.Column) = cel
Next
This will copy the values cell by cell to the same location in the destination worksheet as they are in the source worksheet. You probably want to turn calculation off first. There are certainly other ways to do it as well.
You will still need to open the source workbook, but another approach is to do a direct write of Values. You can do this as long as the destination and source ranges are the same size and shape. This subroutine will do it for you:
Private Sub CopyValuesSourceToDest(src As Range, dest As Range)
'dest should be one cell, src can be a range of cells
If dest.Cells.Count <> 1 Then MsgBox "Only pass one cell as the destination.": Exit Sub
Dim rws As Long, cols As Long, trueDest As Range
rws = src.Rows.Count
cols = src.Columns.Count
Set trueDest = dest.Parent.Range(dest.Cells(1, 1), dest.Cells(1, 1).Offset(rws - 1, cols - 1))
trueDest.Value = src.Value
End Sub
You would then call the sub like this:
CopyValuesSourceToDest sourceSheet.UsedRange, destSheet.Range("B7") 'B7, or whatever
I have written data in a cell that is updated by a macro - it appends a reference depending on what somebody has called a new sheet.
In Cell A1 I end up with macro code that is updated with the new sheet name. Currently users have to copy this text and open another macro and paste the code in, however they keep doing it wrong and breaking it.
What I would like to do is write a macro to copy the contents of Cell A1 and paste them into the original macro.
If it possible to do this?
Based on a suggestion at the MrExcel.com forum:
Sub aaa()
For i = 1 To Application.VBE.CodePanes.Count
Application.VBE.CodePanes(i).CodeModule.ReplaceLine 1, "alert('Yo') ' New code"
Next i
I know this is not an answer to your question but I'm just offering some information. I would suggest that you don't have users create a macro in each sheet. You can access anything on any sheet from a module. I am not sure what you whole process is but you could think of it more along the lines of looking for the sheets you want to change.
Public sub ProcessSheets()
Dim ws As Excel.Worksheet
Dim iIndex As Integer
'Loop through all the worksheets in the workbook.
For iIndex = 1 To ActiveWorkbook.Worksheets.count
'Activate the current sheet, if you need to.
Set ws = Worksheets(iIndex)
ws.Activate
'Check the name of the worksheet.
If Left(ws.Name, 2) = "HD" or Left(ws.Name, 2) = "ER" Then
'Call the function that changes the worksheet here.
'Maybe feed it the worksheet name.
UpdateWorksheet ws.Name
End if
Next iIndex
End Sub
I have been working on a code that I need to always copy the lat NON empty cell of column C in the spreadsheet called "Support2". Then I need to paste in the spreadsheet "Final", always on the cell A2. So I will update the spreadsheet everyday and more values will be added on Column C, that's why it needs to copy always the last one. I have tried the code below but it is not working. I would appreciate your help, Thanks!
Sub test()
Dim myLastCell As Range
Set myLastCell = LastCell(Worksheets("Support2").Range("C:C"))
End Sub
' Now Copy the range:
Worksheets("Support2").Range("C:c" & myLastCell.Row).Copy
Else
MsgBox ("There is no data in specified range")
End If
End Sub
Needed another Sheet("Support2) in it
Sub test()
Sheets("Support2").Range("C" & Sheets("Support2").Range("C1").End(xlDown).Row).Copy
Sheets("Final").Range("A2").PasteSpecial (xlPasteValues)
End Sub
i need a macro to copy and paste a certain cells(b5:d10) from multiple worksheets and paste it into a new single worksheet. just i want to collide the contents .thanks in advance
sub copyrange()
range("b5:d10).copy
range("e1").select
activesheet.paste
application.cutcopymode=false
endsub
my code doesnot copy all the worksheets data. kindly help me to copy and paste it
Sub copyrange()
Dim rngTarget As Range, wksTemp As Worksheet
Set rngTarget = ThisWorkbook.Worksheets(1).Range("A2:C7")
For Each wksTemp In ActiveWorkbook.Worksheets
rngTarget.Value = wksTemp.Range("B5:D10").Value
Set rngTarget = rngTarget.Offset(6)
Next wksTemp
End Sub
I have a macro that selects things in a sheet.
Prior to running the main section of the macro I want to save the active selection, so that I can set the same selection at the end of the macro.
I've tried the solution below, but it doesn't work. I'd appreciate suggestions.
Dim rng As Range
'Beginning of macro
rng = Range(ActiveSheet.Selection) 'Object doesn't support this property or method
'Main section
'End of macro
rng.Select
Solution offered by tmoore82 is the best way to go (+1)
For completeness you can also save the Address as a string:
Dim selectionAddress as String
selectionAddress = Selection.Address 'e.g. A1 is "$A$1"
'Your macro
Range(selectionAddress).Select 'At end of macro select cell A1
Instead of rng = Range(ActiveSheet.Selection), it should be Set rng = Selection.