'The purpose of this code is to transfer cells from one sheet to another.
'Cell selection is based on selecting one row, based on criteria in one column
'and selecting columns based on the headings in row 1 of the source sheet.
'Names of column headings to transfer cells from is listed in the 'transferSheet'
'By this I can change wich columns to transfer, without editing the vb-code
Option Explicit
Public rowIndx As String 'Value to choose source row
Public rowIndxRange As Range 'The range to look for rowIndx
Public srcSheet As Worksheet 'Source sheet
Public dstSheet As Worksheet 'Destination sheet
Public transferSheet As Worksheet 'Transfer sheet w/ values of column headings
Public srcRange As Range
Public dstRange As Range
Public transferRange As Range
Sub CopyCellsByHeadings()
Dim Row As Integer
Dim srcCol As Integer
Dim dstCol As Integer
Dim srcCell As Range
Dim dstCell As Range
Dim lr As Long
Dim cntr As Integer
Dim columnHeading As String
lr = LastRow(dstSheet) + 1 'Finds the last open row
Row = GetRow(rowIndxRange, rowIndx) 'Get row no. from CONTRSPEC based on selection in list box
For cntr = 2 To LastRowOfColumn(transferRange)
columnHeading = transferRange.Cells(cntr, 1).Value
srcCol = GetCol(srcSheet, columnHeading)
dstCol = GetCol(dstSheet, columnHeading)
Set srcCell = srcRange.Cells(Row, srcCol)
Set dstCell = dstRange.Cells(lr, dstCol)
srcCell.Copy dstCell
Next
End Sub
Private Sub SendButton_Click()
'Settings:
Set srcSheet = Worksheets("CONTRSPEC")
Set dstSheet = Worksheets("LOG")
Set transferSheet = Worksheets("RANGELISTS")
Set transferRange = transferSheet.Range("A:A")
Set srcRange = srcSheet.Range("A:XX")
Set dstRange = dstSheet.Range("A:XX")
rowIndx = ListBox1.Value 'Gets value from list box selection
Set rowIndxRange = srcSheet.Range("A:A")
CopyCellsByHeadings
End Sub
Private Sub ListBox1_Click()
End Sub
Function GetRow(columnRange As Range, colAValue As String) 'Returns row no. where "sym" is found
GetRow = columnRange.Find(What:=colAValue, _
After:=columnRange.Cells(1, 1), _
Lookat:=xlWhole, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
End Function
Function GetCol(sh As Worksheet, colHeading As String) 'Returns col no. where "colHeading" is found
GetCol = sh.Cells.Find(What:=colHeading, _
After:=sh.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Column
End Function
Function LastRow(sh As Worksheet) 'Returns row no. of last empty row
On Error Resume Next
LastRow = sh.Cells.Find(What:="*", _
After:=sh.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
On Error GoTo 0
End Function
Function LastRowOfColumn(columnRange As Range) 'Returns row no. of last empty row of col...
On Error Resume Next
LastRowOfColumn = columnRange.Find(What:="*", _
After:=columnRange.Cells(1, 1), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
On Error GoTo 0
End Function
Function LastCol(sh As Worksheet) 'Returns column no. of last empty column
On Error Resume Next
LastCol = sh.Cells.Find(What:="*", _
After:=sh.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Column
On Error GoTo 0
End Function