Excel 2010 Win 7
I have been trying to teach myself VBA. I am somewhat familiar with
programming but I can't figure this out. Can someone point me in the
right direction?
Thanks
Daphne
I have a spreadsheet with information about members of a club.
I need to copy some of the information to a new workbook depending on
the value of cell D in each row.
For example
if the value of cell A in row 1 is "T" then copy cells B1 to E1 to cell
B1 of the first empty row in a new workbook
but if the value is "F" then go to the next row and repeat the routine
until an empty row is found.
A B C D E
Here are some pieces of example code that might help to get you started
with the basics, I'm sure other people know a lot more about Excel here
but this will probably get you close to what you need.
For starters, do you know how to select a file or open a file from a
file browse dialog? Do either of these help to select the target
workbook where the data will be copied?
http://www.exceltip.com/st/Select_filenames_using_VBA_in_Microsoft_Excel/448.html
Public Function FindFile() As String
'open browse window to select target workbook where data will be copied
Dim fn As String
fn = Application.GetOpenFilename("Excel-files,*.xls", 1, "Select
File To Copy Data To", , False)
If fn = "" Then
MsgBox "Nothing Chosen"
Else
'now that you have the name, you can open it or do something else
'return filename
FindFile = fn
'show user filename they selected (just for testing
MsgBox "You selected the file """ & fn & """", vbOKOnly,
"Target Workbook"
End If
End Function
Sub OpenOneSelectedExcelFile()
Dim fn As Variant
fn = Application.GetOpenFilename("Excel-files,*.xls", 1, "Select
One File To Open", , False)
If TypeName(fn) = "Boolean" Then
' the user didn't select a file
Exit Sub
End If
Debug.Print "Selected file: " & fn
Workbooks.Open fn
End Sub
'--------------------------------------------------------------------------
http://en.allexperts.com/q/Excel-1059/2008/9/Macro-copy-data-workbook.htm
This code example allow you to select a second worksheet from an opened
worksheet that contains this code, and copy data from the first to the
selected sheets.
Public Function FindFile() As String
'browse to select target workbook where data will be copied
'right now it only shows files with the xls file extension
Dim fn As String
fn = Application.GetOpenFilename("Excel-files,*.xls", 1, "Select
File To Copy Data To", , False)
If fn = "" Then
exit function
Else
'return filename
FindFile = fn
End If
End Function
Public Sub CopyDataToTargetBook()
'declare variables
Dim sh1 As Worksheet, sh2 As Worksheet, bk2 As Workbook
Dim row As Long, col As Long
'--------------------------------------------------------
'refer to sheet in open book data will be copied from
Set sh1 = ActiveSheet
Application.ScreenUpdating = False
'--------------------------------------------------------
'open sheet data will be copied to
Set bk2 = Workbooks.Open(FindFile)
'target sheet where data will be copied
Set sh2 = bk2.Sheets(1)
'arbitrary copy code example, not using your conditions
For row = 1 To 8
For col = 1 To 3
sh2.Cells(row, col).Value = sh1.Cells(row, col)
Next
Next
'save the book with the copied data
bk2.Close Savechanges:=True
Application.ScreenUpdating = True
'---------------------------------------------------------
set sh1=nothing
set sh2=nothing
set bk2=nothing
End Sub
Does that get you started?
Mike