Reputation: 171
Could someone help me to do the following: I need to copy (or using a similar VBA function) a range of cells to another worksheet.
However, the range will probably include empty cells that I don't want to include with the data, and I also need to insert the values (rather than Paste) so they don't override stuff already on the destination sheet.
.Value
rather than .Paste
or .PasteSpecial
is preferred.
Hope this makes sense
Here is some code I have tried, but this still overrides (even if the selected cells were empty) the data in the destination sheet.
Sub MakeQuote()
Application.ScreenUpdating = False
Sheets("Code Input Sheet").Range("A9:C800").Copy
Workbooks.Open("C:\Users\j\Documents\Trial.xltm").Activate
Sheets("Special Quote").Range("A4").PasteSpecial xlPasteValues, skipBlanks:=False
Application.CutCopyMode = False
Err_Execute:
If Err.Number = 0 Then MsgBox "Copying Successful :)" Else _
MsgBox Err.Description
Application.ScreenUpdating = True
End Sub
So basically it needs to find just the data and copy/insert it into the destination sheet.
Being used to HTML and PHP this is a bit of a jump in the dark for me :)
Many thanks everyone
Upvotes: 1
Views: 4986
Reputation: 12499
Simple Value to Value From Range
Example
Option Explicit
'// Value to Value
Sub Value_To_Value()
Dim FilePath As String
'// Workbook1 File Path
FilePath = Environ("USERPROFILE") & "\Documents\Temp\" '<- Change Path
With Range("A1:A10")
.Value = "='" & FilePath & "[Workbook1.xlsm]Sheet1'!B1:B10"
End With
End Sub
MSDN Environ Function
Upvotes: 0
Reputation: 446
A sample of what kind of code you are looking for is below.
However I would recommend doing a few more Google searches on VBA programming before posting similar questions. This will let you improve upon code listed below. For example, Cells.SpecialCells(xlCellTypeLastCell).Row
uses a build in function/method to figure out what is the last row you wish to copy; which may or may not be what you are looking for.
Further this question highly resembles other questions on Stack Overflow for example: Copy range values one by one
Nevertheless if you wish to use the code below just change the source worksheet from "Sheet1" to whichever sheet your source data is from, and the source range from "C1" to whichever range on that sheet you are using. Similarly you'll need to do with the target.
Sub rangeCopy()
Dim sourceRange As Range
Dim targetRange As Range
Dim lastRow As Long
Dim sourceCounter As Long
Dim targetCounter As Long
Dim outString As String
'Change the source and target sheet as needed.
Set sourceRange = Sheets("Sheet1").Range("C1")
Set targetRange = Sheets("Sheet2").Range("A1")
lastRow = sourceRange.Parent.Cells.SpecialCells(xlCellTypeLastCell).Row
For sourceCounter = 0 To lastRow - 1
'Copy the cell you want to transfer from the source apge
outString = Trim(sourceRange.Offset(sourceCounter).Value)
'Find the next empty cell in the target worksheet
While (Trim(targetRange.Offset(targetCounter).Value) <> "")
targetCounter = targetCounter + 1
Wend
targetRange.Offset(targetCounter).Value = outString
Next
End Sub
Updated Code
This code was updated to match an input source range that contains multiple cells.
sub rangeCopy()
Dim sourceRange As Range, loopRange As Range
Dim targetRange As Range
Dim lastRow As Long
Dim sourceCounter As Long
Dim targetCounter As Long
Dim outString As String
Dim startRow As Long
Dim startCol As Long
Dim endCol As Long
Dim colCounter As Long
'Change the source and target sheet as needed.
Set sourceRange = Sheets("Sheet1").Range("B2:C34")
Set targetRange = Sheets("Sheet2").Range("A1")
startRow = sourceRange.Row
lastRow = sourceRange.Rows.Count
startCol = sourceRange.Column
endCol = sourceRange.Columns.Count - 1
Set loopRange = sourceRange.Parent.Cells(startRow, startCol)
For colCounter = 0 To endCol
targetCounter = 0
For sourceCounter = 0 To lastRow - 1
'Copy the cell you want to transfer from the source apge
outString = Trim(loopRange.Offset(sourceCounter, colCounter).Value)
'Find the next empty cell in the target worksheet
While (Trim(targetRange.Offset(targetCounter, colCounter).Value) <> "")
targetCounter = targetCounter + 1
Wend
targetRange.Offset(targetCounter, colCounter).Value = outString
Next
Next
End Sub
Upvotes: 2