Part of my work consists of, basically, copying and pasteing stuff from one Excel…
- User
Part of my work consists of, basically, copying and pasteing stuff from one Excel workbook to another, e.g. data from one specific cell to another one on the other workbook. Checking and validating some data is, also, a part of it.
- Assistant
If you're looking to automate this process, you can use VBA (Visual Basic for Applications) macros in Excel. With VBA, you can write code to perform tasks such as copying data from one workbook to another, validating data, and more. If you're not familiar with VBA, there are many online resources and tutorials available to help you get started. Additionally, there are also pre-built solutions such as Excel add-ins or plug-ins that can streamline the process for you.
- User
Exactly, how would I exactly go about this. For example, my coworkers prepares a workbook he updates every day, then, I use that workbook as inputs for a totally different one. Configuration of cells might differ day to day, but the data would always match the identifier from the left column. How could I set it to do it automatically, or at least, where could I save time in the process. Give me real examples.
- Assistant
Here is an example of how you can write a VBA macro to copy data from one workbook to another based on a shared identifier in the left column:
- Open both workbooks in Excel.
- In the workbook where you want to paste the data, press "Alt + F11" to open the VBA editor.
- In the editor, go to "Insert" and click "Module". This will create a new module where you can write your code.
- User
Lets try this, so I can undestand VBA code more clearly. Call source sheet "abc" from "Prva tabela" workbook, and target sheet is "xdy" from "Druga tabela" workbook. The cell (A24) left to the source cell (B24) is called "Repo", but the cell (B29) left to the target cell (C29) is called "8.9. Repo plasmani". Rewrite the code using this example where common identifier is only one word within the name.
- Assistant
Sure! Here is an updated version of the code using the specified workbook and sheet names, as well as the specified identifier cell names:
Sub CopyDataFromOtherWorkbook() Dim sourceWorkbook As Workbook Dim sourceSheet As Worksheet Dim targetWorkbook As Workbook Dim targetSheet As Worksheet Dim sourceRange As Range Dim targetRange As Range Dim identifier As String Set sourceWorkbook = Workbooks.Open("C:\Path\To\Prva tabela.xlsx") Set sourceSheet = sourceWorkbook.Sheets("abc") Set targetWorkbook = Workbooks.Open("C:\Path\To\Druga tabela.xlsx") Set targetSheet = targetWorkbook.Sheets("xdy") ' Find the last row with data in the source sheet lastRow = sourceSheet.Cells(sourceSheet.Rows.Count, "A").End(xlUp).Row For i = 1 To lastRow identifier = sourceSheet.Cells(i, 1).Value ' Check if the identifier exists in the target sheet If identifier = "Repo" Then ' Set the source range to the row with the matching identifier Set sourceRange = sourceSheet.Range(sourceSheet.Cells(i, 2), sourceSheet.Cells(i, sourceSheet.Columns.Count).End(xlToLeft)) ' Find the matching row in the target sheet matchingRow = Application.Match("8.9. Repo plasmani", targetSheet.Columns(2), 0) ' Set the target range to the matching row in the target sheet Set targetRange = targetSheet.Range(targetSheet.Cells(matchingRow, 3), targetSheet.Cells(matchingRow, targetSheet.Columns.Count).End(xlToLeft)) ' Copy the data from the source range to the target range sourceRange.Copy targetRange End If Next i sourceWorkbook.Close False targetWorkbook.Close False End Sub - User
What about 10 different things similiar to this with no pattern path in perticular
- Assistant
- User
That's great. What if entirety of "Prva tabela" is written in Latin Serbian, and the entirety of "Druga tabela" is written in Cyrillic Serbian. Would excel be able to recognize "Репо" as "Repo"?
- Assistant
Excel may not recognize "Репо" as "Repo" if it is in Cyrillic Serbian, even though they have the same meaning. In this case, you would need to convert the text in either the source or target workbook to a common encoding, such as Unicode, before comparing the values. You can do this using the
StrConvfunction in VBA:Sub CopyDataFromOtherWorkbook() Dim sourceWorkbook As Workbook Dim sourceSheet As Worksheet Dim targetWorkbook As Workbook Dim targetSheet As Worksheet Dim sourceRange As Range Dim targetRange As Range Dim identifier As String Set sourceWorkbook = Workbooks.Open("C:\Path\To\Prva tabela.xlsx") Set sourceSheet = sourceWorkbook.Sheets("abc") Set targetWorkbook = Workbooks.Open("C:\Path\To\Druga tabela.xlsx") Set targetSheet = targetWorkbook.Sheets("xdy") ' Find the last row with data in the source sheet lastRow = sourceSheet.Cells(sourceSheet.Rows.Count, "A").End(xlUp).Row For i = 1 To lastRow identifier = sourceSheet.Cells(i, 1).Value ' Convert the identifier to Unicode encoding identifier = StrConv(identifier, vbUnicode) ' Check if the identifier exists in the target sheet If identifier = "Repo" Then ' Set the source range to the row with the matching identifier Set sourceRange = sourceSheet.Range(sourceSheet.Cells(i, 2), sourceSheet.Cells(i, sourceSheet.Columns.Count).End(xlToLeft)) ' Find the matching row in the target sheet matchingRow = Application.Match("8.9. Repo plasmani", targetSheet.Columns(2), 0) ' Set the target range to the matching row in the target sheet Set targetRange = targetSheet.Range(targetSheet.Cells(matchingRow, 3), targetSheet.Cells(matchingRow, targetSheet.Columns.Count).End(xlToLeft)) ' Copy the data from the source range to the target range sourceRange.Copy targetRange End If Next i sourceWorkbook.Close False targetWorkbook.Close False End Sub - User
What if some of the stuff that is being copied are numbers that need to be presented in thousands in "Druga tabela". Essentially, they have to be copied, but pasted to their respective cell in "Druga tabela" divided by 1000.
- Assistant
You can modify the code to divide the data by 1000 before pasting it into the target workbook:
Sub CopyDataFromOtherWorkbook() Dim sourceWorkbook As Workbook Dim sourceSheet As Worksheet Dim targetWorkbook As Workbook Dim targetSheet As Worksheet Dim sourceRange As Range Dim targetRange As Range Dim identifier As String Set sourceWorkbook = Workbooks.Open("C:\Path\To\Prva tabela.xlsx") Set sourceSheet = sourceWorkbook.Sheets("abc") Set targetWorkbook = Workbooks.Open("C:\Path\To\Druga tabela.xlsx") Set targetSheet = targetWorkbook.Sheets("xdy") ' Find the last row with data in the source sheet lastRow = sourceSheet.Cells(sourceSheet.Rows.Count, "A").End(xlUp).Row For i = 1 To lastRow identifier = sourceSheet.Cells(i, 1).Value ' Check if the identifier exists in the target sheet If identifier = "Repo" Then ' Set the source range to the row with the matching identifier Set sourceRange = sourceSheet.Range(sourceSheet.Cells(i, 2), sourceSheet.Cells(i, sourceSheet.Columns.Count).End(xlToLeft)) ' Find the matching row in the target sheet matchingRow = Application.Match("8.9. Repo plasmani", targetSheet.Columns(2), 0) ' Set the target range to the matching row in the target sheet Set targetRange = targetSheet.Range(targetSheet.Cells(matchingRow, 3), targetSheet.Cells(matchingRow, targetSheet.Columns.Count).End(xlToLeft)) ' Divide the source data by 1000 sourceRange = sourceRange.Value / 1000 ' Copy the divided data to the target range sourceRange.Copy targetRange End If Next i sourceWorkbook.Close False targetWorkbook.Close False End Sub
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.