Forum Discussion
Gabrijel Kajic
Sep 17, 2018Copper Contributor
Need HELP! Determine Range/Conditions for Copy/Paste Procedure
Hello@all,
I need help defining my copy/paste process. The situation is as follows:
- I need to search for for specific keywords in a sheet of wb1 and copy/paste it to wb2 under certain conditions.
- I dont know the specific sheet or the position of the keywords, so every sheet in the wb should be checked
- In case a keyword is found - condition 1 or condition 2 will be applied, depending on the keyword:
- Condition 1: if keyword in wb1 = "mx1" then copy/paste keyword to wb2 (specific position -> Sheet2, K7) and rename it to "Male". Result would be: "Male" in K7 of Sheet2 in wb2.
- Condition 2: if keyword in wb1 = "Data 1" then copy the value(integer) of the adjoining cell to the right of it and paste to wb2 (specific position -> Sheet3, K3). Result would be: "189" in K7 of Sheet3 in wb2.
- A keyword can only have one of the conditions assigned.
- Actually, my goal is to have a set of keywords, which have condition 1 or condition 2 assigned, as well as a specific paste-location in wb2. So, every sheet should be checked according to the set of keywords.
Example: https://imgur.com/a/8VCNsrC
Code so far - only thing I need is condition 1 and 2, instead of the fixed value range....
Public Sub TransferFile(TemplateFile As String, SourceFile As String)
Dim wbSource As Workbook
Set wbSource = Workbooks.Open(SourceFile) 'open source
Dim wbTemplate As Workbook
Dim NewWbName As String
Dim wsSource As Worksheet
For Each wsSource In wbSource.Worksheets 'loop through all worksheets in source workbook
Set wbTemplate = Workbooks.Open(TemplateFile) 'open new template
'/* Definition of the value range - This is the part which has to be changed. Instead of the fixed values/range below, I need condition 1 and condition 2.*/
With wbTemplate.Worksheets("Sheet1")
.Range("A2").Value = wsSource.Range("A2").Value
.Range("A3").Value = wsSource.Range("A3").Value
.Range("B2").Value = wsSource.Range("B2").Value
.Range("B3").Value = wsSource.Range("B3").Value
End With
NewWbName = Left(wbSource.Name, InStr(wbSource.Name, ".") - 1)
wbTemplate.SaveAs wbSource.Path & Application.PathSeparator & NewWbName & "_New.xlsx"
wbTemplate.Close False 'close template
Next wsSource
wbSource.Close False 'close source
End Sub
Would appreciate any help!
Gabe
1 Reply
- Gabrijel KajicCopper Contributor
anyone? :)