Have a few questions about the most efficient way to copy data from one worksheet to another.
Have looked at a few texts, references online, including this forum. Below is what I have so far. It works, but I suspect it is awkward.
Looking for comments and suggestions. Thanks.
Q1: Is there a more efficient way to select a range?
Q1.1 example #1 could the following be condensed to 2 lines rather than 3?
Dim gNames As Range
Range("B3").Select
Set gNames = Range(Selection, Selection.End(xlToRight))
Q1.2: could the following be condensed to a single line?
Range("A3").Offset(1, k - 1).Select
Range(Selection, Selection.End(xlDown)).Select
Q2: is there a more efficient way to select, copy, and paste?
e.g. (note that c and k are defined in the full code below. c is a Cell and k is an integer).
Range("A3").Offset(1, k - 1).Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
Worksheets("Sheet1").Cells(c.Row + 1, c.Column).Select
ActiveSheet.Paste
Q3: Any recommendations about the following code?
This is just a test program. Eventually this will be scaled up to handle about 200 keys, and about 5000 data values for each key.
keys and data will be on different worksheets. The data copied below the keys will be used to generate some Excel graphs.
Sub CopyStoS()
'
' Objective: copy data from one region to another, based on the value of a key.
' Details:
' gNames: List of keys starting on B3 (length of list is variable, e.g. B3 to D3 or B3 to F3)
' Actions:
' 1. for each key in gNames, check if the key is found in the range I3:L3
' 2. if key is found in range I3:L3, copy the data in that column (e.g. I4:I44, the length of data is variable) to below the B3 key.
'
Dim k As Integer
Dim gNames As Range
Range("B3").Select
Set gNames = Range(Selection, Selection.End(xlToRight))
For Each c In gNames
k = Find_Index(Trim(c.Value))
If (k > -1) Then
MsgBox ("found it" & CStr(k))
Range("A3").Offset(1, k - 1).Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
Worksheets("Sheet1").Cells(c.Row + 1, c.Column).Select
ActiveSheet.Paste
End If
Next
End Sub
Function Find_Index(findString As String) As Integer
Dim rng As Range
Dim i As Integer
Dim found As Boolean
If Trim(findString) <> "" Then
With Sheets("Sheet1").Range("I3:L3") 'searches specified Range in Sheet 1
Set rng = .Find(What:=findString, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not rng Is Nothing Then
found = True
i = rng.Column
Else
found = False
i = -1
End If
End With
End If
Find_Index = i
End Function
Have looked at a few texts, references online, including this forum. Below is what I have so far. It works, but I suspect it is awkward.
Looking for comments and suggestions. Thanks.
Q1: Is there a more efficient way to select a range?
Q1.1 example #1 could the following be condensed to 2 lines rather than 3?
Dim gNames As Range
Range("B3").Select
Set gNames = Range(Selection, Selection.End(xlToRight))
Q1.2: could the following be condensed to a single line?
Range("A3").Offset(1, k - 1).Select
Range(Selection, Selection.End(xlDown)).Select
Q2: is there a more efficient way to select, copy, and paste?
e.g. (note that c and k are defined in the full code below. c is a Cell and k is an integer).
Range("A3").Offset(1, k - 1).Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
Worksheets("Sheet1").Cells(c.Row + 1, c.Column).Select
ActiveSheet.Paste
Q3: Any recommendations about the following code?
This is just a test program. Eventually this will be scaled up to handle about 200 keys, and about 5000 data values for each key.
keys and data will be on different worksheets. The data copied below the keys will be used to generate some Excel graphs.
Sub CopyStoS()
'
' Objective: copy data from one region to another, based on the value of a key.
' Details:
' gNames: List of keys starting on B3 (length of list is variable, e.g. B3 to D3 or B3 to F3)
' Actions:
' 1. for each key in gNames, check if the key is found in the range I3:L3
' 2. if key is found in range I3:L3, copy the data in that column (e.g. I4:I44, the length of data is variable) to below the B3 key.
'
Dim k As Integer
Dim gNames As Range
Range("B3").Select
Set gNames = Range(Selection, Selection.End(xlToRight))
For Each c In gNames
k = Find_Index(Trim(c.Value))
If (k > -1) Then
MsgBox ("found it" & CStr(k))
Range("A3").Offset(1, k - 1).Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
Worksheets("Sheet1").Cells(c.Row + 1, c.Column).Select
ActiveSheet.Paste
End If
Next
End Sub
Function Find_Index(findString As String) As Integer
Dim rng As Range
Dim i As Integer
Dim found As Boolean
If Trim(findString) <> "" Then
With Sheets("Sheet1").Range("I3:L3") 'searches specified Range in Sheet 1
Set rng = .Find(What:=findString, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not rng Is Nothing Then
found = True
i = rng.Column
Else
found = False
i = -1
End If
End With
End If
Find_Index = i
End Function